Hack Forums
[.NET] [Tutorial - VB.NET & C#] How to use Delegates to update controls on another thread. - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [.NET] [Tutorial - VB.NET & C#] How to use Delegates to update controls on another thread. (/showthread.php?tid=2547239)



[Tutorial - VB.NET & C#] How to use Delegates to update controls on another thread. - SomeWhiteGuy - Jun 1, 2012

Final outcome (Click to View)
Sometimes you might need to update a control on another thread, like a RichTextBox (Will be referred to as "RTB" throughout).

You cannot just simply do this, you would have to create a Delegate in order to not get an error that says:

[IMG]http://i1114.photobucket.com/albums/k526/RoG_FaNBoY/crossThreadError.png[/IMG]

So let's go over delegates right quick.

What is a delegate?
"A delegate is a type* that references a method. Once a delegate is assigned a method, it behaves exactly like that method. The delegate method can be used like any other method, with parameters and a return value." - MSDN

* Information on types: http://msdn.microsoft.com/en-us/library/ms173104.aspx
Information on methods: http://msdn.microsoft.com/en-us/library/ms173114%28v=vs.80%29.aspx

Delegates are not really that confusing once you get used to them, I'm writing this tutorial because I recently learned Delegates and at first I thought that it was going to be impossible, lol.

So let's create a delegate sub right quick.
C#:
[img]http://ucap.coding-forums.com/SomeWhiteGuy/lKsDbXxULWmZPRlcYife.png[/img]

VB.NET:
[img]http://ucap.coding-forums.com/SomeWhiteGuy/KJPsZqfMnPWiyKHvbNeC.png[/img]

Now let's create our sub that is going to do the actual updating.
C#:
[img]http://ucap.coding-forums.com/SomeWhiteGuy/UxnNQKJuPXMNensiLatc.png[/img]

VB.NET:
[img]http://ucap.coding-forums.com/SomeWhiteGuy/QkadqRkxeRHBPooCYtqx.png[/img]

Now we need to create another sub that is going to be the sub that you're using for the thread. So we'll just call that someRandomSub()

It should look like this:
[img]http://ucap.coding-forums.com/SomeWhiteGuy/ArCgCiteuAhExupvPRRX.png[/img]

Now we need to declare a thread, it's in the System.Threading namespace, hopefully you'll be able to do that by yourself. The thread needs to point to our sub that we created, in my case; the subs called someRandomSub().

Go ahead and run that and see what happens, you should get the error that I showed at the beginning on this post.

Now on the sub that we just created; we're going to have to check to see if an Invoke(is)Required. (Which it will be, but it's always good to check.) So we can avoid the error that we just encountered.

Let's add in what we need to prevent that error.
C#:
[img]http://ucap.coding-forums.com/SomeWhiteGuy/SDrTVnJelXFeuBqIqbcw.png[/img]

VB.NET:
[img]http://ucap.coding-forums.com/SomeWhiteGuy/JtQOcXhHMsXiuhxVYxuh.png[/img]

And that's it! It should work now.

Hope you learned a little something about delegates. 👍



* - unknowmaster - Jun 1, 2012

Cool. This shorten my method a lot. I used to declare one by one.
Thanks a lot.


* - Deceptive - Jun 1, 2012

Easier way in C#:
Code
Invoke((MethodInvoker)delegate
{
   //Any thing here.
});



* - Killer1478 - Jun 1, 2012

I enjoyed this, but for picturebox's or combo box's how would a person use a delegate for that?
I also watched Perplexity's video, but never understood how to port it to a picture box or some such thing.


* - SomeWhiteGuy - Jun 1, 2012

(Jun 1, 2012 07:07 PM)Killer1478 Wrote:  I enjoyed this, but for picturebox's or combo box's how would a person use a delegate for that?
I also watched Perplexity's video, but never understood how to port it to a picture box or some such thing.

What are you trying to do with a combo box?

PictureBox would be easy.

Just change the "rtb" part to a picture box control and the [text] part to a bitmap.


* - Killer1478 - Jun 1, 2012

(Jun 1, 2012 07:11 PM)SomeWhiteGuy Wrote:  What are you trying to do with a combo box?

PictureBox would be easy.

Just change the "rtb" part to a picture box control and the [text] part to a bitmap.

Add items to. From there I should be able to work out the rest.


* - SomeWhiteGuy - Jun 1, 2012

(Jun 1, 2012 07:23 PM)Killer1478 Wrote:  Add items to. From there I should be able to work out the rest.

Declare an array and loop through it and add each string to the combo box, that would work.

Just replace "rtb" with a combo box control and [text] with an array eg: [text]()


* - Killer1478 - Jun 1, 2012

(Jun 1, 2012 08:14 PM)SomeWhiteGuy Wrote:  Declare an array and loop through it and add each string to the combo box, that would work.

Just replace "rtb" with a combo box control and [text] with an array eg: [text]()

Ah, I see. Thank you for your time cant wait to use it in a project.


* - DotNetNut - Jun 2, 2012

(Jun 1, 2012 07:06 PM)Deceptive Wrote:  Easier way in C#:
Code
Invoke((MethodInvoker)delegate
{
   //Any thing here.
});

Can that also be done in vb.net?

Gonna try as i have heard a few of you talk about it before but never actually tried it ... :P