Why you have to update UI from a thread?

Indeed…

From your “Switching to async” conversation, there was one point where you had set up a thread class that wasn’t actually running in a separate thread:

Have you resolved that setup error? Or are you perhaps still running your “thread” code in the main thread?

I resolved it Tim, but I never really understood the difference between sub classing thread placing it on a form as a control, implementing the Run Method in the subclass vs sub classing thread, placing the subclass as a control on the form and adding the Run Event handler. There is a subtle difference I didn’t get.

The thing that trips people up is that there’s nothing magic about the thread class itself that automatically makes your code run in a separate thread. You can create a thread subclass and give it methods, but when you call those methods, they don’t automatically get run in a separate thread. Also, your method can invoke the thread’s Run event, but it still won’t happen in a separate thread.

The one and only thing that makes code run in a separate thread is the Thread.Run method. When you call that method, it creates a separate thread in your app and calls the Thread.Run event in that thread. In your previous example, you made your own thread.run method, but it lacked the ability to create a separate thread in the app, so everything happened in the main thread.

Another thing to bear in mind is that code is run in the thread that called it, regardless of where it is defined. This means that calling a method on a thread subclass doesn’t make that method run in a separate thread (unless the Thread.Run method is called as part of it). It also means that any code called from a separate thread is run on that thread, regardless of where in your project the code is defined. You can call a window method from a thread and that method is run as part of the thread, not in the context of the window.

Does that help clarify? If not, ask questions.

Tim

So when I called Insert to insert an object into the queue, it checked the thread to see if it was running, if it wasn’t insert (a method of the thread subclass) called run, which raised an event in the main thread to actually run the code I need.