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

A Thread to Visual Basic

作者:闵涛 文章来源:闵涛的学习笔记 点击数:5506 更新时间:2009/4/23 16:39:28
n a single application but work completely independently. Internet browsers are a good example of multithreaded clients, where each browser window runs in its own thread. Note that multithreading should not be used as a substitute for good event driven design.

Multithreading DLL

A multithreading DLL does not actually create its own threads. It is simply a DLL that creates objects that run in the same thread that requests the objects. For example: a multithreaded ActiveX control (which is a DLL) creates controls that run in the same thread as the form that contains the control. This can improve efficiency on a multithreaded client such as an Internet browser.

Multithreaded Servers DLL or EXE

In a client server architecture, multithreading can improve performance if you have a mix of long and short client requests. Be careful though -- if all of your client requests are of similar length, multithreading can actually slow down the server''''s average response time! Never assume that the fact that your server is multithreading will necessarily improve performance.

The Threading Contract

Believe it or not, all of this has been in the way of introduction. Some of the material reviews information that is covered in far greater depth in my Developing ActiveX Components book, other material describes new information for service pack 2.

Now, allow me to ask a question that goes directly to the heart of multithreading using COM (the component object model on which all Visual Basic objects, and those in other windows applications that use OLE are based).

Given:

That multithreading is potentially extremely dangerous and specifically -- that attempting to multithread code that is not designed to support multithreading is likely to cause fatal errors and system crashes.

Question:

How is it possible that Visual Basic allows you to create objects and use them under both single and multithreaded environments without any regard to whether they are designed for single or multithreaded use?

In other words -- How can a multithreaded Visual Basic application use objects that are not designed to be thread safe? How can other multithreaded applications use single threaded Visual Basic objects?

In short: how does COM handle threading issues?

If you know about COM, you know that it defines the structure of a contract. A COM object agrees to follow certain rules so that it can be used successfully from any application or object that supports COM.

Most people think first of the interface part of the contract -- the methods and properties that an object exposes.

But you may not be aware of the fact that COM also defines threading as part of the contract. And like any part of the COM contract -- if you break it, you are in very deep trouble.

Visual Basic, naturally, hides most of this from you, but in order to understand what follows, you must learn a little bit about the COM threading models.

The Single Threading Model:

A single threaded server is the simplest type of server to implement. It is also the easiest to understand. In the case of an EXE server, the server runs in a single thread. All objects are created in that thread. All method calls to each object supported by the server must arrive in that thread.

But what if the client is running in a different thread? In that case, a proxy object must be created for the server object. This proxy object runs in the client''''s thread and reflects the methods and properties of the actual object. When a method is called on the proxy object, it performs whatever operations are necessary to switch to the object''''s thread, then calls the methods on the actual object using the parameters passed to the proxy. Naturally, this is a rather time consuming task -- but it does allow the contract to be followed. This process of switching threads and transferring data from the proxy object, to the actual object and back is called marshalling. It is covered in more depth in chapter 6 in my Developing ActiveX Components book.

In the case of DLL servers, the single threading model demands that all objects in the server be created and called in the same thread as the first object created by the server.

The Apartment Threading Model

Note that apartment model threading as defined by COM does NOT require that each thread have its own set of global variables. That''''s just how Visual Basic implemented the apartment model. The apartment model states that each object may be created in its own thread, however once an object is created, its methods and properties may only be called by the same thread that created the object. If another thread wants to access methods of the object, it must go through a proxy.

This is a relatively easy model to implement. If you eliminate global variables (as Visual Basic does), the apartment model grants you thread safety automatically -- since each object is effectively running in its own thread, and due to the lack of global variables, the different object threads do not interact with each other.

The Free Threading Model

The free threading model basically says that all bets are off. Any object can be created in any thread. All methods and properties on any object can be called at any time from any thread. The object accepts full responsibility for handling any necessary synchronization.

This is the hardest model to implement successfully, since it demands that the programmer handle all synchronization. In fact, until recently, OLE itself did not support this threading model! However, since marshalling is never required, this is the most efficient threading model.

Which model does your server support?

How does an application, or Windows itself, know which threading model a server is using? This information is included in the registry. When Visual Basic creates and object, it checks the registry to determine in which cases a proxy object and marshalling are required.

It is the client''''s responsibility to adhere strictly to the threading requirements of each object that it creates.

The CreateThread API

Now let''''s take a look at how the CreateThread API can be used with Visual Basic.

Say you have a class that you want to have running in another thread in order to perform some background operation. A generic class of this type might have the following code (from the MTDemo 3 example):

'''' Class clsBackground
'''' MTDemo 3 - Multithreading example
'''' Copyright © 1997 by Desaware Inc. All Rights Reserved
Option Explicit
Event DoneCounting()
Dim l As Long
Public Function DoTheCount(ByVal finalval&) As Boolean
Dim s As 

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