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

A Thread to Visual Basic

作者:闵涛 文章来源:闵涛的学习笔记 点击数:5501 更新时间:2009/4/23 16:39:28
ood, the opposite is true. Multithreading problems can be extremely intermittent and difficult to find. This means that multithreading demands careful design up front.

Avoiding Multithreading Problems

There are two relatively easy ways to avoid multithreading problems.

  1. Avoid all use of global variables.

  2. Add synchronization code wherever global variables are used.

The first approach is the one used by Visual Basic. When you turn on multithreading in a Visual Basic applications, all global variables become local to a specific thread. This is inherent in the way Visual Basic implements apartment model threading -- more on this later.

The original release of Visual Basic 5.0 only allowed multithreading in components that had no user interface elements. This was because they had not figured out at the time a way to make the forms engine thread safe. For example: when you create a form in Visual Basic, VB gives it an implied global variable name (thus if you have a form named Form1, you can directly access its methods using Form1.method instead of declaring a separate form variable). This type of global variable can cause the kinds of multithreading problems you saw earlier. There were undoubtedly other problems within the forms engine as well -- making a package that complex safe for multithreading can be quite a challenge.

With service pack 2, Visual Basic''''s forms engine was made thread safe. One sign of this is that each thread has its own implied global variable for each form defined in the project.

New for Service Pack 2

By making the forms engine thread safe, Service pack 2 made it possible for you to create multithreading client applications using Visual Basic. This is demonstrated in the MTDemo2 project:

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

The application must be defined as an ActiveX Exe program with startup set to Sub Main in a code module as follows:

'''' MTDemo2 - Multithreading demo program
'''' Copyright © 1997 by Desaware Inc. All Rights Reserved
Option Explicit
Declare Function FindWindow Lib "user32" Alias "FindWindowA" 
  (ByVal lpClassName As String, _
  ByVal lpWindowName As String) As Long
Sub Main()
	Dim f As frmMTDemo2
	'''' We need this because Main is called on each new thread
	Dim hwnd As Long
	hwnd = FindWindow(vbNullString, "Multithreading Demo2")
	If hwnd = 0 Then
		Set f = New frmMTDemo2
		f.Show
		Set f = Nothing
	End If
End Sub

The first time through, the program loads and displays the main form of the application. The Main routine needs some way of finding out whether this is the first thread of the application because it is executed at the start of every thread. You can''''t use a global variable to find this out because the Visual Basic apartment model keeps global variables specific to a single thread. In this example the FindWindow API function is used to check if the main form of the example has been loaded. There are other ways to find out if this is the main thread, including use of system synchronization objects - but this too is a subject for another time and place.

Multithreading is accomplished by creating an object in a new thread. The object must be defined using a class module. In this case, a simple class module is defined as follows:

'''' MTDemo2 - Multithreading demo program
'''' Copyright © 1997 by Desaware Inc. All Rights Reserved
Option Explicit
Private Sub Class_Initialize()
	Dim f As New frmMTDemo2
	f.Show
	Set f = Nothing
End Sub
We can set the form variable to nothing after it is created because the act of 
showing the form will keep it loaded.
'''' MTDemo2 - Multithreading demo program
'''' Copyright © 1997 by Desaware Inc. All Rights Reserved
Option Explicit
Private Sub cmdLaunch1_Click()
	Dim c As New clsMTDemo2
	c.DisplayObjPtr Nothing
End Sub
Private Sub cmdLaunch2_Click()
	Dim c As clsMTDemo2
	Set c = CreateObject("MTDemo2.clsMTDemo2")
End Sub
Private Sub Form_Load()
	lblThread.Caption = Str$(App.ThreadID)
End Sub

The form displays its thread identifier in a label on the form. The form contains two launch buttons, one that uses the New operator, the other that uses the CreateObject operator.

If you run the program within the Visual Basic environment, you''''ll see that the forms are always created in the same thread. This is because the Visual Basic environment only supports a single thread. If you compile the program, you''''ll see that the CreateObject approach creates both the clsMTDemo2 and its form in a new thread.

Why Multithread?

Why all the fuss about multithreading if there is so much potential danger involved? Because, in certain situations, multithreading can dramatically improve performance. In some cases it can improve the efficiency of certain synchronization operations such as waiting for an application to terminate. It allows more flexibility in application architecture. For example, Add a long operation to the form in the MTDemo2 application with code such as this:

Private Sub cmdLongOp_Click()
Dim l&
Dim s$
For l = 1 To 1000000
s = Chr$(l And &H7F)
Next l
End Sub

Launch several instances of the form using the cmdLaunch1 button. When you click on the cmdLongOp button on any of the forms, you will see that it freezes up operations on all of the other forms. This is because all of the forms are running on a single thread -- and that thread is busy running the long loop. If you reproduce this using the cmdLaunch2 button (with a compiled executable) and click the cmdLongOp button on a form, only that form will be frozen -- the other forms will continue to be active. They are running in their own execution thread, and the long loop operation only ties up its own thread. Of course, you probably shouldn''''t be placing these kinds of long operations in your forms in any case.

Here is a brief summary of when multithreading has value:

ActiveX EXE Server -- no shared resources.

When you have an ActiveX EXE server that you expect to share among applications, multithreading prevents the applications from interfering with each other. If one application performs a long operation on an object in a single threaded server, the other applications are frozen out waiting for the server to become available. Multithreading avoids this problem. However, there are cases where you may want to use an ActiveX EXE server to arbitrate access to a shared resource. An example of this is the stock quote server described in my Developing ActiveX Components book. In this case the single thread runs the stock quote server which is shared among all of the applications using the server in turn.

Multithreading Client -- Implemented as an ActiveX EXE Server

A simple form of this approach is demonstrated in the MTDemo2 application. It is used when an application supports multiple windows that must exit withi

上一页  [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……
    咸宁网络警察报警平台