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

of

Contents

  1. Overview
  2. What is a separate code thread?
  3. Why/When to have a separate thread.
  4. The tool for separate threads in Visual Basic.
  5. The problem starting the server''''s execution.
  6. RaiseEvent is a one way street.
  7. Reentrancy considerations.
  8. The timer examples in the book.
  9. A solution for starting the server''''s execution.
10. A bit about marshaling.
11. The Mini-Database demo project.
Overview

In Visual Basic it is possible to obtain one or more additional separate code threads to operate a single program. The threads are stable and secure and communication between them is provided for intrinsicly. There is no need to hack the language with unpredictable API''''s.

This page focuses on how that is done and demonstrates the problem you may have (or not be aware of) in starting these threads running.

Client and server are object oriented programming (OOP) terms. To simplify matters in the ensuing discussion, the client will be a standard EXE. The server will be an ActiveX component object instantiated by the client. The client sends information and requests to the server object. The server responds when the job is done by raising an event in the client and usually returning some processed information to the client.

What is a separate code thread?

A thread is a list of assembly level instructions that the CPU (central processing unit) of your computer executes one after another, one at a time, all the way until a program ends and releases the thread. The Windows?programs that you can switch between when you click on the taskbar at the bottom of the screen are operating on separate threads. The amazing thing is that not only can you switch from one program to another, but they can all run at once!

This is possible because the CPU can switch code threads. It stops executing one thread, saves all the information about that thread, loads all the information about the next thread in line, and then starts executing that set of instructions. Countless thread code switches are happening right now in your computer every second as you read this.

So technically the programs on your taskbar are not all running at once, but the switching normally happens so fast that it seems like they are.

A switch occurs whenever the CPU gets a break from the program holding the presently executing thread. In Visual Basic these breaks are provided automatically when waiting for some type of input (a key press or a mouse click). If your code performs many instructions without a break, it may monopolize the CPU so that other programs can''''t get their share of CPU time. In such a case, it is up to you as a good programmer to be courteous and judiciously allow breaks with the DoEvents statement.

Why/When to have a separate thread:

If you play music on your computer while you type an email reply, or if you keep more than one web browser window open at the same time to download one page while you read another, then you are aware of the benefits of separate code threads.

In these instances, the operating system provides separate threads automatically, and the applications really don''''t have to work together as client and server.

When could we take advantage of separate code threads in a single program? ?/FONT> To synchronize input of data with processing data. An example would be allowing an user to complete a second form, while the first is being processing. ?/FONT> To synchronize complex computations where separate, alternating processing may allow for more rapid completion. i.e. - The result of processing on one thread arrives in time to be used on the other. ?/FONT> To resolve or eliminate reentrancy quirks and conflicts that arise in a single thread when it overlaps itself and generally performs operations in an order not anticipated. Chasing these overlaps with flags and exits ends up becoming just plain bad programming. ?/FONT> To provide a model of a remote server during development. In network or internet settings, it is possible to instantiate a remote server object for a local client program. Here again a separate thread is provided automatically because, of course, two machines can not run in the same thread. However, the code for such a remote instantiation is usually subtly if not significantly different than the code for a local server. A separately threaded local program will provide a precise model to enable the programmer to think "remotely."

The tool for separate threads in Visual Basic:

Of course such a discussion would be moot if there were no vehicle for implementation. Fortunately, Visual Basic has provided us such a tool through ActiveX, and specifically the ActiveX EXE type component. It is there specifically to give us a separate thread - if you only need a same thread library, then choose an ActiveX DLL as your project.

In Visual Basic:
  • Same thread = In process = ActiveX DLL
  • Separate thread = Out of process = ActiveX EXE
  • ActiveX has been designed by Microsoft to nudge Visual Basic programmers toward object oriented programming. It is through the instantiation of a required public class that ActiveX is able to pipe us into the safe haven of the COM standard and provide us with secure conversations between separate threads. You will also be happy to learn that the App Object has a .ThreadID property which will be your compass for exploration.

    It is the ActiveX EXE we shall explore for the separate code thread it provides. Visual Basic has the ability to allow for multi-threads of separately threaded EXE servers (additional threads for each instantiation) through unattended execution compilation options. The examples herein only deal with single, separate threads of execution.

    Being a single, separate thread does not mean that it can not have more than one client instantiating and calling it. When a component has one thread of execution, code for only one object executes at a time. The Automation feature of the Component Object Model (COM) deals with this situation by serializing requests. That is, the requests are queued and processed one at a time until all have been completed. It is limited because if control is yielded, the next queued call is allowed entry and if unanticipated, reentrancy problems may occur.

    In ActiveX components, control is yielded with:
  • 1. DoEvents
  • 2. Calling an object out of process
  • 3. Raising an event in an object out of process
  • Another advantage of having a single, separate thread on which all instances of a server object (instantiations by more than one client included) will run is that globally defined variables in the server (declared Public in a code module) will be visible to all instances of the server. Such global variables in multi-threaded servers are not supported in Visual Basic. (It''''s hard to do because it requires synchronization of the multi-threads.)
    The problem starting the server''''s execution:

    Simply calling a public method of a server running on a separate thread will start the server executing code. The problem is that the calling client will wait at that instruction for the return from the server. This will happen even if the call is to a Sub in the server (i.e. expecting no return value as with a function).

    To illustrate:

    Example One:
    
    ''''==============================================
    ''''The Server Code: compiled as an ActiveX EXE
    ''''     Class name: threadServer
    ''''==============================================
    Option Explicit
    Event done(id&)
    
    Public Sub waitAwhile()
        Dim j#, k#
        For j = 1 To 300000
            k = j
            DoEvents
        Next
        
        RaiseEvent done(App.ThreadID)
    End Sub
    
    ''''==============================================
    ''''The Client Code run from the VB IDE
    ''''==============================================
    Option Explicit
    Private WithEvents ts1 As threadServer
    
    Private Sub Form_Load()
        AutoRedraw = True
        Set ts1 = New threadServer
    End Sub
    
    Private Sub Form_Click()
        Cls
        ts1.waitAwhile
        Print "Client done at: " & Str$(Timer) & _
            "   on thread: " &

    [1] [2]  下一页


    [办公软件]ASC、CODE、PHONETIC等等文本与数据函数大全  [办公软件]Word表格中Shift+Alt+方向键的妙用
    [系统软件]A REVIEW OF SQLEXPLORER PLUG-IN  [VB.NET程序]Read a string at a given address
    [VB.NET程序]Read a byte, integer or long from memory  [Delphi程序]// I have a comment ----Delphi 研发人员谈注释 …
    [Delphi程序]// I have a comment  ----Delphi 研发人员谈注释  [Delphi程序]Creating a real singleton class in Delphi 5
    [Delphi程序]Download a file from a FTP Server  [Delphi程序]2004.11.29.Starting a Project

    Starting a separate code thread

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

    Starting a
    Separate Code Thread

    in Visual Basic?/H3>

    Go to Rick''''s site.

    by Rick Meyer
    教程录入: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……
    咸宁网络警察报警平台