[.Net] Invoking from enemy to friend

Discussion in 'Code Snippets and Tutorials' started by the_skip, Sep 30, 2007.

  1. the_skip

    the_skip Level IV

    Joined:
    Dec 25, 2006
    Messages:
    2,354
    Likes Received:
    1
    Location:
    Indiana
    If you are like many people and decided to move to .net you'll see threading and find it as a plus and for any neopet project a must, but cross thread operations arn't allowed. You heard of invoking but don't get a hang of it, but now I will show you how to use invoking to be your best friend
    Note:
    I am using C# and used a web converter to convert to vb.net

    Problem 1: You have to add an item to a listbox from another thread
    Solution 1:
    Code (C#):
    1. <div class="csharp" id="{CB}" style="font-family: monospace;"><ol><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp;</li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;"><span style="color: #008080; font-style: italic;">//This is a delgate, &nbsp;think of a delegate as a replacement for a function</span></li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;"><span style="color: #FF0000;">delegate</span> <span style="color: #0600FF;">void</span> AddListItem<span style="color: #000000;">(</span><span style="color: #FF0000;">string</span> text<span style="color: #000000;">)</span>;</li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;"><span style="color: #0600FF;">private</span> <span style="color: #0600FF;">void</span> AddListBoxItem<span style="color: #000000;">(</span><span style="color: #FF0000;">object</span> item<span style="color: #000000;">)</span></li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">{</span></li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">if</span> <span style="color: #000000;">(</span><span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">listBox1</span>.<span style="color: #0000FF;">InvokeRequired</span><span style="color: #000000;">)</span></li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">{</span></li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// THis is not the same thread as the form</span></li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">//So now we have to make a delgate, &nbsp;then tell it which function to replace</span></li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// THe next argument is the arguments to the function it is replacing</span></li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">listBox1</span>.<span style="color: #0000FF;">Invoke</span><span style="color: #000000;">(</span>[url=http://www.google.com/search?q=new+msdn.microsoft.com]<span style="color: #008000;">new</span>[/url] AddListBoxItem<span style="color: #000000;">(</span><span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">AddListBoxItem</span><span style="color: #000000;">)</span>, item<span style="color: #000000;">)</span>;</li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">}</span></li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">else</span></li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">{</span></li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// This is the thread the form is on so we can directly aces it</span></li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">listBox1</span>.<span style="color: #0000FF;">Items</span>.<span style="color: #0000FF;">Add</span><span style="color: #000000;">(</span>item<span style="color: #000000;">)</span>;</li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">}</span></li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">}</span></li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp;</li></ol></div>
    Code (vb.net):
    1. <div class="vbnet" id="{CB}" style="font-family: monospace;"><ol><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;"><span style="color: #008080; font-style: italic;">'This is a delgate, &nbsp;think of a delegate as a replacement for a function</span></li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;"><span style="color: #0600FF;">Delegate</span> <span style="color: #0600FF;">Sub</span> AddListItem<span style="color: #000000;">(</span><span style="color: #FF8000;">ByVal</span> text <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span><span style="color: #000000;">)</span></li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;"><span style="color: #FF8000;">Private</span> <span style="color: #0600FF;">Sub</span> AddListBoxItem<span style="color: #000000;">(</span><span style="color: #FF8000;">ByVal</span> item <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Object</span><span style="color: #000000;">)</span></li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; <span style="color: #0600FF;">If</span> <span style="color: #FF8000;">Me</span>.<span style="color: #0000FF;">listBox1</span>.<span style="color: #0000FF;">InvokeRequired</span> <span style="color: #FF8000;">Then</span></li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">' THis is not the same thread as the form</span></li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">'So now we have to make a delgate, &nbsp;then tell it which function to replace</span></li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">' THe next argument is the arguments to the function it is replacing</span></li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #FF8000;">Me</span>.<span style="color: #0000FF;">listBox1</span>.<span style="color: #0000FF;">Invoke</span><span style="color: #000000;">(</span><span style="color: #FF8000;">New</span> AddListBoxItem<span style="color: #000000;">(</span><span style="color: #FF8000;">Me</span>.<span style="color: #0000FF;">AddListBoxItem</span><span style="color: #000000;">)</span>, item<span style="color: #000000;">)</span></li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; <span style="color: #FF8000;">Else</span></li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">' This is the thread the form is on so we can directly aces it</span></li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #FF8000;">Me</span>.<span style="color: #0000FF;">listBox1</span>.<span style="color: #0000FF;">Items</span>.<span style="color: #0000FF;">Add</span><span style="color: #000000;">(</span>item<span style="color: #000000;">)</span></li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">If</span></li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;"><span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Sub</span></li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp;</li></ol></div>
    Alright, from this code you can tell that a delgegate takes the place of a function (profunction) and then when you invoke you also have to pass a new delaget and its arguments. The next problem will show how to use delages with more than one argument.
    Probelm 2: You need to edit a listbox item
    Solustion 2:
    Code (C#):
    1. <div class="csharp" id="{CB}" style="font-family: monospace;"><ol><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp;</li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;"><span style="color: #008080; font-style: italic;">//since we have to be using two arguments we have it match up with the two arguments in the chnage text</span></li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;"><span style="color: #FF0000;">delegate</span> <span style="color: #0600FF;">void</span> ChangeListItem<span style="color: #000000;">(</span><span style="color: #FF0000;">int</span> index, <span style="color: #FF0000;">string</span> text<span style="color: #000000;">)</span>;</li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;"><span style="color: #0600FF;">private</span> <span style="color: #0600FF;">void</span> changelistitem<span style="color: #000000;">(</span><span style="color: #FF0000;">int</span> index, <span style="color: #FF0000;">string</span> text<span style="color: #000000;">)</span></li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">{</span></li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">if</span> <span style="color: #000000;">(</span>listBox1.<span style="color: #0000FF;">InvokeRequired</span><span style="color: #000000;">)</span></li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">{</span></li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">//invokes</span></li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">listBox1</span>.<span style="color: #0000FF;">Invoke</span><span style="color: #000000;">(</span>[url=http://www.google.com/search?q=new+msdn.microsoft.com]<span style="color: #008000;">new</span>[/url] ChangeListItem<span style="color: #000000;">(</span><span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">changelistitem</span><span style="color: #000000;">)</span>, index, text<span style="color: #000000;">)</span>; <span style="color: #008080; font-style: italic;">//notice how the arguments come after the delgaration of the delegate</span></li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">}</span></li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">else</span></li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">{</span></li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">listBox1</span>.<span style="color: #0000FF;">Items</span><span style="color: #000000;">[</span>index<span style="color: #000000;">]</span> = text;</li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">}</span></li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">}</span></li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp;</li></ol></div>
    Code (vb.net):
    1. <div class="vbnet" id="{CB}" style="font-family: monospace;"><ol><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;"><span style="color: #008080; font-style: italic;">'since we have to be using two arguments we have it match up with the two arguments in the chnage text</span></li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;"><span style="color: #0600FF;">Delegate</span> <span style="color: #0600FF;">Sub</span> ChangeListItem<span style="color: #000000;">(</span><span style="color: #FF8000;">ByVal</span> index <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Integer</span>, <span style="color: #FF8000;">ByVal</span> text <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span><span style="color: #000000;">)</span></li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;"><span style="color: #FF8000;">Private</span> <span style="color: #0600FF;">Sub</span> changelistitem<span style="color: #000000;">(</span><span style="color: #FF8000;">ByVal</span> index <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Integer</span>, <span style="color: #FF8000;">ByVal</span> text <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span><span style="color: #000000;">)</span></li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; <span style="color: #0600FF;">If</span> listBox1.<span style="color: #0000FF;">InvokeRequired</span> <span style="color: #FF8000;">Then</span></li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">'invokes</span></li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">'notice how the arguments come after the delgaration of the delegate</span></li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #FF8000;">Me</span>.<span style="color: #0000FF;">listBox1</span>.<span style="color: #0000FF;">Invoke</span><span style="color: #000000;">(</span><span style="color: #FF8000;">New</span> ChangeListItem<span style="color: #000000;">(</span><span style="color: #FF8000;">Me</span>.<span style="color: #0000FF;">changelistitem</span><span style="color: #000000;">)</span>, index, text<span style="color: #000000;">)</span></li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; <span style="color: #FF8000;">Else</span></li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #FF8000;">Me</span>.<span style="color: #0000FF;">listBox1</span>.<span style="color: #0000FF;">Items</span><span style="color: #000000;">(</span>index<span style="color: #000000;">)</span> = text</li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">If</span></li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;"><span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Sub</span></li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp;</li></ol></div>
    With this you should be able to use invokes alot more easily, I'll post some more problems and solutions later