昨晚2:30的时候还没睡着,觉得有必要把vb编写多线程程序再次写一次;主要是以前忽略的细节和重要的环节;今天在公司打开一年多没用的vb,写了如下的代码;想写多线程的朋友可以调试一下看看,关于多线程的任务模式,同步和互斥,临界资源和临界区(文中提到)欢迎跟帖讨论;
'请将该部分数据保存为 FORM1.frm 文件
VERSION 5.00
Begin VB.Form Form1
Caption = "多线程"
ClientHeight = 3195
ClientLeft = 60
ClientTop = 345
ClientWidth = 6450
LinkTopic = "Form1"
ScaleHeight = 3195
ScaleWidth = 6450
StartUpPosition = 3 '窗口缺省
Begin VB.TextBox Text1
Height = 270
Left = 960
TabIndex = 2
Text = "2"
Top = 2760
Width = 2415
End
Begin VB.CommandButton Command2
Caption = "返回"
Height = 255
Left = 3480
TabIndex = 1
Top = 2760
Width = 1455
End
'请将该部分数据保存为 FORM1.frm 文件
VERSION 5.00
Begin VB.Form Form1
Caption = "多线程"
ClientHeight = 3195
ClientLeft = 60
ClientTop = 345
ClientWidth = 6450
LinkTopic = "Form1"
ScaleHeight = 3195
ScaleWidth = 6450
StartUpPosition = 3 '窗口缺省
Begin VB.TextBox Text1
Height = 270
Left = 960
TabIndex = 2
Text = "2"
Top = 2760
Width = 2415
End
Begin VB.CommandButton Command2
Caption = "返回"
Height = 255
Left = 3480
TabIndex = 1
Top = 2760
Width = 1455
End

这篇博客展示了如何在VB中实现多线程,通过CreateThread API创建两个线程进行不同的绘图操作。在主线程中,用户输入会被平方显示。线程通过GetDC、SetBkColor和TextOut等GDI函数在窗体上绘图,以避免非线程安全组件引发的错误。此外,还介绍了线程同步的重要性,虽然示例中并未实际使用临界区。
最低0.47元/天 解锁文章
1万+

被折叠的 条评论
为什么被折叠?



