VB.NET覚書

VB.NETの覚書
[個人メモ] [仕事メモ] [アイデア] [Ruby]
[TIPS] [対応表] [コメント書き方] [.NET Tips] [VB中学校] [VB.NET の入門サイト] [VB.NET コーディング標準 ] [VBレスキュー] [NonSoft]

2019-02-06

デリゲートラムダ式

Public Delegate Function Func1(ByVal msg As String) As Boolean

デリゲート関数の戻りの型と引数の型を定義した関数ポインタ型みたいなもので、その型で定義した変数に、同じ型のラムダ式を代入できる。

デリゲートインスタンス化でき、その時にはnewと既存関数へのAddressOfを使用する

Public Delegate Sub Sub1()

Dim s = New Sub1(AddressOf test)

Public Class Form1

    Public Delegate Function Func1(x As Integer) As Integer
    Public Delegate Sub Sub1()
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

        Dim y As Func1

        Dim inc As Func1 = Function(x) x + 1

        Dim inc2 As Func1 = Function(x)
                                Return x + 2
                            End Function

        y = inc
        MsgBox(y(1))
        y = inc2
        MsgBox(y(1))

           Dim s As Sub1

        s = New Sub1(AddressOf test)

        s()

    End Sub

    Sub test()
        MsgBox("test")
    End Sub

End Class

マルチスレッド

Public Class Form1

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

        Me.BackgroundWorker1.RunWorkerAsync()

    End Sub

    Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        Do
            Label1.Text = Now.ToLongTimeString()
            Threading.Thread.Sleep(1000)
        Loop
    End Sub

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Control.CheckForIllegalCrossThreadCalls = False
    End Sub
End Class


プログレスバーマルチスレッド

Public Class Form1

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        BackgroundWorker1.WorkerReportsProgress = True
        BackgroundWorker1.WorkerSupportsCancellation = True
        BackgroundWorker1.RunWorkerAsync()

    End Sub

    Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork

        For i As Integer = 1 To 100
            Threading.Thread.Sleep(1000)

            If BackgroundWorker1.CancellationPending = True Then
                e.Cancel = True
                Exit For
            End If
            BackgroundWorker1.ReportProgress(i)
        Next
    End Sub

    Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
        ProgressBar1.Value = e.ProgressPercentage
    End Sub

    Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
        MsgBox("終了")
        ProgressBar1.Value = 0
    End Sub

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        BackgroundWorker1.CancelAsync()
    End Sub

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

    End Sub
End Class

マルチスレッドクラス

Imports System.ComponentModel
'http://multithreadingapp.blogspot.com/

Enum ST_NO
    E_IDLE
    E_BUSY
    E_CANCEL
    E_ERR
    E_DONE
    E_ALL_NUM
End Enum
Public Delegate Function Job() As Integer

Public Class clsMultiThread

    Dim bw As BackgroundWorker = New BackgroundWorker
    Dim m_progress As Integer = 0
    Dim m_status_msg As String = ""
    Dim m_status_no As Integer = 0

    Public m_job As Job

    Sub New()
        bw.WorkerSupportsCancellation = True
        bw.WorkerReportsProgress = True

        AddHandler bw.DoWork, AddressOf bw_DoWork
        AddHandler bw.ProgressChanged, AddressOf bw_ProgressChanged
        AddHandler bw.RunWorkerCompleted, AddressOf bw_RunWorkerCompleted

        m_status_msg = "IDLE"
        m_status_no = ST_NO.E_IDLE

    End Sub

    Function getStatusMessage() As String
        Return m_status_msg
    End Function

    Function getStatusNo() As Integer
        Return m_status_no
    End Function

    Private Sub bw_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs)

        Dim worker As BackgroundWorker = CType(sender, BackgroundWorker)
        m_status_msg = "BUSY"
        m_status_no = ST_NO.E_BUSY

        Dim Ret As Integer

        Do While True

            Ret = m_job()

            If bw.CancellationPending = True Then
                e.Cancel = True
                Exit Do
            Else
                ' Perform a time consuming operation and report progress.
                System.Threading.Thread.Sleep(500)
                bw.ReportProgress(Ret)
            End If

        Loop



        For i = 1 To 10
            If bw.CancellationPending = True Then
                e.Cancel = True
                Exit For
            Else
                ' Perform a time consuming operation and report progress.
                System.Threading.Thread.Sleep(500)
                bw.ReportProgress(i * 10)
            End If
        Next
    End Sub

    Private Sub bw_ProgressChanged(ByVal sender As Object, ByVal e As ProgressChangedEventArgs)
        m_progress = e.ProgressPercentage
        'Me.tbProgress.Text = e.ProgressPercentage.ToString() & "%"
    End Sub

    Private Sub bw_RunWorkerCompleted(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs)
        If e.Cancelled = True Then
            m_status_msg = "Canceled!"
            m_status_no = ST_NO.E_CANCEL
            'Me.tbProgress.Text = "Canceled!"
        ElseIf e.Error IsNot Nothing Then
            m_status_msg = "Error: " & e.Error.Message
            m_status_no = ST_NO.E_ERR
            'Me.tbProgress.Text = "Error: " & e.Error.Message
        Else
            m_status_msg = "Done!"
            m_status_no = ST_NO.E_DONE
            'Me.tbProgress.Text = "Done!"
            MsgBox("Done")
        End If
    End Sub

    Public Sub start()
        If Not bw.IsBusy = True Then
            bw.RunWorkerAsync()
        End If
    End Sub

    Public Sub cancel()
        If bw.WorkerSupportsCancellation = True Then
            bw.CancelAsync()
        End If
    End Sub

End Class

トラックバック - http://sub.g.hatena.ne.jp/garyo/20190206