转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 软件开发 >> VB.NET程序 >> 正文
A Thread to Visual Basic         ★★★★

A Thread to Visual Basic

作者:闵涛 文章来源:闵涛的学习笔记 点击数:5507 更新时间:2009/4/23 16:39:28
 

 

 

B

 

C

E

Or it could be:

Thread 1

A

B

C

D

 

 

E

 

Thread 2

 

 

 

 

B

C

 

E

or perhaps:

Thread 1

A

B

C

D

 

 

 

E

Thread 2

 

 

 

 

B

C

E

 

In other words, when you start a new thread of execution in an application, you can never know the exact order in which instructions in the two threads will execute relative to each other. The two threads are completely independent.

Why is this a problem?

A Multithreading Simulator

Consider the MTDemo project:

(You can download the sample code from ftp.desaware.com/SampleCode/Articles/Thread.zip)

The project contains a single code module that contains two global variables as follows:

'''' MTDemo - Multithreading Demo program
'''' Copyright © 1997 by Desaware Inc. All Rights Reserved
Option Explicit
Public GenericGlobalCounter As Long
Public TotalIncrements As Long

It contains a single form named frmMTDemo1 which contains the following code:

'''' MTDemo - Multithreading Demo program
'''' Copyright © 1997 by Desaware Inc. All Rights Reserved
Option Explicit
Dim State As Integer
	'''' State = 0 - Idle
	'''' State = 1 - Loading existing value
	'''' State = 2 - Adding 1 to existing value
	'''' State = 3 - Storing existing value
	'''' State = 4 - Extra delay	
Dim Accumulator As Long
Const OtherCodeDelay = 10
Private Sub Command1_Click()
	Dim f As New frmMTDemo1
	f.Show
End Sub
Private Sub Form_Load()
	Timer1.Interval = 750 + Rnd * 500
End Sub
Private Sub Timer1_Timer()
	Static otherdelay&
	Select Case State
		Case 0
			lblOperation = "Idle"
			State = 1
		Case 1
			lblOperation = "Loading Acc"
			Accumulator = GenericGlobalCounter
			State = 2
		Case 2
			lblOperation = "Incrementing"
			Accumulator = Accumulator + 1
			State = 3
		Case 3
			lblOperation = "Storing"
			GenericGlobalCounter = Accumulator
			TotalIncrements = TotalIncrements + 1
			State = 4
		Case 4
			lblOperation = "Generic Code"
			If otherdelay >= OtherCodeDelay Then
				State = 0
				otherdelay = 0
			Else
				otherdelay = otherdelay + 1
			End If
		End Select
	UpdateDisplay
End Sub
Public Sub UpdateDisplay()
	lblGlobalCounter = Str$(GenericGlobalCounter)
	lblAccumulator = Str$(Accumulator)
	lblVerification = Str$(TotalIncrements)
End Sub

This program uses a timer and a simple state machine to simulate multithreading. The State variable describes the five instructions that this program executes in order. State zero is an idle state. State one loads local variable with the GenericGlobalCounter global variable. State two increments the local variable. State three stores the result into the GenericGlobalCounter variable and increments the TotalIncrements variable (which counts the number of times that the GenericGlobalCounter variable has been incremented). State 4 adds an additional delay representing time spent running other instructions in the program.

The UpdateDisplay function updates three labels on the form that show the current value of the GenericGlobalCounter variable, the local accumulator, and the total number of increments.

Each timer tick represents a CPU cycle on the current thread. If you run the program you''''ll see that the value of the GenericGlobalCounter variable will always be exactly equal to the TotalIncrements variable -- which makes sense, because the TotalIncrements variable shows the number of times the thread has incremented the GenericGlobalCounter.

But what happens when you click the Command1 button and start a second instance of the form? This new form simulates a second thread.

Every now and then, the instructions will line up in such a way that both forms load the same GenericGlobalCounter value, increment it, and store it. As a result, the value will only increase by one, even though each thread believed that it had independently incremented the variable. In other words -- the variable was incremented twice, but the value only increased by one. If you launch several forms you will quickly see that the number of increments as represented by the TotalIncrements variable grows much more rapidly than the GenericGlobalCounter variable.

What if the variable represents an object lock count - which keeps track of when an object should be freed? What if it represents a signal that indicates that a resource is in use?

This type of problem can lead to resources becoming permanently unavailable to the system, to object being locked internally in memory, or freed prematurely. It can easily lead to application crashes.

This example was designed to make the problem easy to see -- but try experimenting with the value of the OtherCodeDelay variable. When the dangerous code is relatively small compared to the entire program, problems will appear less frequently. While this may sound g

上一页  [1] [2] [3] [4] [5] [6] [7]  下一页


[办公软件]在Excel中插入时间/日期TODAY和NOW函数  [网络安全]激活型触发式AutoRun.inf病毒和rose病毒的清除方案
[Web开发]asp.net c#其它语句之break、continue、goto实例介…  [Web开发]一大堆常用的超强的正则表达式及RegularExpressio…
[平面设计]制作动态GIF图像的好帮手─Coffee GIf Animator  [平面设计]功能超强的GIF动画制作软件─Ulead Gif Animator
[平面设计]AutoCAD常用快捷命令(外加中文说明)  [平面设计]AutoCAD常用快捷命令(字母类,外加中文说明)
[平面设计]AutoCAD快捷命令介绍  [平面设计]多种方法解决AutoCAD打印时出现错误
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · C语言系列  · VB.NET程序
    · JAVA开发  · Delphi程序
    · 脚本语言
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台