[Maxscript] DotNet DataGridView Add() + MultiThreading = Max Crash

polycounter lvl 11
Offline / Send Message
SimonT polycounter lvl 11
I try to fill a DotNet DataGridView via MultiThreading. It makes Max crash (Max gets totally unresponsive and i have to kill it via task manager). Any help would be awesome!

CANy7c5UsAAUzXl.png:large

Problem
If i add more rows (via multithreading) than "fit" (so that a scrollbar has to be created), 3Ds Max crashes. It works if you just execute "addItems()" without any Multithreading.

Try the snipped below. As soon as you comment out the 3rd "dgv.rows.add()" it works (because no scrollbar has to be created?!).

Alternatives
Might be possible to modify the DataSource of the DatagridView directly via DotNet DataTable but it seems that this doesn't work (except you use a dotNet form instead of a Maxscript Rollout). The DataGridView doesn't show any data/stays gray) - found one thread where a guy tried the same, without success.

Questions
1. Any idea how i can modify (add/remove) the DataGridView without crashing max?
2. Any idea how i can bind a dataTable to the DataGridView in a MaxscriptRollout?
(
	rollout dgTest "DataGridView Test" width:448 height:600
	(
		dotNetControl dgv "System.Windows.Forms.DataGridView" pos:[8,8] width:432 height:110
		
		fn addItems = (
			
			dgv.rows.add()
			dgv.rows.add()
			--Comment this out to avoid a Max crash
			dgv.rows.add()
		)
		
		on dgTest open do (
			
			--Create a Column and add it to the DataGridView
			infoCol = dotNetObject "System.Windows.Forms.DataGridViewTextBoxColumn"
			infoCol.headerText = "Info"
			dgv.columns.add infoCol
			
			--Execute the addItems Function via MultiThreading
			myThread = dotnetobject "CSharpUtilities.SynchronizingBackgroundWorker"
			dotnet.addEventHandler myThread "DoWork" addItems
			myThread.runWorkerAsync()
		)
	)

	createDialog dgTest
)

Replies

  • monster
    Offline / Send Message
    monster Polycount Sponsor
    It doesn't crash for me, but there is about a 4 second system freeze when I run the script. Adding the 3rd line isn't causing the slow down. I can add 10 lines quickly, it's right after it draws the scrollbar that the freeze happens.

    I'm not sure this is the right place to use multithreading. Even the regular 3ds Max UI is all single threaded.

    But if scrollbars are the problem, then just disable them while you fill out the grid.
    (
    	rollout dgTest "DataGridView Test" width:448 height:600
    	(
    		dotNetControl dgv "System.Windows.Forms.DataGridView" pos:[8,8] width:432 height:110
    		
    		fn addItems sender e= (
    			
    			for i = 1 to 20 do
    			(
    				dgv.rows.add()
    			)
    		)
    		
    		fn addScrollbars sender e =
    		(
    			dgv.scrollbars = dgv.scrollbars.both
    		)
    		
    		on dgTest open do (
    			
    			--Create a Column and add it to the DataGridView
    			infoCol = dotNetObject "System.Windows.Forms.DataGridViewTextBoxColumn"
    			infoCol.headerText = "Info"
    			dgv.columns.add infoCol
    			dgv.scrollbars = dgv.scrollbars.none
    			
    			--Execute the addItems Function via MultiThreading
    			myThread = dotnetobject "CSharpUtilities.SynchronizingBackgroundWorker"
    			dotnet.addEventHandler myThread "DoWork" addItems
    			dotnet.addEventHandler myThread "RunWorkerCompleted" addScrollbars
    			myThread.runWorkerAsync()
    		)
    	)
    
    	createDialog dgTest
    )
    
  • haiddasalami
    Offline / Send Message
    haiddasalami polycounter lvl 7
    I havent had much experience or luck with multithreading in Max. Tried a while back to create a system for cross-app talk using a Python server between Max and Maya for some artists and the FBX import would just freeze Max and having a background worker didnt seem to help. Anywho ran your code with no hitch besides the 4 second lag Monster talked about. This is on 2015 Max...hmm should see maybe if its fixed in newer versions. This was back on Max 2012.
  • monster
    Offline / Send Message
    monster Polycount Sponsor
    The consensus over at CgSociety is that multithreading works great if you have a lot of calculations to do, but anything that actually affects the scene just won't work. I've never seen anyone try to use multithreading with UI. [edit]Until now.[/edit]
  • SimonT
    Offline / Send Message
    SimonT polycounter lvl 11
    Thanks Monster!!

    I use Max 2011 and here Max freezes not only 4 seconds but at least 3 minutes, if not more. BUT, disabling the scrollbars works!!!

    Why i use MT:

    I crawl through a lot data and already fill the list (single threaded) but then in addition i change the color of the cells and add images to an extra row. This already works and feels really fluent.
    My next step is to not clear() the list everytime when somthing changes - i want a background-thread which checks what's in the list and what has to be added/deleted and only that stuff shall be touched - the rest shall stay. I hope this will lead to a good UX feeling :D

    Any Ideas about managing to manipulate and show the Datasource? (was my 2nd question)
Sign In or Register to comment.