Reputation: 51
I have two threads, main and another one that I created to do some work. I don't understand what happens when I call originalContext.Post (SyncronizationContext of main thread) from that other thread, where some UI elements are updated, when main thread is already terminated ? Is this safe and why ?
Thanks in advance.
Upvotes: 3
Views: 187
Reputation: 942178
This is of course not going to come to very good end. It depends on the actual synchronization provider, there is more than one. Trying to focus a bit on the common ones, WindowsFormsSynchronizationContext and WpfSynchronizationContext. Both will flush any pending posts from the queue, they'll just disappear without a trace. If you continue on posting then Winforms will throw an InvalidOperationException. Wpf is more convoluted, it can set the DispatcherOperation.Status field to indicate that it didn't work. But as near as I can tell, this field is not being observed by the context code so it should just fall in the bit-bucket silently.
Clearly you never want this to happen. The simple workaround is Thread.IsBackground = true to let the CLR clean up the mess, it is not often desirable to let a program continue without a UI.
Upvotes: 3