'//----------------------------------------------------------------------
'//【非同期メソッドの呼び出し】- EasyInvoke
'//
'//
'//                                                       by Masato Hirai
'//----------------------------------------------------------------------
Imports System
Imports System.Drawing
Imports System.Drawing.Text
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
Imports System.ComponentModel
Imports System.Threading
Imports GeniusHirai.ColorfulControls

Public Class Form1 : Inherits Form

#Region " Windows フォーム デザイナで生成されたコード "

    Public Sub New()
        MyBase.New()

        ' この呼び出しは Windows フォーム デザイナで必要です。
        InitializeComponent()

        ' InitializeComponent() 呼び出しの後に初期化を追加します。

    End Sub

    ' Form は、コンポーネント一覧に後処理を実行するために dispose をオーバーライドします。
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    ' Windows フォーム デザイナで必要です。
    Private components As System.ComponentModel.IContainer

    ' メモ : 以下のプロシージャは、Windows フォーム デザイナで必要です。
    'Windows フォーム デザイナを使って変更してください。  
    ' コード エディタを使って変更しないでください。
  Friend WithEvents ListBox1 As System.Windows.Forms.ListBox
  Friend WithEvents RichLabel1 As GeniusHirai.ColorfulControls.RichLabel
  Friend WithEvents Button1 As GeniusHirai.ColorfulControls.OvalButton
  Friend WithEvents CheckBox1 As GeniusHirai.ColorfulControls.BigCheckBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.ListBox1 = New System.Windows.Forms.ListBox
Me.RichLabel1 = New GeniusHirai.ColorfulControls.RichLabel
Me.Button1 = New GeniusHirai.ColorfulControls.OvalButton
Me.CheckBox1 = New GeniusHirai.ColorfulControls.BigCheckBox
Me.SuspendLayout()
'
'ListBox1
'
Me.ListBox1.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
            Or System.Windows.Forms.AnchorStyles.Left) _
            Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.ListBox1.BackColor = System.Drawing.Color.FromArgb(CType(255, Byte), CType(255, Byte), CType(128, Byte))
Me.ListBox1.Font = New System.Drawing.Font("MS UI Gothic", 36.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(128, Byte))
Me.ListBox1.ItemHeight = 48
Me.ListBox1.Location = New System.Drawing.Point(16, 128)
Me.ListBox1.Name = "ListBox1"
Me.ListBox1.Size = New System.Drawing.Size(456, 388)
Me.ListBox1.TabIndex = 1
'
'RichLabel1
'
Me.RichLabel1.BackColor = System.Drawing.Color.Teal
Me.RichLabel1.Dock = System.Windows.Forms.DockStyle.Top
Me.RichLabel1.Font = New System.Drawing.Font("MS UI Gothic", 27.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(128, Byte))
Me.RichLabel1.ForeColor = System.Drawing.Color.Aqua
Me.RichLabel1.Location = New System.Drawing.Point(0, 0)
Me.RichLabel1.Name = "RichLabel1"
Me.RichLabel1.Size = New System.Drawing.Size(488, 112)
Me.RichLabel1.TabIndex = 2
Me.RichLabel1.Text = "BeginInvoke Demo"
'
'Button1
'
Me.Button1.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.Button1.FaceColor = GeniusHirai.ColorfulControls.OvalButton.FaceColorConstant.Blue
Me.Button1.Font = New System.Drawing.Font("Arial", 24.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Button1.Location = New System.Drawing.Point(312, 16)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(160, 72)
Me.Button1.TabIndex = 3
Me.Button1.Text = "START"
'
'CheckBox1
'
Me.CheckBox1.BackColor = System.Drawing.Color.Teal
Me.CheckBox1.Font = New System.Drawing.Font("MS UI Gothic", 24.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(128, Byte))
Me.CheckBox1.ForeColor = System.Drawing.Color.White
Me.CheckBox1.Location = New System.Drawing.Point(56, 56)
Me.CheckBox1.Name = "CheckBox1"
Me.CheckBox1.Size = New System.Drawing.Size(256, 40)
Me.CheckBox1.TabIndex = 4
Me.CheckBox1.Text = "非同期で実行"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 12)
Me.ClientSize = New System.Drawing.Size(488, 534)
Me.Controls.Add(Me.CheckBox1)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.RichLabel1)
Me.Controls.Add(Me.ListBox1)
Me.Name = "Form1"
Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
Me.Text = "非同期呼び出しデモ"
Me.ResumeLayout(False)

    End Sub

#End Region

'//
'// デリゲートの宣言
'//
Private Delegate Function DoTaskDel(ByVal loops As Integer) As String
Private Delegate Sub AddItemDel(ByVal s As String)
'//
'// [スタート]ボタン
'//
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  Dim s As String
  Me.Cursor = New Cursor(Me.GetType(), "busy_rl.cur")
  Button1.FaceColor = OvalButton.FaceColorConstant.Gray
  Button1.Refresh()
  ListBox1.Items.Clear()
  AddItem("---START---")

  If Not CheckBox1.Checked Then
    '同期
    s = DoTask(5)
    AddItem(s)
  Else
    '非同期
    Dim d As New DoTaskDel(AddressOf DoTask)
    d.BeginInvoke(5, AddressOf TaskComplete, d)
  End If

  Me.Cursor = Cursors.Default
  Button1.FaceColor = OvalButton.FaceColorConstant.Blue
End Sub

'//
'//【DoTask】非同期用メソッド
'//
Private Function DoTask(ByVal loops As Integer) As String
  For k As Integer = 1 To loops
    Thread.Sleep(1000)     '1秒のWAIT
    AddItem(k.ToString)
  Next
  Return "---FINISH---"
End Function

'//
'//【AddItem】リストに項目を追加するメソッド
'//
Private Sub AddItem(ByVal s As String)
  If Me.InvokeRequired Then
    Dim d As New AddItemDel(AddressOf AddItem)
    Me.Invoke(d, New Object() {s})
  Else
    ListBox1.Items.Add(AppDomain.GetCurrentThreadId & " : " & s)
    ListBox1.Refresh()
  End If
End Sub

'//
'//【TaskComplete】非同期完了通知コールバック
'//
Private Sub TaskComplete(ByVal a As IAsyncResult)
  Dim d As DoTaskDel
  d = CType(a.AsyncState, DoTaskDel)
  Dim s As String = d.EndInvoke(a)
  AddItem(s)
End Sub

End Class
