Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET Create New Dispatcher

I am trying to create a second thread with dispatcher so that I can have the primary dispatcher (for the UI) completely stress-free, and have the UI constantly respondant.

Now, I could create multiple threads for each sub (or void in C#), but isn't it possible for me to create one new thread and grab it's dispatcher, and invoke to that? This is what I've done:

Private CheckLoopThread As New System.Threading.Thread(New System.Threading.ThreadStart(AddressOf CheckLoop))

CheckLoopThread.Priority = System.Threading.ThreadPriority.Lowest
CheckLoopThread.Start()
Dim Test As Windows.Threading.Dispatcher = Windows.Threading.Dispatcher.FromThread(CheckLoopThread)

However, the variable "Test" is after execution "Nothing". How is this possible? Is the another way to create a second dispatcher?

Answers are appreciated in any .NET form. Visual Basic or C#. I am working in VB.NET WPF on the .NET 4.0 framework.

Thanks in advance.

like image 500
René Sackers Avatar asked Jul 15 '11 16:07

René Sackers


1 Answers

Dispatcher.FromThread(...) will not create a Dispatcher and will return null if a Dispatcher has not already been created for the thread. To create a Dispatcher for a thread, you will have to access Dispatcher.CurrentDispatcher at least once on your CheckLoopThread. As it says on MSDN for Dispatcher.CurrentDispatcher:

If a Dispatcher is not associated with the current thread, a new Dispatcher will be created. This is not the case with the FromThread method. FromThread will return null if there is not a dispatcher associated with the specified thread

like image 132
Jakob Christensen Avatar answered Nov 16 '22 01:11

Jakob Christensen