-
vb.net的异步读写数据流(使用线程、委托)
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim i As Int32
For i = 0 To 123451
TextBox1.Text = i
Next
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
TextBox2.Text = "终于出现奇迹"
End Sub
End Class
Imports System.Threading
Public Class Form1
Dim mythread As Thread
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
mythread = New Thread(AddressOf ShowNumber) '构造线程
mythread.Name = "myShowNumber"
mythread.Start() '启动线程
End Sub
Private Sub ShowNumber()
Dim i As Int32
For i = 0 To 123451
TextBox1.Text = i
Next
mythread.Abort() '终止线程
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
TextBox2.Text = "终于出现奇迹"
End Sub
End Class
然而一点击,发现出错,提示:线程间操作无效: 从不是创建控件“TextBox1”的线程访问它。
Imports System.Threading
Public Class Form1
Dim mythread As Thread
Private Delegate Sub VoidShow(ByRef i As Int32) '定义要委托的类型
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
mythread = New Thread(AddressOf ShowNumber)
mythread.Name = "myShowNumber"
mythread.Start()
End Sub
Private Sub ShowNumber()
Dim i As Int32
For i = 0 To 123451
'TextBox1.Text = i
Me.Invoke(New VoidShow(AddressOf TureShowNumber), i) '用New构造委托,再用Invoke执行
Next
mythread.Abort()
End Sub
'新加入的被委托要做的事
Private Sub TureShowNumber(ByRef i As Int32)
TextBox1.Text = i
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
TextBox2.Text = "终于出现奇迹"
End Sub
End Class
Public Sub New ( _
path As String, _
mode As FileMode, _
access As FileAccess, _
share As FileShare, _
bufferSize As Integer, _ '缓冲大小
useAsync As Boolean _ 'True为异步
)
Imports System.IO
Imports System.Threading
Public Class Form1
Dim btArray(15) As Byte
Dim fs As FileStream
Dim myThread As Thread
Dim blnProcess As Boolean '进程是否使用标志
Private Delegate Sub ShowMyMessage(ByVal str As String) '线程中无法调用窗体控件,用委托解决
'启动写或读进程
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
TextBox1.Text = ""
Try
If RadioButton2.Checked = True Then '写选中
myThread = New Thread(AddressOf WriteData)
myThread.Name = "WriteBulkData"
myThread.Start()
Else
myThread = New Thread(AddressOf ReadData)
myThread.Name = "ReadBulkData"
myThread.Start()
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub WriteData()
Try
fs = New FileStream("D:\11.txt", FileMode.Open, FileAccess.Write, FileShare.Write, 16, True)
Dim myWCB As New AsyncCallback(AddressOf MyAsyncWriteCallBack)
blnProcess = True
fs.BeginWrite(btArray, 0, 12, myWCB, Nothing)
ProcessMessage("Write")
fs.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub ReadData()
Try
fs = New FileStream("d:\11.txt", FileMode.Open, FileAccess.Read, FileShare.Read, 16, True)
Dim myRCB As New AsyncCallback(AddressOf MyAsyncReadCallBack)
blnProcess = True
fs.BeginRead(btArray, 0, 16, myRCB, Nothing)
ProcessMessage("Read")
fs.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub MyAsyncWriteCallBack(ByVal myIar As IAsyncResult)
Thread.Sleep(50)
blnProcess = False
fs.EndWrite(myIar)
'委托显示信息
Dim str As String = " 异步线程数据写入完成。"
Me.Invoke(New ShowMyMessage(AddressOf ShowMessage), str)
End Sub
Private Sub MyAsyncReadCallBack(ByVal myIar As IAsyncResult)
Thread.Sleep(50)
blnProcess = False
fs.EndRead(myIar)
'委托显示信息
Dim str As String = " 异步线程数据读取完成。"
Me.Invoke(New ShowMyMessage(AddressOf ShowMessage), str)
End Sub
Private Sub ShowMessage(ByVal str As String)
TextBox1.Text &= Now.ToString & str & vbCrLf
End Sub
Private Sub ProcessMessage(ByVal strRW As String)
Dim strMessage As String = ""
If strRW = "Read" Then
strMessage = " 判断异步正在读取..."
Else
strMessage = " 判断异步正在写入..."
End If
Do While blnProcess = True
Me.Invoke(New ShowMyMessage(AddressOf ShowMessage), strMessage)
Loop
Thread.Sleep(50)
strMessage = " 判断读写已经完成。"
Me.Invoke(New ShowMyMessage(AddressOf ShowMessage), strMessage)
End Sub
End Class
栏目列表
最新更新
python爬虫及其可视化
使用python爬取豆瓣电影短评评论内容
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
SQL SERVER中递归
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
uniapp/H5 获取手机桌面壁纸 (静态壁纸)
[前端] DNS解析与优化
为什么在js中需要添加addEventListener()?
JS模块化系统
js通过Object.defineProperty() 定义和控制对象
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比