What happen if the UI Thread returned from Application.Run while other thread is trying to call Invo

  • Thread starter Thread starter babylon
  • Start date Start date
B

babylon

the UI Thread can't Invoke the function, right?
and 'the other thread' block indefinitely?

if so, any solution?
thx
 
babylon said:
the UI Thread can't Invoke the function, right?
and 'the other thread' block indefinitely?

if so, any solution?
thx

Hi,

You would probably get an exception saying that you are trying to call
Invoke on an already disposed object.

You should synchronize, and not call invoke if the application is shutting
down. You can use a mutex for that (System.Threading.Mutex).

HTH,
 
You should synchronize, and not call invoke if the application is shutting
down. You can use a mutex for that (System.Threading.Mutex).

Perhaps this is not enough information.

Do not let your thread call Invoke on the UI-thread directly.

Instead supply the thread with a delegate, which points to a method that
first checks whether the application is shutting down. For instance, the
delegate could be a method of your form class, which checks a "bool
isFormClosing" in your form class. Initialize it to false. In the form's
closing event handler, begin by setting it to true but you should guard this
by a mutex (just protect setting it to true). Then in your callback
delegate, check isClosing first before calling Invoke and you should guard
the whole method using the same mutex mentioned earlier. This ensures that
Invoke will not get called on a disposed form. Invoke wil be called only if
isClosing is false, and since the whole method is guarded by the mutex, the
call to Invoke is guaranteed to be done on a live form object. If you don't
know how to use Mutex check msdn.

Hope this helps,
Tom.
 
Back
Top