vb中线程怎么使⽤_VB.NET中的线程介绍
vb中线程怎么使⽤
To understand threading in VB.NET, it helps to understand some of the foundation concepts. First up is that threading is something that happens because the operating system supports it. Microsoft Windows is a pre-emptive multitasking operating system. A part of Windows called the task scheduler parcels out processor time to all the running programs. These small chunks of processor time are called time slices. Programs aren't in charge of how much processor time they get, the task scheduler is. Because these time slices are so small, you get the illusion that the computer is doing several things at once.
要了解VB.NET中的线程,它有助于了解⼀些基础概念。 ⾸先是线程是会发⽣的事情,因为操作系统⽀持它。 Microsoft Windows是抢先式多任务操作系统。 Windows的⼀部分称为任务计划程序,将处理器时间分配给所有正在运⾏的程序。 这些⼩的处理器时间块称为时间⽚。 程序不负责获得多少处理器时间,任务调度程序负责。 由于这些时间⽚⾮常⼩,您会感到计算机正在⼀次执⾏多项操作。
线程的定义 ( Definition of Thread )
A thread is a single sequential flow of control.
线程是控制的单个顺序流。
Some qualifiers:
⼀些预选赛:
A thread is a "path of execution" through that body of code.
线程是通过那段代码的“执⾏路径”。
Threads share memory so they have to cooperate to produce the correct result.
线程共享内存,因此它们必须合作以产⽣正确的结果。
A thread has thread-specific data such as registers, a stack pointer, and a program counter.
线程具有特定于线程的数据,例如寄存器,堆栈指针和程序计数器。
A process is a single body of code that can have many threads, but it has at least one and it has a single context
(address space).
进程是可以包含多个线程的单个代码主体,但是它⾄少具有⼀个线程,并且具有单个上下⽂(地址空间)。
This is assembly level stuff, but that's what you get into when you start thinking about threads.
这是程序集级别的东西,但这就是您开始考虑线程时要涉及的内容。
多线程与多处理 ( Multithreading vs. Multiprocessing )
is not the same as multicore parallel processing, but multithreading and multiprocessing do work together. Most PCs today have processors that have at least two cores, and ordinary home machines sometimes have up to eight cores. Each core is a separate processor, capable of running programs by itself. You get a performance boost when the OS assigns a different process to different cores. Using multiple threads and multiple processors for even greater performance is called thread-level parallelism.
与多核并⾏处理不同,但是多线程和多处理可以协同⼯作。 如今,⼤多数PC的处理器⾄少具有两个内核,⽽普通家⽤计算机有时最多具有⼋个内核。 每个内核是⼀个单独的处理器,能够⾃⼰运⾏程序。 当操作系统将不同的进程分配给不同的内核时,您将获得性能提升。 使⽤多个线程和多个处理器以获得更⾼的性能称为线程级并⾏性。
A lot of what can be done depends on what the operating system and the processor hardware can do, not always what you can do in your program, and you shouldn't expect to be able to use multiple threads on everything. In fact, you might not find many problems that benefit from multiple threads. So, don't implement multithreading just because it's there. You can easily reduce your program's performance if it's not a good candidate for multithreading. Just as examples, video codecs may be the worst programs to multithread because the data is inherently . Server programs that handle web pages might be among the best because the different clients are inherently independent.
可以完成的很多⼯作取决于操作系统和处理器硬件的⼯作能⼒,⽽并不总是取决于程序中可以执⾏的⼯作,并且您不应该期望能够在所有程序上使⽤多个线程。 实际上,您可能不到许多受益于多个线程的问题。 因此,不要仅仅因为多线程就在其中⽽实现多线程。 如果它不是多线程的理想选择,则可以轻松降低程序的性能。 就像⽰例⼀样,视频编解码器对于多线程来说可能是最差的程序,因为数据本质上是 。处理⽹页的服务器程序可能是最好的,因为不同的客户端本质上是独⽴的。
练习线程安全 ( Practicing Thread Safety )
Multithreaded code often requires complex coordination of threads. Subtle and difficult-to-find bugs are common because different threads often have to share the same data so data can be changed b
y one thread when another isn't expecting it. The general term for this problem is "race condition." In other words, the two threads can get into a "race" to update the same data and the result can be different depending on which thread "wins". As a trivial example, suppose you're coding a loop:
一个线程可以包含多个进程多线程代码通常需要复杂的线程协调。 细微且难以发现的错误是常见的,因为不同的线程通常必须共享相同的数据,因此当⼀个线程不期望使⽤另⼀个线程时就可以更改数据。 此问题的总称是“竞赛条件”。 换句话说,两个线程可以进⼊“竞赛”以更新相同的数据,并且结果取决于哪个线程“获胜”⽽有所不同。 作为⼀个简单的⽰例,假设您正在编写⼀个循环:
If the loop counter "I" unexpectedly misses the number 7 and goes from 6 to 8—but only some of the time—it would have disastrous effects on whatever the loop is doing. Preventing problems like this is called thread safety. If the program needs the result of one operation in a later operation, then it can be impossible to code parallel processes or threads to do it.
如果循环计数器“ I”意外地漏掉了数字7,并且从6变为8(但仅在某些时间),则对循环所做的任何事情都会造成灾难性的影响。 防⽌这样的问题称为线程安全。 如果程序在下⼀个操作中需要⼀个操作的结果,则可能⽆法编写并⾏进程或线程的代码来执⾏该操作。
基本多线程操作 ( Basic Multithreading Operations )
It's time to push this precautionary talk to the background and write some multithreading code. This article uses a Console Application for simplicity right now. If you want to follow along, start Visual Studio with a new Console Application project.
现在该把这种预防性的讨论推向后台并编写⼀些多线程代码了。 为了简便起见,本⽂现在使⽤控制台应⽤程序。 如果要继续,请使⽤新的Console Application项⽬启动Visual Studio。
The primary namespace used by multithreading is the System.Threading namespace and the Thread class will create, start, and stop new threads. In the example below, notice that TestMultiThreading is a delegate. That is, you have to use the name of a method that the Thread method can call.
多线程使⽤的主要命名空间是System.Threading命名空间,并且Thread类将创建,启动和停⽌新线程。 在下⾯的⽰例中,请注意TestMultiThreading是⼀个委托。 也就是说,您必须使⽤Thread⽅法可以调⽤的⽅法的名称。
In this app, we could have executed the second Sub by simply calling it:
在这个应⽤程序中,我们可以通过简单地调⽤它来执⾏第⼆个Sub:
This would have executed the entire application in serial fashion. The first code example above, how
ever, kicks off the TestMultiThreading subroutine and then continues.
这将以串⾏⽅式执⾏整个应⽤程序。 但是,上⾯的第⼀个代码⽰例启动了TestMultiThreading⼦例程,然后继续。
递归算法⽰例 ( A Recursive Algorithm Example )
Here's a multithreaded application involving calculating permutations of an array using a recursive algorithm. Not all of the code is shown here. The array of characters being permuted is simply "1," "2," "3," "4," and "5." Here's the pertinent part of the code.
这是⼀个多线程应⽤程序,涉及使⽤递归算法计算数组的排列。 此处未显⽰所有代码。 排列的字符数组只是“ 1,”,“ 2”,“
3”,“ 4”和“ 5”。 这是代码的相关部分。
Notice that there are two ways to call the Permute sub (both commented out in the code above). One kicks off a thread and the other calls it directly. If you call it directly, you get:
请注意,有两种⽅法可以调⽤Permute⼦项(在上⾯的代码中都注释掉了)。 ⼀个启动⼀个线程,另⼀个直接调⽤它。 如果直接调⽤它,则会得到:
However, if you kick off a thread and Start the Permute sub instead, you get:
但是,如果您启动⼀个线程并启动Permute⼦,则会得到:
This clearly shows that at least one permutation is generated, then the Main sub moves ahead and finishes, displaying "Finished Main," while the rest of the permutations are being generated. Since the display comes from a second sub called by the Permute sub, you know that is part of the new thread as well. This illustrates the concept that a thread is "a path of execution" as mentioned earlier.
这清楚地表明,⾄少⽣成了⼀个排列,然后Main⼦项继续前进并完成,显⽰“ Finished Main”,⽽其余的排列正在⽣成。 由于显⽰来⾃Permute⼦程序调⽤的第⼆个⼦程序,因此您知道这也是新线程的⼀部分。 如前所述,这说明了线程是“执⾏路径”的概念。
⽐赛条件⽰例 ( Race Condition Example )
The first part of this article mentioned a race condition. Here's an example that shows it directly:
本⽂的第⼀部分提到了竞争条件。 这是⼀个直接显⽰它的⽰例:
The Immediate window showed this result in one trial. Other trials were different. That's the essence of a race condition.
⽴即窗⼝在⼀次试验中显⽰了此结果。 其他试验是不同的。 那是⽐赛条件的本质。
vb中线程怎么使⽤

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。