Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize Multi-thread programming Technology with VB.NET

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to achieve multithreaded programming technology in VB.NET. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

Traditional VB.NET multithreaded programming developers have established synchronous applications in which transactions are executed sequentially. Although multithreaded applications are more efficient because multiple transactions run more or less simultaneously, it is difficult to build such programs with previous versions of VisualBasic.

Multithreaded programs are feasible because the operating system is multitasking and has the ability to simulate running multiple applications at the same time. Although most personal computers have only one processor, today's operating systems provide multitasking by dividing processor time among multiple pieces of executing code. A thread may be the entire application, but it is usually a part of the application that can be run separately. The operating system allocates processing time to each thread according to its priority and how long it has been closest to running. Multithreading greatly improves the performance of applications with time-intensive transactions, such as file input and output.

But there are also places where care must be taken. Although multithreading can improve performance, each thread still needs additional memory to build and processor time to run, and building too many threads can degrade application performance. When designing multithreaded applications, you should compare performance with overhead. Multitasking has been part of the operating system for a long time. But until recently VisualBasic programmers were able to perform multithreaded transactions using the undocumented feature (undocumented) or indirectly using COM components or asynchronous parts of the operating system. The .NET Framework components provide comprehensive support in the System.Threading namespace for the development of multithreaded applications.

This article discusses the benefits of multithreading and how to develop multithreaded applications using VisualBasic .NET. Although the VisualBasic .NET and .NET Framework components make it easier to develop multithreaded applications, this article has adapted it to advanced readers and developers who want to move from early VisualBasic to VB.NET multithreaded programming.

1 advantages of multithreading

Although synchronous applications are easy to develop, their performance is generally lower than multithreaded applications because a new transaction must wait for the previous transaction to complete before it can start. If it takes longer than expected to complete a synchronous transaction, the application may not respond. Multithreading can run multiple processes at the same time. For example, a word processor can perform a spell-checking transaction while continuing to operate on a document. Because multithreaded applications break programs into separate transactions, they can fully improve performance in the following ways:

◆ multithreading technology makes it easier for programs to respond because the user interface stays active while other work continues.

Transactions that ◆ is not currently busy can give processor time to other transactions.

◆ transactions that spend a lot of processing time can periodically relinquish time to other transactions.

◆ transactions can be stopped at any time.

◆ can optimize performance by raising or lowering the priority of individual transactions. The decision to explicitly build a multithreaded application depends on several factors. Multithreading is best suited for the following situations:

◆ time-intensive or processing-intensive transactions interfere with the user interface.

◆ individual transactions must wait for external resources, such as remote files or Internet connections.

For example, an application follows a link on a Web page and downloads a file that meets certain criteria. Such applications can download files one by one synchronously or use multiple threads to download multiple files at the same time. The multithreaded approach is much more efficient than the synchronization method because files can be downloaded even if some threads receive slow responses from remote Web servers.

2 create a new thread

The most direct way to set up a thread is to create a new instance of the thread class and use the AddressOf statement to pass a delegate for the process you want to run. For example, the following code runs a subprocedure called SomeTask as a separate thread.

Dim Thread1 As New System.Threading.Thread (AddressOf SomeTask) Thread1.Start 'the code here runs immediately

This is all the work of setting up and starting threads. Any code that follows the Start method of the calling thread executes immediately without waiting for the end of the previous thread. The following table shows the methods you can use to control individual threads:

Most of the above methods are literally easy to understand, but the concept of safe point may be new to you. A safe point is a location in the code where the general language runtime can safely perform automatic garbage collection (garbage collection, the process of releasing unwanted variables and restoring memory). When a thread's Abort or Suspend method is called, the common language runtime analyzes the code and determines the appropriate place for the thread to stop running.

The following table is a list of some common properties of threads:

Its properties and methods are important when creating and managing threads. The "Thread synchronization" section of this article discusses how you can use these properties and methods to control and adjust threads.

3 Thread parameters and return values

The thread call in the previous example has no parameters and return values. This is one of the main disadvantages of using this method to build and run threads. However, you can wrap threads in a class or structure to provide and return parameters for procedures running on separate threads

The FriendStrArgAsString FriendRetValAsBoolean SubSomeTask () 'StrArg field is a parameter MsgBox ("TheStrArgcontainsthestring" & StrArg) RetVal=True' sets the return value in the return parameter EndSub EndClass' to use this class, set the property or field of the stored parameter Then asynchronously call the required method SubDoWork () DimTasksAsNewTasksClass () DimThread1AsNewSystem.Threading.Thread (AddressOfTasks.SomeTask) Tasks.StrArg= "SomeArg" 'set the field used as a parameter Thread1.Start ()' start a new thread Thread1.Join () 'wait for thread 1 to end' display the return value MsgBox ("Thread1returnedthevalue" & Tasks.RetVal) EndSub ClassTasksClass

Manually building and managing threads is best suited for applications that want good control over details, such as thread priority and thread model. As you might imagine, it is difficult to manage a large number of threads in this way. Consider using thread pools to reduce complexity when you need a lot of threads.

This is the end of this article on "how to achieve multithreaded programming technology in VB.NET". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report