- 題名: 表示前のFormでのinvokeについて
- 日時: 2005/12/22 11:37:27
- ID: 14296
- この記事の返信元:
- (なし)
- この記事への返信:
- [14299] Re[1]: 表示前のFormでのinvokeについて2005/12/22 11:50:55
- ツリーを表示
まどかさんありがとうございます。
Application.Runの前にハンドルを参照することで解決しました。
また何かありましたらよろしくお願いします。
ソースコード---
Public Class Class1
Shared Sub main()
Dim f1 As New Form1
Dim f2 As New Form2
Dim th As New Mythread
f1.addform2(f2)
f1.addmythread(th)
f2.addmythread(th)
Dim h As Integer = f2.Handle.Toint32
th.start()
Application.Run(f1)
End Sub
End Class
分類:[.NET]
いつも参考にさせていただいています。 .NET Tips 時間のかかる処理の進行状況を表示する を参考に別スレッドから UI変更をしようとしているのですが、一度もshowしていないFormでinvokeすると エラーが発生してしまいます。 コンストラクタを呼び出すだけではウィンドウハンドルは作成されないのでしょうか? 上手い解決方法をご教授お願いします。 エラーメッセージ--- ウィンドウ ハンドルが作成される前、コントロールで Invoke または InvokeAsync を呼び出せません。 ソースコード--- Public Class Class1 Shared Sub main() Dim f1 As New Form1 Dim f2 As New Form2 Dim th As New Mythread f1.addform2(f2) f1.addmythread(th) f2.addmythread(th) th.start() Application.Run(f1) End Sub End Class Public Class Form1 Inherits System.Windows.Forms.Form #Region " Windows フォーム デザイナで生成されたコード " #End Region Private _f2 As Form2 Private WithEvents _th As Mythread Private Delegate Sub SetColordelegate() Public Sub addform2(ByVal f2 As Form2) _f2 = f2 End Sub Public Sub addmythread(ByVal th As Mythread) _th = th End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click _f2.Show() End Sub Public Sub SetColor() Me.BackColor = System.Drawing.Color.Red End Sub Private Sub _th_SecondThreadEvent() Handles _th.SecondThreadEvent Me.Invoke(New SetColordelegate(AddressOf SetColor)) End Sub End Class Public Class Form2 Inherits System.Windows.Forms.Form #Region " Windows フォーム デザイナで生成されたコード " #End Region Private Delegate Sub SetColordelegate() Private WithEvents _th As Mythread Public Sub addmythread(ByVal th As Mythread) _th = th End Sub Public Sub SetColor() Me.BackColor = System.Drawing.Color.Red End Sub Private Sub _th_SecondThreadEvent() Handles _th.SecondThreadEvent 'エラー発生 Me.Invoke(New SetColordelegate(AddressOf SetColor)) End Sub End Class Public Class Mythread Public Event SecondThreadEvent() Private SecondThread As System.Threading.Thread Public Sub start() SecondThread = New System.Threading.Thread(AddressOf SecondMethod) SecondThread.Start() End Sub Private Sub SecondMethod() System.Threading.Thread.Sleep(5000) RaiseEvent SecondThreadEvent() End Sub End Class