Implement background processes in Visual Basic. NET

Source: Internet
Author: User
Tags comments
Rockford Lhotka
Magenic Technologies
October 1, 2002
Download the VBbackground.exe sample file from the MSDN Code Center ). (Note that in the example file, the comments of programmers are in English. In this document, the comments are translated into Chinese to facilitate your understanding .)
Abstract: Rocky Lhotka recommends and implements a structured architecture example, which can be used as a medium between the auxiliary thread and UI thread, this simplifies the process of writing multi-threaded auxiliary code and UI for control. This architecture includes downloadable code examples, which can be adjusted based on your application needs.
With multithreading, applications can execute multiple tasks at the same time. With multithreading, one thread can run the user interface, and another thread can perform complex operations or operations in the background. Microsoft & reg; Visual Basic & reg;. NET supports multithreading, so we can easily obtain this function.
However, multithreading also has its shortcomings. When an application uses multiple threads, we always encounter the problem that multiple threads attempt to interact with the same data or resources at the same time. In this case, the problem becomes very complex and difficult to debug.
What's worse, multi-threaded code usually seems to run normally during initial development, however, in the production process, an error occurs because multiple threads interact with the same data or resources at the same time. This increases the risk of multi-threaded programming!
Because it is very difficult to design and debug multi-threaded applications, Microsoft has created the "single-threaded unit" (STA) concept in COM. Visual Basic 6 code is always run in STA, so you only need to consider a single thread for the code. This completely avoids the problems caused by data sharing or resources, but it also means that we must take strict measures to take advantage of multithreading.
. NET does not have this common problem in Stas. ALL. NET code is run in the AppDomain that allows multi-thread operations. This means that Visual Basic. NET code is also running in AppDomain, so you can use multithreading. Obviously, you must write code carefully at any time to avoid conflicts between threads.
To avoid conflicts between threads, the simplest way is to ensure that multiple threads will never interact with the same data or resources. Although not possible, it is recommended that you avoid or minimize the use of shared data or resources during design for any multi-threaded application.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.