Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

.NET 

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

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/...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. 👍
Thread: Backend Web-Dev. - by SomeWhiteGuy

WATCH OUT 'LIL HOE,
MY PARTNA STILL SMOKE ON PAROLE
selling -
megabits.org + freakin.pro
Cool. This shorten my method a lot. I used to declare one by one.
Thanks a lot.
Easier way in C#:
Code
Invoke((MethodInvoker)delegate
{
   //Any thing here.
});
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.
(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.
Thread: Backend Web-Dev. - by SomeWhiteGuy

WATCH OUT 'LIL HOE,
MY PARTNA STILL SMOKE ON PAROLE
selling -
megabits.org + freakin.pro
(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.
(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]()
Thread: Backend Web-Dev. - by SomeWhiteGuy

WATCH OUT 'LIL HOE,
MY PARTNA STILL SMOKE ON PAROLE
selling -
megabits.org + freakin.pro
(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.
(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


Possibly Related Threads...
Thread / Author Replies Views Last Post
by AnonynukSep 28, 2026 10:32 AM 2 43 Sep 28, 2026 10:32 AM
Last Post: Anonynuk
[.NET] by ExproMar 25, 2022 03:58 AM 8 734 Mar 25, 2022 03:58 AM
Last Post: SaketSaket
[.NET] by NaywynJun 7, 2020 12:33 AM 32 3,017 Jun 7, 2020 12:33 AM
Last Post: savaganka