1

「Blahing」という名前のモジュール内のサブコードは次のとおりです。

    Sub BlahBlah(ByVal Count As Long)
        For i As Long = 0 To Count
            frmBlaher.txtBlah.Appendtext("Blah")
        Next
    End Sub

frmBlaher というフォーム内のボタン クリック イベント コードを次に示します。

     Private Sub WriteBlah_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles WriteBlah.Click
         Dim Thread As New Threading.Thread(Sub() Blahing.BlahBlah(Val(_
              TxtBlahCount.Text)))

         Thread.Start()
     End Sub

txtBlahCount に任意の数値 (たとえば 10) を入力して WriteBlah ボタンを押しても、何も起こりません。複数のブレークポイントを設定したところ、"Appendtext" 部分が発生するのに機能しないことがわかりました。txtBlah の Text_Changed イベントを確認したところ、発生しましたが、唯一の問題は、txtBlah にテキストが表示されないことです。私はマルチスレッドが初めてです。以前にこの質問に対する多くの回答を読みましたが、どれも例を示していませんでした。手伝ってくれる?

4

3 に答える 3

2

少し異なるコードを実行します。これは、構造がvb.netのマルチスレッドのように見える方法です(Vb.netが名前空間をモデルに渡さないことに関係しています)

これは、読み込み中の MainThread からの startThread になります。

Private Sub DoSomethingSimple()
    Dim DoSomethingSimple_Thread As New Thread(AddressOf DoSimple)
    DoSomethingSimple_Thread.Priority = ThreadPriority.AboveNormal
    DoSomethingSimple_Thread.Start(Me)
End Sub

これは実際のスレッド自体になります(新しいモデル/クラスまたは同じクラス内)

Private Sub DoSimple(beginform As Form)
    'Do whatever you are doing that has nothing to do with ui

    'For UI calls use the following
    SomethingInvoked(PassibleVariable, beginform)

End Sub

メイン スレッドへの呼び出しごとにデリゲート メソッドと呼び出しメソッドを記述します。

Delegate Sub SomethingInvoked_Delegate(s As Integer, beginform As Form)
Sub SomethingInvoked_Invoke(ByVal s As Integer, beginform As Form)
    If beginform.NameOfControlYouAreUpdating.InvokeRequired Then ' change NameOfControlYouAreUpdating to the Name of Control on the form you wish to update
        Dim d As New SomethingInvoked_Delegate(AddressOf SomethingInvoked_Invoke)
        beginform.Invoke(d, New Object() {s, beginform})
    Else

        'Do something...
        beginform.NameOfControlYouAreUpdating.Condition = Parameter

    End If
End Sub

これは、vb.net でスレッドを記述する (ぶら下がっていない) 方法でテストされています。

あなたのコードをこのテンプレートに実装するのにさらに助けが必要な場合は、私に知らせてください:P

于 2013-07-25T20:53:54.157 に答える
1

これは、コントロールを作成したスレッド以外のスレッドからコントロールを更新しようとしているためです。Control.Invoke および Control.InvokeRequired メソッドを使用すると、これを回避できます。Control.Invoke は、渡されたデリゲートをコントロールを作成したスレッドで実行します。

私はVBをまったく使用していませんが、次のようなことを試すことができます:

Delegate Sub BlahBlahDelegate(ByVal Count As Long)

Sub BlahBlah(ByVal Count As Long)
    If frmBlaher.txtBlah.InvokeRequired Then
        Dim Del As BlahBlahDelegate
        Del = new BlahBlahDelegate(AddressOf BlahBlah)
        frmBlaher.txtBlah.Invoke(Del, New Object() { Count })
    Else
        For i As Long = 0 To Count
            frmBlaher.txtBlah.AppendText("Blah")
        Next
    End If
End Sub
于 2013-07-25T20:07:02.403 に答える