Note that there are some explanatory texts on larger screens.

plurals
  1. POVB.net avoiding cross thread exception with extension method
    primarykey
    data
    text
    <p>Hello I am trying to implement a solution for updating form controls without using a delegate.</p> <p>I am attempting to use the 1st solution on this page:</p> <p><a href="http://www.dreamincode.net/forums/blog/143/entry-2337-handling-the-dreaded-cross-thread-exception/" rel="nofollow">http://www.dreamincode.net/forums/blog/143/entry-2337-handling-the-dreaded-cross-thread-exception/</a></p> <pre><code>Imports System.ComponentModel Imports System.Runtime.CompilerServices Public Module MyInvoke &lt;Extension()&gt; _ Public Sub CustomInvoke(Of T As ISynchronizeInvoke)(ByVal control As T, ByVal toPerform As Action(Of T)) If control.InvokeRequired Then control.Invoke(toPerform, New Object() {control}) toPerform(control) End If End Sub End Module </code></pre> <p>The site gives this as example of how to use:</p> <pre><code>Label1.CustomInvoke(l =&gt; l.Text = "Hello World!") </code></pre> <p>But i get 'l' is not declared error. As you can see im very new to VB or any OOP.</p> <p>I can get the second solution on that page to work (using delegates) but i have quite a few things to do in this thread and it seems like i would need to write a new delegate sub for each thing, which seems wasteful.</p> <p>What i need to do is select the 1st item from a combobox, update a textbox.text with the selected item, and pass the selected item to a function. Then wait for x seconds and start again, selecting the second item.</p> <p>I can get it to work in a single threaded application, but i need the interface to remain responsive.</p> <p>Any help greatly appreciated.</p> <p>EDIT: OK so changing the syntax worked for the example. However if i change it from</p> <pre><code>Label1.CustomInvoke(Sub(l) l.text = "hello world!") </code></pre> <p>(which worked just fine) to:</p> <pre><code>Dim indexnumber As Integer = 0 ComboBox1.CustomInvoke(Sub(l) l.SelectedIndex = indexnumber) </code></pre> <p>I get a cross threading error as though i didnt even use this method:</p> <pre><code>Cross-thread operation not valid: Control 'ComboBox1' accessed from a thread other than the thread it was created on. </code></pre> <p>So now im back to where i started? Any further help very much appreciated.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload