vb.net中用task异步进行操作

一、无传入参数,无返回参数

写法1

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

Dim t1 As Task = Task.Run(Sub()
Dim RetS As String = ""
For i As Integer = 0 To 90000
RetS += "iiiiiiiiii"
Next
File.WriteAllText("2.txt", RetS)
End Sub)
Await t1

End Sub

写法2

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

Dim t2 As Task = Task.Run(New Action(AddressOf mWriteText))

Await t2

End Sub

Public Sub mWriteText()

Dim RetS As String = ""

For i As Integer = 0 To 90000

RetS += "iiiiiiiiii"

Next

File.WriteAllText("2.txt", RetS)

End Sub

二、带传入参数,带返回参数

写法1

Private Async Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

Dim t5 = Await WriteAllText(90000)

Me.Text = t5

End Sub

 

Public Async Function WriteAllText(ByVal mCount As Integer) As Task(Of String)

Return Await Task.Run(Function()

Return mWriteText(mCount)

End Function)

End Function

 

Public Function mWriteText(ByVal mCount As Integer) As String

Dim RetS As String = ""

For i As Integer = 0 To mCount

RetS += "iiiiiiiiii"

Next

File.WriteAllText("2.txt", RetS)

Return DateTime.Now.ToString("HH:mm:ss")

End Function

写法2

Private Async Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

Dim t4 As Task(Of String) = Task.Run(Function()

Return mWriteText(90000)

End Function)

Dim t5 As String = Await t4

Me.Text = t5

End Sub

 

Public Function mWriteText(ByVal mCount As Integer) As String

Dim RetS As String = ""

For i As Integer = 0 To mCount

RetS += "iiiiiiiiii"

Next

File.WriteAllText("2.txt", RetS)

Return DateTime.Now.ToString("HH:mm:ss")

End Function

写法3

Private Async Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

Dim t3 As Task(Of String) = Task.Run(Function()

Dim RetS As String = ""

For i As Integer = 0 To 90000

RetS += "iiiiiiiiii"

Next

File.WriteAllText("2.txt", RetS)

Return DateTime.Now.ToString("HH:mm:ss")

End Function)

Dim t5 As String = Await t3

Me.Text = t5

End Sub

三、多任务等待

Private Async Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

Dim tasks As New List(Of Task(Of String))()

Dim t1 As Task(Of String) = WriteAllText(10000)

tasks.Add(t1)

Dim t2 As Task(Of String) = WriteAllText(20000)

tasks.Add(t2)

Dim t3 As Task(Of String) = WriteAllText(30000)

tasks.Add(t3)

 

Dim kk() As String = Await Task.WhenAll(tasks)

Me.Text = kk(0) + "," + kk(1) + "," + kk(2)

 

End Sub

 

Public Async Function WriteAllText(ByVal mCount As Integer) As Task(Of String)

Return Task.Run(Function()

Return mWriteText(mCount)

End Function)

End Function

 

Public Function mWriteText(ByVal mCount As Integer) As String

Dim RetS As String = ""

For i As Integer = 0 To mCount

RetS += "iiiiiiiiii"

Next

File.WriteAllText("2.txt", RetS)

Return DateTime.Now.ToString("HH:mm:ss")

End Function

四、更新UI界面

Task.Run里面的函数是线程函数,直接更新会报错,下面使用IProgress(Of T)来辅助显示进度条。(其实也可以用invoke委托的方式)

Dim progressT As IProgress(Of Integer)

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

progressT = New Progress(Of Integer)(AddressOf ProReport)

End Sub

Public Sub ProReport(ByVal i As Integer)

ProgressBar1.Value = i

End Sub

Private Async Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click

Dim t3 As Task(Of String) = WriteAllText(30000, progressT)

Me.Text = Await t3

End Sub


Public Async Function WriteAllText(ByVal mCount As Integer, mprogressT As IProgress(Of Integer)) As Task(Of String)

Return Await Task.Run(Function()

Return mWriteText(mCount, mprogressT)

End Function)

End Function

 

Public Async Function mWriteText(ByVal mCount As Integer, mprogressT As IProgress(Of Integer)) As Task(Of String)

Dim RetS As String = ""

For i As Integer = 0 To mCount

RetS += "iiiiiiiiii"

Dim Precent As Integer = (i / mCount) * 100

Await Task.Delay(2000)

mprogressT.Report(Precent)

'ProgressBar1.Value = Precent

Next

File.WriteAllText("2.txt", RetS)

Return DateTime.Now.ToString("HH:mm:ss")

End Function

注:在Task.Run中,不建议用sleep函数来延时。建议使用Await Task.Delay(2000)

未经允许不得转载:桔子雨工作室 » vb.net中用task异步进行操作
分享到: 生成海报
一个数字化服务提供商

承接外贸建站,软件APP开发

部分产品联系我们
切换注册

登录

忘记密码 ?

您也可以使用第三方帐号快捷登录

切换登录

注册

微信扫一扫关注
如已关注,请回复“登录”二字获取验证码