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

わんくま同盟

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

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


(過去ログ 18 を表示中)
■6864 / )  Re[2]: Control.Invokeが使えない件。
□投稿者/ れい (66回)-(2007/08/25(Sat) 09:34:28)
2007/08/25(Sat) 09:37:52 編集(投稿者)
> テストコードはhttp://bbs.wankuma.com/index.cgi?mode=al2&namber=6760の改造で詳細は後述。

.Net1.1用コードは以下。
.Net2.0でもInherits以外同じ。
なんか怪しい感じだけどツッコミは禁止。


Public Class Form1
	Inherits System.Windows.Forms.Form

	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 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 Me
				ChildFormCreated = False
			End SyncLock
			subthread.Start()
			Me.Text = "Create"
			FormCreateIteration += 1
			InvokeIteration = 0
			While True
				SyncLock Me
					If ChildFormCreated Then Exit Sub
				End SyncLock
			End While
		End If
		InvokeIteration += 1
		Me.Text = FormCreateIteration.ToString & ":" & InvokeIteration.ToString
		Try
			ChildForm.WriteLine(FormCreateIteration & ":" & InvokeIteration.ToString)
		Catch ex As Exception
		End Try
	End Sub

	Private Sub SubThreadProc()
		ChildForm = New Form2
		ChildForm.MainForm = Me
		ChildForm.ShowDialog()
		ChildForm.Dispose()
		ChildForm = Nothing
	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

	Public Sub WriteLine(ByVal text As String)
		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 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)
		SyncLock MainForm
			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

End Class

返信 編集キー/


管理者用

- Child Tree -