C# と VB.NET の質問掲示板

わんくま同盟

ASP.NET、C++/CLI、Java 何でもどうぞ

C# と VB.NET の入門サイト


(過去ログ 17 を表示中)
■6760 / )  別スレッドでShowDialogしたフォームのクローズ
□投稿者/ 困ったちゃん (1回)-(2007/08/23(Thu) 12:57:42)

分類:[VB.NET/VB2005] 

お尋ねします。
メインフォームForm1から別スレッドで経過表示用フォームForm2をShowDialog表示する下記のコードで、
書き込み処理中にForm2のクローズボックスをクリックすると、Invokeのところで凍りついてしまいます。
どこに問題があるのでしょうか。
開発環境・使用言語はVB2003/Framework1.1です。

'/*** Form1 (メインフォーム) ***/
Public Class Form1 : Inherits System.Windows.Forms.Form
    '// Friend WithEvents Button1 As System.Windows.Forms.Button
    '// Friend WithEvents Button2 As System.Windows.Forms.Button
    Private fm2 As Form2

    '// Button1.Click : Form2を別スレッドで開く
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If fm2 Is Nothing Then
            Dim t As New System.Threading.Thread(AddressOf Me.ShowForm2)
            t.Start()
        End If
    End Sub

    '// Form2を開くためのプロシージャ
    Private Sub ShowForm2()
        fm2 = New Form2
        fm2.ShowDialog()
        fm2.Dispose()
        fm2 = Nothing
    End Sub

    '// Button2.Click : 経過記録を表示しながら行う繰り返し処理
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.Button2.Enabled = False
        For i As Integer = 1 To 1000
            Me.WriteLine(i.ToString() & "巡目です。")
        Next
        Me.Button2.Enabled = True
    End Sub

    '// Form2に1行表示
    Private Sub WriteLine(ByVal text As String)
        If fm2 Is Nothing Then Return
        Try
            fm2.WriteLine(text)
        Catch ex As System.ObjectDisposedException
        End Try
    End Sub
End Class

'/*** Form2 (経過表示用フォーム) ***/
Public Class Form2 : Inherits System.Windows.Forms.Form
    '// Friend WithEvents RichTextBox1 As System.Windows.Forms.RichTextBox 
    Private Delegate Sub WriteLineDelegate(ByVal text As String)
    Private dlgt As New WriteLineDelegate(AddressOf Me.WriteLine)

    '// リッチテキストボックスに文字を追記表示する
    Public Sub WriteLine(ByVal text As String)
        If Me.IsDisposed OrElse Not Me.IsHandleCreated Then Return
        If Me.InvokeRequired Then
            Me.Invoke(dlgt, New Object() {text})    '// ここで凍りつく
        Else
            With Me.RichTextBox1
                .AppendText(text & vbCrLf)
                .ScrollToCaret()
                .Focus()
            End With
        End If
    End Sub
End Class


なお、Form1.ShowForm2の fm2.Dispose() : fm2 = Nothing を外しても状況は変わりませんでした。

返信 編集キー/


管理者用

- Child Tree -