VB.NET线程学习

前端之家收集整理的这篇文章主要介绍了VB.NET线程学习前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
Imports System.Threading
Public Class Form1
    ''''''''''''''''''''''''''''''''''''''''''''''
    '' 下面的代码是使用多线程同时弹出4个msgBox''''
    ''''''''''''''''''''''''''''''''''''''''''''''
    ''''''''''定义'''''
    Structure MyParameters
        Dim nNum As Integer
    End Structure
    Public paramPM(3) As MyParameters

    ''' <summary>
    ''' 线程池共用事件
    ''' </summary>
    ''' <param name="paramPM"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function sPM2(ByVal paramPM As MyParameters) As String
        MessageBox.Show("Hello" + paramPM.nNum.ToString)
        Return ""
    End Function
    ''' <summary>
    ''' 按钮单击事件
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Button1.Click
        ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf sPM2),paramPM(0))
        ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf sPM2),paramPM(1))
        ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf sPM2),paramPM(2))
        ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf sPM2),paramPM(3))
    End Sub

    ''' <summary>
    ''' 窗口登陆事件
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub Form1_Load(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles MyBase.Load
        initpm() ' 初始化paramPM(3)
    End Sub

    ''' <summary>
    ''' 初始化
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub initpm()
        paramPM(0).nNum = 0
        paramPM(1).nNum = 1
        paramPM(2).nNum = 2
        paramPM(3).nNum = 3
    End Sub
End Class


上面的代码是通过点击一个按钮,同时弹出四个对话框

Imports System.Threading
Public Class Form1

    '' 计算平方
    Function Square(ByVal Value As Double)
        Return Value * Value
    End Function

    '' 将方法封装到类中
    Public Class SqueareClass
        Public Value As Double
        Public Square As Double
        'Public Sub CalcSquare(ByVal Value As Integer)
        Public Sub CalcSquare()
            Square = Value * Value
        End Sub
    End Class

    Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Button1.Click
        Dim oSquare As New SqueareClass()
        Dim t As Thread
        t = New Thread(AddressOf oSquare.CalcSquare)
        oSquare.Value = 30
        t.Start()
        If t.Join(500) Then
            MsgBox(oSquare.Square)
        End If
    End Sub
End Class


上面的代码是通过将线程执行函数封装到类中,并通过join同步线程,获取方法的执行结果

原文链接:https://www.f2er.com/vb/258649.html

猜你在找的VB相关文章