Jun 1, 2012 06:18 PM
(This post was last modified: Jun 1, 2012 06:36 PM by SomeWhiteGuy.)
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. 👍
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
WATCH OUT 'LIL HOE,
MY PARTNA STILL SMOKE ON PAROLE
selling -
megabits.org + freakin.pro

