Saturday 15 January 2011

c# - Control.Invoke method not returning -



c# - Control.Invoke method not returning -

i'm trying parallelize code create run faster. far, it's been headaches , no results.

i want update several datagridviews @ same time :

parallel.invoke( () => updatedgv1(), () => updatedgv2(), () => updatedgv3() );

i have tried using easy (but not optimal) way recommended everywhere on web (like here http://msdn.microsoft.com/en-us/library/ms171728(v=vs.85).aspx ).

private void updatedgv1() { /* stuff */ assignvalue(this.dgv1, colpos, rowpos, value); /* in loop */ } delegate void assignevaluecallback(datagridview dgv, int columnpos, int rowpos, string valeur); public void assignvalue(datagridview dgv, form form, int columnpos, int rowpos, string value) { if (dgv.invokerequired) { assignevaluecallbackd = new assignevaluecallback(assignvalue); dgv.invoke(d, new object[] { dgv, columnpos, rowpos, value }); } else { dgv[columnpos, rowpos].value = value; } }

the main thread gets stuck @ "parallel.invoke(...)" call, waiting other threads finish.

the threads created "parallel.invoke(...)" stuck @ point:

mscorlib.dll!system.threading.waithandle.internalwaitone(system.runtime.interopservices.safehandle waitablesafehandle, long millisecondstimeout, bool hasthreadaffinity, bool exitcontext) mscorlib.dll!system.threading.waithandle.waitone(int millisecondstimeout, bool exitcontext) system.windows.forms.dll!system.windows.forms.control.waitforwaithandle(system.threading.waithandle waithandle) system.windows.forms.dll!system.windows.forms.control.marshaledinvoke(system.windows.forms.control caller, system.delegate method, object[] args, bool synchronous) system.windows.forms.dll!system.windows.forms.control.invoke(system.delegate method, object[] args) etc

why stuck?

i assume you're calling parallel.invoke ui thread. if so, that's problem.

parallel.invoke blocks until calls finish... means you're blocking ui thread. tasks you're starting can't complete, because control.invoke blocks until phone call on ui thread has finished - subtasks waiting ui thread become available in order update ui, ui thread waiting subtasks finish.

you could prepare using begininvoke instead of invoke (and may want anyway) it's fundamentally bad thought utilize parallel.invoke in ui thread, exactly because blocks.

c# .net-4.0

No comments:

Post a Comment