How can ThreadPool directly access the controls of another thread?

In the application you posted, you are not accessing any controls from the ThreadPool, you are only calling Refresh on the form. This actually sends a message to the form to tell itself to redraw itself, but that message is received on the main GUI thread rather than from the ThreadPool thread.

In the application you posted, you are not accessing any controls from the ThreadPool, you are only calling Refresh on the form. This actually sends a message to the form to tell itself to redraw itself, but that message is received on the main GUI thread rather than from the ThreadPool thread. Therefore, you need not Invoke in your case because you do not do any cross thread activity.

The OnPaint method gets called indirectly via the windows message pump rather than directly from the Refresh method. If you were to try and, for example, set the text of a textbox from a background thread... it would throw an exception, and you would be required to use the Invoke pattern to make it work.

1 +1, @Searock you can see the effect by calling Console. WriteLine(Thread. ManagedThreadId); at OnPaint method so you will see that it only called from the UI thread.

– Jalal Aldeen Saa'd Jul 24 at 8:32.

The ThreadPool is a pool of reusable threads that you can use to execute code. The ThreadPool threads run on a separate thread to the UI thread. You're concerned with the UI thread.

Any thread can access the UI thread, however, it is not thread safe. That's the key word here. It might work, it might not.

If it does, well, you had good fortune. Not thread safe simply means that you are not guaranteed a consistent and expected behaviour. Setting something now might work, but a millisecond later, it might fail randomly without reason.

With WinForms, . NET by default checks for cross-thread calls, but we can disable this and access the UI in a non-thread safe context by setting System.Windows.Forms.Control. CheckForIllegalCrossThreadCalls to false.

However, disabling this means that we need to expect and cater for random behaviour.

You can access from other thread to elements, but it's not safe. If several thread try to access you control concurrently it throws an exception. Because WF controls not thread safe.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions