VB.NET Delegates and Invoke - can somebody explain these to me?

断了今生、忘了曾经 提交于 2019-11-28 07:49:39
Adriaan Stander

OK, well done for getting so far.

Lets start with point 1.

From How to: Make Thread-Safe Calls to Windows Forms Controls

If you use multithreading to improve the performance of your Windows Forms applications, you must make sure that you make calls to your controls in a thread-safe way.

Access to Windows Forms controls is not inherently thread safe. If you have two or more threads manipulating the state of a control, it is possible to force the control into an inconsistent state.

So, as you can see, you need to ensure that when changing the state of a control, it is done in a threadsafe manner.

Now, the property Control.InvokeRequired checks whether the code you are executing is on a different thread to the one that originally created the control.

If it is, we need some way to call code on that original thread.

For this reason you need to use Control.Invoke Method to execute such code on the original thread.

Executes a delegate on the thread that owns the control's underlying window handle.

Now the thing is, you need to tell that thread what it should be executing, and this is done using the delegate.

Represents a delegate, which is a data structure that refers to a static method or to a class instance and an instance method of that class.

Now, the last thing I would recommend is that you look at what the differences between Delegates, Anonymous Delegates, Anonymous Methods and Lamda expressions.

InvokeRequired asks 'Am I on the right thread?', if so carry on, else I need a delegate - in your code you see a lambda Sub

 Invoke(Sub() LblStatus.Text = statusText)

The other way this could work is to direct the result to a diff Sub routine(where the delegate has crossed over to the correct thread), but here we are able to run a Sub inside the Invoke method itself - just a simpler way to do it.

A delegate is required any time you use a separate thread to do work async.

Invoke - MSDN

qwr
  1. BackgroundWorker is another way to have thread and make thread-safe call from it. on ProgressChanged, and RunWorkerCompleted you can make thread-safe calls to UI

  2. Delegate is just reference to the method

Control.Invoke(Delegate)

Executes the specified delegate on the thread that owns the control's underlying window handle.

Control.InvokeRequired

Gets a value indicating whether the caller must call an invoke method when making method calls to the control because the caller is on a different thread than the one the control was created on.

And Why we need it all these stuffs . From msdn document it is clarified :

If you have two or more threads manipulating the state of a control, it is possible to force the control into an inconsistent state. Other thread-related bugs are possible, such as race conditions and deadlocks. It is important to make sure that access to your controls is performed in a thread-safe way.

So for safety reasons we have to implement this, otherwise our worker threads and UI thread may both try to access the data member at once, and it will wreak havoc on our application . So instead of disabling it and having a chance crash possibility of your application, you should better use thread-safe patterns .

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!