我是靠谱客的博主 跳跃未来,最近开发中收集的这篇文章主要介绍VB.Net 使用Thread和DoEvent 进行并发处理,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

进度条控制问题:
一. BackgroundWorker 组件 和Thread

许多经常执行的操作可能需要很长的执行时间。

类似这样的操作可能导致用户界面在操作运行时挂起。如果您需要用户界面的响应却遇到与此类操作关联的长时间延迟,BackgroundWorker组件可以提供一种方便的解决方案。
使用 BackgroundWorker 组件,您可以在不同于应用程序的主用户界面线程的另一线程上异步(“在后台”)执行耗时的操作。若要使用 BackgroundWorker,只需要告诉该组件要在后台执行的耗时的辅助方法,然后调用 RunWorkerAsync 方法。在辅助方法以异步方式运行的同时,您的调用线程继续正常运行。该方法运行完毕,BackgroundWorker 激发 RunWorkerCompleted 事件(可选择包含操作结果)向调用线程发出警报。

“组件”选项卡的“工具箱”中提供了 BackgroundWorker 组件。若要向窗体添加 BackgroundWorker,请将 BackgroundWorker 组件拖到窗体上。该组件出现在组件栏中,该组件的属性出现在“属性”窗口中。

若要启动异步操作,请使用 RunWorkerAsync 方法。RunWorkerAsync 采用一个可选的 object 参数,可以使用该参数将变量传递给辅助方法。BackgroundWorker 类公开 DoWork 事件,您的辅助线程通过 DoWork 事件处理程序附加到该事件。

DoWork 事件处理程序采用一个 DoWorkEventArgs 参数,该参数具有 Argument 属性。此属性接收来自 RunWorkerAsync 的参数,并可以传递至 DoWork 事件处理程序中调用的辅助方法。下面的示例演示如何分配名为 ComputeFibonacci 的辅助方法的结果。 

★★★★★★★★例子不太好,只是简单的使用★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

Option Explicit On      '変数の、明示的な宣言を強制します。
Option Strict On        '変数の、明示的な型宣言を強制します。

Imports Tocera.App.UI.Common
Imports System.Windows.Forms
Imports System.configuration
'
' 共通ダイアログ
'
Public Class CmnDialog              '共通ダイアログ画面

    'Private mResults As New List(Of Integer)
    Dim th As New Threading.Thread(AddressOf execSearch)
    Dim resultData As Hashtable = New Hashtable
    Private _GUIParam As GUIParam                           ''画面間パラメタ
    Public Property GUIParameter() As GUIParam
        Get
            Return Me._GUIParam
        End Get
        Set(ByVal value As GUIParam)
            Me._GUIParam = value
        End Set
    End Property

    Private _DialogParam As CmnDialogParam                  ''共通ダイアログパラメタ
    Public ReadOnly Property DialogParameter() As CmnDialogParam
        Get
            Return CType(Me._GUIParam.Param, CmnDialogParam)
        End Get
    End Property
    Private service As CmnDialogService                 ''共通ダイアログサービスクラス
    Private formBean As CmnDialogFormBean               ''共通ダイアログBeanクラス
    Private AppConf As AppConfiguration = Nothing       ''App.Conig
    Private cmnDlgMode As CmnDialogMode = Nothing       ''ダイアログモードクラス
    Private Log As Logger = Nothing                     ''ロガー
    Private MsgMng As MessageMng = Nothing              ''メッセージマネジャー

    '
    ' 機能      : コンストラクタ
    '
    ' 引き数    : なし
    '
    ' 返り値    : なし
    '
    ' 機能説明  : コンスラクタ
    '
    ' 備考      : なし
    '
    Sub New()

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

        ' InitializeComponent() 呼び出しの後で初期化を追加します。
      
        AppConf = New AppConfiguration


        Log = LoggerBuilder.GetLogger

        MsgMng = MessageBuilder.GetMessageMng
       
    End Sub

    '
    ' 機能      : Form_Load
    '
    ' 引き数    : VS2005のデフォルト値
    '
    ' 返り値    : VS2005のデフォルト値
    '
    ' 機能説明  : Form_Load
    '
    ' 備考      : なし
    '
    Private Sub CmnDialogForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
        'Me.Cursor = Cursors.WaitCursor

        If (Me._GUIParam Is Nothing) Then
            Throw New Exception("画面間パラメタが設定されていません。")
        End If

        Me.ControlBox = False
        Me.FormBorderStyle = Windows.Forms.FormBorderStyle.FixedSingle

        service = New CmnDialogService()
        formBean = New CmnDialogFormBean

        Me.btCancel.Enabled = True

        InitObject()

        cmnDlgMode = New CmnDialogMode(Me.DialogParameter.DialogMode)
        formBean.machineCode = Me.DialogParameter.machineCode

        '画面表示位置設定
        Util.SetCenterScreen(Me)

        ProgressBar1.Value = 0

        BackgroundWorker1.RunWorkerAsync()
        'execSearch()
    End Sub
    Private Sub InitObject()

    End Sub
    Private Sub execSearch()

        InitSelectList()
        'Me.BackgroundWorker1.ReportProgress(0)
        'Dim resultData As Hashtable = New Hashtable

        ' ******************************************************************************
        ' 検索処理の実行
        ' ******************************************************************************
        formBean = service.SearchTable(GUIParameter, formBean)

        If formBean.TableList.Tables(0).Rows.Count < 1 Then

            Dim msg As String = MsgMng.GetMessage("E90010")
            Throw New AppException("E90010", msg)

            Exit Sub
        End If

        formBean = service.SearchData(GUIParameter, formBean)


       
    End Sub
    Private Sub InitSelectList()
        Try
            ' データソースの初期化
            Me.SelectList.DataSource = Nothing
            Me.SelectList.Columns.Clear()

            ' 最下行に空行を表示しない。
            Me.SelectList.AllowUserToAddRows = False

            '行のサイズ変更不可
            Me.SelectList.AllowUserToResizeRows = False

            '行ヘッダを表示しない
            Me.SelectList.RowHeadersVisible = False

            '行全体を選択状態にする。
            Me.SelectList.SelectionMode = DataGridViewSelectionMode.FullRowSelect

        Catch aex As AppException
            'メッセージ表示
            Util.AppMsgBox(aex.ErrCode & " " & aex.Message, MsgBoxStyle.Exclamation)

        Catch ex As Exception
            'ログ出力
            Log.Err(ex)

            'メッセージ表示
            Dim msg As String = MsgMng.GetMessage("E99999")
            Util.AppMsgBox("E99999" & " " & msg & ControlChars.CrLf & ex.Message, MsgBoxStyle.Exclamation)
        End Try
    End Sub

    Private Sub btCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btCancel.Click
        Log.Trace("START>>" & Me.GetType.FullName & " " & System.Reflection.MethodBase.GetCurrentMethod.Name)
        Try

            BackgroundWorker1.CancelAsync()

            Me.Cursor = Cursors.WaitCursor

            Me.DialogResult = Windows.Forms.DialogResult.Cancel

            Me.formBean.SelectList.Dispose()
            Me.service = Nothing
            Me.formBean = Nothing

            Me.Dispose()

            Me.Close()

        Catch ex As Exception

        Finally
            Me.Cursor = Cursors.Default

        End Try
        Log.Trace("END<<" & Me.GetType.FullName & " " & System.Reflection.MethodBase.GetCurrentMethod.Name)
    End Sub

    Private Sub BackgroundWorker1_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        'マルチスレッドのスタート
        th.Start()

        Dim num As Integer
        For num = 1 To 10000
            num = num + 2

            If Not th.IsAlive Then
                num = 10000

            End If

            If num > 9000 And th.IsAlive Then
                th.Join()
            End If

            Threading.Thread.Sleep(2)
            Me.BackgroundWorker1.ReportProgress(CInt(num / 100))

            If Me.BackgroundWorker1.CancellationPending Then
                Exit Sub
            End If
        Next

    End Sub

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

    Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
        Dim datSet As DataSet = formBean.SelectList
        Dim i As Integer = 0
        Dim datcolumn As DataColumn

        Dim title(2) As String
        title(0) = "テーブル名所"
        title(1) = "件数"

        For i = 0 To datSet.Tables(0).Columns.Count - 1
            datcolumn = datSet.Tables(0).Columns(i)
            Me.SelectList.Columns.Add(datcolumn.Caption, title(i))
            Me.SelectList.Columns(i).DataPropertyName = datcolumn.Caption
        Next

        Me.SelectList.DataSource = formBean.SelectList
        Me.SelectList.DataMember = "TABLE"

        With Me.SelectList
            .Columns(0).Width = 300
            .Columns(1).Width = .Width - .Columns(0).Width
        End With

    End Sub
End Class

二.使用DoEvent
★★★★★★★★例子不太好,只是简单的使用★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

Option Explicit On      '変数の、明示的な宣言を強制します。
Option Strict On        '変数の、明示的な型宣言を強制します。

Imports Tocera.App.UI.Common
Imports System.Windows.Forms
Imports System.configuration
'
' 共通ダイアログ
'
Public Class CmnDialog              '共通ダイアログ画面

    Private _GUIParam As GUIParam                           ''画面間パラメタ
    Public Property GUIParameter() As GUIParam
        Get
            Return Me._GUIParam
        End Get
        Set(ByVal value As GUIParam)
            Me._GUIParam = value
        End Set
    End Property

    Private _DialogParam As CmnDialogParam                  ''共通ダイアログパラメタ
    Public ReadOnly Property DialogParameter() As CmnDialogParam
        Get
            Return CType(Me._GUIParam.Param, CmnDialogParam)
        End Get
    End Property
    Private service As CmnDialogService                 ''共通ダイアログサービスクラス
    Private formBean As CmnDialogFormBean               ''共通ダイアログBeanクラス
    Private AppConf As AppConfiguration = Nothing       ''App.Conig
    Private cmnDlgMode As CmnDialogMode = Nothing       ''ダイアログモードクラス
    Private Log As Logger = Nothing                     ''ロガー
    Private MsgMng As MessageMng = Nothing              ''メッセージマネジャー

    '
    ' 機能      : コンストラクタ
    '
    ' 引き数    : なし
    '
    ' 返り値    : なし
    '
    ' 機能説明  : コンスラクタ
    '
    ' 備考      : なし
    '
    Sub New()

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

        ' InitializeComponent() 呼び出しの後で初期化を追加します。
      
        AppConf = New AppConfiguration


        Log = LoggerBuilder.GetLogger

        MsgMng = MessageBuilder.GetMessageMng
       
    End Sub

    '
    ' 機能      : Form_Load
    '
    ' 引き数    : VS2005のデフォルト値
    '
    ' 返り値    : VS2005のデフォルト値
    '
    ' 機能説明  : Form_Load
    '
    ' 備考      : なし
    '
    Private Sub CmnDialogForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
        'Me.Cursor = Cursors.WaitCursor

        If (Me._GUIParam Is Nothing) Then
            Throw New Exception("画面間パラメタが設定されていません。")
        End If

        Me.ControlBox = False
        Me.FormBorderStyle = Windows.Forms.FormBorderStyle.FixedSingle

        service = New CmnDialogService()
        formBean = New CmnDialogFormBean

        Me.btCancel.Enabled = True

        InitObject()

        cmnDlgMode = New CmnDialogMode(Me.DialogParameter.DialogMode)
        formBean.machineCode = Me.DialogParameter.machineCode

        '画面表示位置設定
        Util.SetCenterScreen(Me)


        Application.DoEvents()

        execSearch()

    End Sub
    Private Sub InitObject()

    End Sub
    Private Sub execSearch()

        InitSelectList()

        Dim resultData As Hashtable = New Hashtable

        ' ******************************************************************************
        ' 検索処理の実行
        ' ******************************************************************************
        formBean = service.SearchTable(GUIParameter, formBean)

        If formBean.TableList.Tables(0).Rows.Count < 1 Then

            Dim msg As String = MsgMng.GetMessage("E90010")
            Throw New AppException("E90010", msg)

            Exit Sub
        End If

        formBean = service.SearchData(GUIParameter, formBean)
        '======================>>>>
        ProgressBar1.Value = 20
        Application.DoEvents()

        Dim datSet As DataSet = formBean.SelectList
        Dim i As Integer = 0
        Dim datcolumn As DataColumn

        Dim title(2) As String
        title(0) = "テーブル名所"
        title(1) = "件数"

        For i = 0 To datSet.Tables(0).Columns.Count - 1
            datcolumn = datSet.Tables(0).Columns(i)
            Me.SelectList.Columns.Add(datcolumn.Caption, title(i))
            Me.SelectList.Columns(i).DataPropertyName = datcolumn.Caption
        Next

        Me.SelectList.DataSource = formBean.SelectList
        Me.SelectList.DataMember = "TABLE"

        '======================>>>>
        ProgressBar1.Value = 90
        Application.DoEvents()

        With Me.SelectList
            .Columns(0).Width = 300
            .Columns(1).Width = .Width - .Columns(0).Width
        End With

        GUIParameter.Result = resultData

        '======================>>>>
        ProgressBar1.Value = 100
        Application.DoEvents()

    End Sub
    Private Sub InitSelectList()
        Try
            ' データソースの初期化
            Me.SelectList.DataSource = Nothing
            Me.SelectList.Columns.Clear()

            ' 最下行に空行を表示しない。
            Me.SelectList.AllowUserToAddRows = False

            '行のサイズ変更不可
            Me.SelectList.AllowUserToResizeRows = False

            '行ヘッダを表示しない
            Me.SelectList.RowHeadersVisible = False

            '行全体を選択状態にする。
            Me.SelectList.SelectionMode = DataGridViewSelectionMode.FullRowSelect

        Catch aex As AppException
            'メッセージ表示
            Util.AppMsgBox(aex.ErrCode & " " & aex.Message, MsgBoxStyle.Exclamation)

        Catch ex As Exception
            'ログ出力
            Log.Err(ex)

            'メッセージ表示
            Dim msg As String = MsgMng.GetMessage("E99999")
            Util.AppMsgBox("E99999" & " " & msg & ControlChars.CrLf & ex.Message, MsgBoxStyle.Exclamation)
        End Try
    End Sub

    Private Sub btCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btCancel.Click
        Log.Trace("START>>" & Me.GetType.FullName & " " & System.Reflection.MethodBase.GetCurrentMethod.Name)
        Try
            Me.Cursor = Cursors.WaitCursor

            Me.DialogResult = Windows.Forms.DialogResult.Cancel

            Me.formBean.SelectList.Dispose()
            Me.service = Nothing
            Me.formBean = Nothing

            Me.Dispose()

            Me.Close()

        Catch ex As Exception

        Finally
            Me.Cursor = Cursors.Default

        End Try
        Log.Trace("END<<" & Me.GetType.FullName & " " & System.Reflection.MethodBase.GetCurrentMethod.Name)
    End Sub
End Class

最后

以上就是跳跃未来为你收集整理的VB.Net 使用Thread和DoEvent 进行并发处理的全部内容,希望文章能够帮你解决VB.Net 使用Thread和DoEvent 进行并发处理所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(54)

评论列表共有 0 条评论

立即
投稿
返回
顶部