你好 我想實現一個解決方案來更新表單控件而不使用委託。VB.net避免與擴展方法的交叉線程異常
我試圖使用此頁面上的第一個解決方案:
http://www.dreamincode.net/forums/blog/143/entry-2337-handling-the-dreaded-cross-thread-exception/
Imports System.ComponentModel
Imports System.Runtime.CompilerServices
Public Module MyInvoke
<Extension()> _
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
該網站給出這個作爲例子說明如何使用:
Label1.CustomInvoke(l => l.Text = "Hello World!")
,但我得到' l'沒有聲明錯誤。你可以看到我對VB或任何OOP都很陌生。
我可以得到第二個解決方案在該頁面上工作(使用委託),但我有很多事情要做在這個線程中,好像我需要爲每個東西寫一個新的委託子,這似乎浪費。
我需要做的是從組合框中選擇第一項,使用所選項更新textbox.text,並將所選項傳遞給函數。 然後等待x秒鐘並重新開始,選擇第二個項目。
我可以讓它在單線程應用程序中工作,但我需要接口保持響應。
任何幫助非常感謝。
編輯: 確定,以便更改爲該示例工作的語法。 但是如果我改變它從
Label1.CustomInvoke(Sub(l) l.text = "hello world!")
(這只是正常工作)到:
Dim indexnumber As Integer = 0
ComboBox1.CustomInvoke(Sub(l) l.SelectedIndex = indexnumber)
我得到一個跨線程錯誤,就像我沒有,甚至使用此方法:
Cross-thread operation not valid: Control 'ComboBox1' accessed from a thread other than the thread it was created on.
那麼現在我回到我開始的地方? 任何進一步的幫助非常感謝。
謝謝,愚蠢的錯誤在那裏。 – Steve 2011-01-13 20:33:25