2007/08/28(Tue) 05:50:27 編集(投稿者)
とりあえず。
以下のコードで私の環境では現在33128、まだ動作しています。
FormClosedでも25000以上は動いていてプログラムを終了させました。
Public Class Form1
Inherits System.Windows.Forms.Form
Public Form2SyncObject As New Object
Public ChildForm As Form2
Public InvokeIteration As Integer = 0
Public FormCreateIteration As Integer = 0
Public ChildFormCreated As Boolean
Public Timer1 As System.Windows.Forms.Timer
Private SyncObject As Object
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.components = New System.ComponentModel.Container
Timer1 = New System.Windows.Forms.Timer(Me.components)
Me.components.Add(Timer1)
AddHandler Timer1.Tick, New EventHandler(AddressOf Timer1_Tick)
Me.Timer1.Interval = 10
Me.Timer1.Start()
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs)
If ChildForm Is Nothing Then
Dim subthread As New System.Threading.Thread(AddressOf Me.SubThreadProc)
SyncLock Form2SyncObject
ChildFormCreated = False
End SyncLock
subthread.Start()
Me.Text = "Create"
FormCreateIteration += 1
InvokeIteration = 0
While True
SyncLock Form2SyncObject
If ChildFormCreated Then Exit Sub
End SyncLock
End While
End If
InvokeIteration += 1
Me.Text = FormCreateIteration.ToString & ":" & InvokeIteration.ToString
Try
SyncLock Form2SyncObject
ChildForm.WriteLine(FormCreateIteration & ":" & InvokeIteration.ToString)
End SyncLock
Catch ex As Exception
End Try
End Sub
Private Sub SubThreadProc()
ChildForm = New Form2
ChildForm.MainForm = Me
ChildForm.ShowDialog()
SyncLock Form2SyncObject
ChildForm.Dispose()
ChildForm = Nothing
End SyncLock
End Sub
End Class
Public Class Form2
Inherits System.Windows.Forms.Form
Private Delegate Sub WriteLineDelegate(ByVal text As String)
Public MainForm As Form1
Public TextBox1 As TextBox
Private formClose As Boolean = False
Private formCloseLockObject As New Object
Public Sub WriteLine(ByVal text As String)
Dim lockedFormClose As Boolean
SyncLock formCloseLockObject
lockedFormClose = formClose
End SyncLock
If Not lockedFormClose Then
If Me.InvokeRequired Then
Me.Invoke(New WriteLineDelegate(AddressOf WriteLine), New Object() {text})
Else
Me.Text = text
Me.TextBox1.Text = text
Me.TextBox1.ScrollToCaret()
Me.TextBox1.Focus()
End If
End If
End Sub
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.components = New System.ComponentModel.Container
Dim Timer1 As New System.Windows.Forms.Timer(Me.components)
TextBox1 = New TextBox
Me.components.Add(Timer1)
Me.Controls.Add(TextBox1)
AddHandler Timer1.Tick, New EventHandler(AddressOf Timer1_Tick)
AddHandler Me.FormClosed, New FormClosedEventHandler(AddressOf Form2_FormClosed)
AddHandler Me.FormClosing, New FormClosingEventHandler(AddressOf Form2_FormClosing)
SyncLock MainForm.Form2SyncObject
MainForm.ChildFormCreated = True
End SyncLock
Timer1.Interval = New Random().Next(100, 1000)
Timer1.Start()
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs)
Me.Close()
End Sub
Private Sub Form2_FormClosed(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs)
'formClose = True
End Sub
Private Sub Form2_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs)
SyncLock formCloseLockObject
formClose = True
End SyncLock
End Sub
End Class