diff --git a/.openpublishing.redirection.json b/.openpublishing.redirection.json index 142585579d356..62f14b5139bf3 100644 --- a/.openpublishing.redirection.json +++ b/.openpublishing.redirection.json @@ -1514,6 +1514,14 @@ "source_path":"docs/csharp/programming-guide/concepts/threading/thread-synchronization.md", "redirect_url":"/dotnet/standard/threading/overview-of-synchronization-primitives" }, + { + "source_path":"docs/csharp/programming-guide/concepts/threading/index.md", + "redirect_url":"/dotnet/standard/threading/" + }, + { + "source_path":"docs/visual-basic/programming-guide/concepts/threading/index.md", + "redirect_url":"/dotnet/standard/threading/" + }, { "source_path":"docs/framework/windows-workflow-foundation/samples/absolute-delay.md", "redirect_url":"/previous-versions/dotnet/netframework-4.0/ff522352(v%3dvs.100)" diff --git a/docs/csharp/language-reference/keywords/volatile.md b/docs/csharp/language-reference/keywords/volatile.md index 41626eb45b0da..394e7fff7f37e 100644 --- a/docs/csharp/language-reference/keywords/volatile.md +++ b/docs/csharp/language-reference/keywords/volatile.md @@ -23,7 +23,7 @@ The `volatile` keyword can be applied to fields of these types: Other types, including `double` and `long`, cannot be marked `volatile` because reads and writes to fields of those types cannot be guaranteed to be atomic. To protect multi-threaded access to those types of fields, use the class members or protect access using the [`lock`](lock-statement.md) statement. -The volatile keyword can only be applied to fields of a `class` or `struct`. Local variables cannot be declared `volatile`. +The `volatile` keyword can only be applied to fields of a `class` or `struct`. Local variables cannot be declared `volatile`. ## Example @@ -31,7 +31,7 @@ The following example shows how to declare a public field variable as `volatile` [!code-csharp[declareVolatile](~/samples/snippets/csharp/language-reference/keywords/volatile/Program.cs#Declaration)] -The following example demonstrates how an auxiliary or worker thread can be created and used to perform processing in parallel with that of the primary thread. For background information about multithreading, see [Managed Threading](../../../standard/threading/index.md) and [Threading (C#)](../../programming-guide/concepts/threading/index.md). +The following example demonstrates how an auxiliary or worker thread can be created and used to perform processing in parallel with that of the primary thread. For more information about multithreading, see [Managed Threading](../../../standard/threading/index.md). [!code-csharp[declareVolatile](~/samples/snippets/csharp/language-reference/keywords/volatile/Program.cs#Volatile)] diff --git a/docs/csharp/programming-guide/concepts/index.md b/docs/csharp/programming-guide/concepts/index.md index 87f72d3e2d943..9c56c41d1383b 100644 --- a/docs/csharp/programming-guide/concepts/index.md +++ b/docs/csharp/programming-guide/concepts/index.md @@ -22,7 +22,6 @@ This section explains programming concepts in the C# language. |[Object-Oriented Programming (C#)](../../../csharp/programming-guide/concepts/object-oriented-programming.md)|Describes common object-oriented concepts, including encapsulation, inheritance, and polymorphism.| |[Reflection (C#)](../../../csharp/programming-guide/concepts/reflection.md)|Explains how to use reflection to dynamically create an instance of a type, bind the type to an existing object, or get the type from an existing object and invoke its methods or access its fields and properties.| |[Serialization (C#)](../../../csharp/programming-guide/concepts/serialization/index.md)|Describes key concepts in binary, XML, and SOAP serialization.| -|[Threading (C#)](../../../csharp/programming-guide/concepts/threading/index.md)|Provides an overview of the .NET threading model and shows how to write code that performs multiple tasks at the same time to improve the performance and responsiveness of your applications.| ## Related Sections diff --git a/docs/csharp/programming-guide/concepts/threading/index.md b/docs/csharp/programming-guide/concepts/threading/index.md deleted file mode 100644 index 67008ba49a8db..0000000000000 --- a/docs/csharp/programming-guide/concepts/threading/index.md +++ /dev/null @@ -1,34 +0,0 @@ ---- -title: "Threading (C#)" -ms.date: 07/20/2015 -ms.assetid: 236d157d-37c0-4ee8-89fc-721e6c596325 ---- -# Threading (C#) -Threading enables your C# program to perform concurrent processing so that you can do more than one operation at a time. For example, you can use threading to monitor input from the user, perform background tasks, and handle simultaneous streams of input. - - Threads have the following properties: - -- Threads enable your program to perform concurrent processing. - -- The .NET Framework namespace makes using threads easier. - -- Threads share the application's resources. For more information, see [Using Threads and Threading](../../../../../docs/standard/threading/using-threads-and-threading.md). - - By default, a C# program has one thread. However, auxiliary threads can be created and used to execute code in parallel with the primary thread. These threads are often called *worker threads*. - - Worker threads can be used to perform time-consuming or time-critical tasks without tying up the primary thread. For example, worker threads are often used in server applications to fulfill incoming requests without waiting for the previous request to be completed. Worker threads are also used to perform "background" tasks in desktop applications so that the main thread--which drives user interface elements--remains responsive to user actions. - - Threading solves problems with throughput and responsiveness, but it can also introduce resource-sharing issues such as deadlocks and race conditions. Multiple threads are best for tasks that require different resources such as file handles and network connections. Assigning multiple threads to a single resource is likely to cause synchronization issues, and having threads frequently blocked when waiting for other threads defeats the purpose of using multiple threads. - - A common strategy is to use worker threads to perform time-consuming or time-critical tasks that do not require many of the resources used by other threads. Naturally, some resources in your program must be accessed by multiple threads. For these cases, the namespace provides classes for synchronizing threads. These classes include , , , , and . - - You can use some or all these classes to synchronize the activities of multiple threads, but some support for threading is supported by the C# language. For example, the [Lock Statement](../../../../csharp/language-reference/keywords/lock-statement.md) provides synchronization features through implicit use of . - -> [!NOTE] -> Beginning with the [!INCLUDE[net_v40_long](~/includes/net-v40-long-md.md)], multithreaded programming is greatly simplified with the and classes, [Parallel LINQ (PLINQ)](../../../../standard/parallel-programming/parallel-linq-plinq.md), new concurrent collection classes in the namespace, and a new programming model that is based on the concept of tasks rather than threads. For more information, see [Parallel Programming](../../../../../docs/standard/parallel-programming/index.md). - -## Related Topics - -|Title|Description| -|-----------|-----------------| -|[Threading](../../../../../docs/standard/threading/index.md)|Describes how to implement threading in the .NET Framework.| diff --git a/docs/csharp/programming-guide/concepts/toc.md b/docs/csharp/programming-guide/concepts/toc.md index eba1ef3fde456..2ca90b9a819e1 100644 --- a/docs/csharp/programming-guide/concepts/toc.md +++ b/docs/csharp/programming-guide/concepts/toc.md @@ -11,4 +11,3 @@ ## [Object-Oriented Programming](object-oriented-programming.md) ## [Reflection](reflection.md) ## [Serialization (C#)](serialization/) -## [Threading](threading/index.md) diff --git a/docs/standard/threading/index.md b/docs/standard/threading/index.md index 1126201e50db0..54e4f02c55ddd 100644 --- a/docs/standard/threading/index.md +++ b/docs/standard/threading/index.md @@ -23,7 +23,7 @@ Whether you are developing for computers with one processor or several, you want Explains how to create, start, pause, resume, and abort threads. [Managed Threading Best Practices](../../../docs/standard/threading/managed-threading-best-practices.md) - Discusses levels of synchronization, how to avoid deadlocks and race conditions, single-processor and multiprocessor computers, and other threading issues. + Discusses levels of synchronization, how to avoid deadlocks and race conditions, and other threading issues. [Threading Objects and Features](../../../docs/standard/threading/threading-objects-and-features.md) Describes the managed classes you can use to synchronize the activities of threads and the data of objects accessed on different threads, and provides an overview of thread pool threads. diff --git a/docs/standard/threading/managed-threading-best-practices.md b/docs/standard/threading/managed-threading-best-practices.md index 5a2d5a2f09322..253300ddde852 100644 --- a/docs/standard/threading/managed-threading-best-practices.md +++ b/docs/standard/threading/managed-threading-best-practices.md @@ -1,6 +1,6 @@ --- title: "Managed Threading Best Practices" -ms.date: "11/30/2017" +ms.date: "10/15/2018" ms.technology: dotnet-standard dev_langs: - "csharp" @@ -13,13 +13,13 @@ ms.assetid: e51988e7-7f4b-4646-a06d-1416cee8d557 author: "rpetrusha" ms.author: "ronpet" --- -# Managed Threading Best Practices +# Managed threading best practices Multithreading requires careful programming. For most tasks, you can reduce complexity by queuing requests for execution by thread pool threads. This topic addresses more difficult situations, such as coordinating the work of multiple threads, or handling threads that block. > [!NOTE] > Starting with the .NET Framework 4, the Task Parallel Library and PLINQ provide APIs that reduce some of the complexity and risks of multi-threaded programming. For more information, see [Parallel Programming in .NET](../../../docs/standard/parallel-programming/index.md). -## Deadlocks and Race Conditions +## Deadlocks and race conditions Multithreading solves problems with throughput and responsiveness, but in doing so it introduces new problems: deadlocks and race conditions. ### Deadlocks @@ -53,7 +53,7 @@ else { } ``` -### Race Conditions +### Race conditions A race condition is a bug that occurs when the outcome of a program depends on which of two or more threads reaches a particular block of code first. Running the program many times produces different results, and the result of any given run cannot be predicted. A simple example of a race condition is incrementing a field. Suppose a class has a private **static** field (**Shared** in Visual Basic) that is incremented every time an instance of the class is created, using code such as `objCt++;` (C#) or `objCt += 1` (Visual Basic). This operation requires loading the value from `objCt` into a register, incrementing the value, and storing it in `objCt`. @@ -64,39 +64,20 @@ else { Race conditions can also occur when you synchronize the activities of multiple threads. Whenever you write a line of code, you must consider what might happen if a thread were preempted before executing the line (or before any of the individual machine instructions that make up the line), and another thread overtook it. -## Number of Processors - Most computers now have multiple processors (also called cores), even small devices such as tablets and phones. If you know you're developing software that will also run on single-processor computers, you should be aware that multithreading solves different problems for single-processor computers and multiprocessor computers. - -### Multiprocessor Computers - Multithreading provides greater throughput. Ten processors can do ten times the work of one, but only if the work is divided so that all ten can be working at once; threads provide an easy way to divide the work and exploit the extra processing power. If you use multithreading on a multiprocessor computer: - -- The number of threads that can execute concurrently is limited by the number of processors. - -- A background thread executes only when the number of foreground threads executing is smaller than the number of processors. - -- When you call the method on a thread, that thread might or might not start executing immediately, depending on the number of processors and the number of threads currently waiting to execute. - -- Race conditions can occur not only because threads are preempted unexpectedly, but because two threads executing on different processors might be racing to reach the same code block. - -### Single-Processor Computers - Multithreading provides greater responsiveness to the computer user, and uses idle time for background tasks. If you use multithreading on a single-processor computer: - -- Only one thread runs at any instant. - -- A background thread executes only when the main user thread is idle. A foreground thread that executes constantly starves background threads of processor time. - -- When you call the method on a thread, that thread does not start executing until the current thread yields or is preempted by the operating system. - -- Race conditions typically occur because the programmer did not anticipate the fact that a thread can be preempted at an awkward moment, sometimes allowing another thread to reach a code block first. - -## Static Members and Static Constructors +## Static members and static constructors A class is not initialized until its class constructor (`static` constructor in C#, `Shared Sub New` in Visual Basic) has finished running. To prevent the execution of code on a type that is not initialized, the common language runtime blocks all calls from other threads to `static` members of the class (`Shared` members in Visual Basic) until the class constructor has finished running. For example, if a class constructor starts a new thread, and the thread procedure calls a `static` member of the class, the new thread blocks until the class constructor completes. This applies to any type that can have a `static` constructor. + +## Number of processors + +Whether there are multiple processors or only one processor available on a system can influence multithreaded architecture. For more information, see [Number of Processors](https://docs.microsoft.com/previous-versions/dotnet/netframework-1.1/1c9txz50(v%3dvs.71)#number-of-processors). + +Use the property to determine the number of processors available at runtime. -## General Recommendations +## General recommendations Consider the following guidelines when using multiple threads: - Don't use to terminate other threads. Calling **Abort** on another thread is akin to throwing an exception on that thread, without knowing what point that thread has reached in its processing. @@ -139,7 +120,7 @@ else { ``` > [!NOTE] - > In the .NET Framework version 2.0, the method provides atomic updates in increments larger than 1. + > In the .NET Framework 2.0 and later, use the method for atomic increments larger than 1. In the second example, a reference type variable is updated only if it is a null reference (`Nothing` in Visual Basic). @@ -177,9 +158,9 @@ else { ``` > [!NOTE] - > In the .NET Framework version 2.0, the method has a generic overload that can be used for type-safe replacement of any reference type. + > Beginning with .NET Framework 2.0, the method overload provides a type-safe alternative for reference types. -## Recommendations for Class Libraries +## Recommendations for class libraries Consider the following guidelines when designing class libraries for multithreading: - Avoid the need for synchronization, if possible. This is especially true for heavily used code. For example, an algorithm might be adjusted to tolerate a race condition rather than eliminate it. Unnecessary synchronization decreases performance and creates the possibility of deadlocks and race conditions. diff --git a/docs/standard/threading/threads-and-threading.md b/docs/standard/threading/threads-and-threading.md index 15a605dce9239..43004ec1f0adc 100644 --- a/docs/standard/threading/threads-and-threading.md +++ b/docs/standard/threading/threads-and-threading.md @@ -1,91 +1,51 @@ --- -title: "Threads and Threading" -ms.date: "03/30/2017" +title: "Threads and threading" +ms.date: "11/08/2018" ms.technology: dotnet-standard helpviewer_keywords: - "multiple threads" - - "threading [.NET Framework]" - - "threading [.NET Framework], multiple threads" + - "threading [.NET]" + - "threading [.NET], multiple threads" ms.assetid: 5baac3aa-e603-4fa6-9f89-0f2c1084e6b1 author: "rpetrusha" ms.author: "ronpet" --- -# Threads and Threading -Operating systems use processes to separate the different applications that they are executing. Threads are the basic unit to which an operating system allocates processor time, and more than one thread can be executing code inside that process. Each thread maintains exception handlers, a scheduling priority, and a set of structures the system uses to save the thread context until it is scheduled. The thread context includes all the information the thread needs to seamlessly resume execution, including the thread's set of CPU registers and stack, in the address space of the thread's host process. - - The .NET Framework further subdivides an operating system process into lightweight managed subprocesses, called application domains, represented by . One or more managed threads (represented by ) can run in one or any number of application domains within the same managed process. Although each application domain is started with a single thread, code in that application domain can create additional application domains and additional threads. The result is that a managed thread can move freely between application domains inside the same managed process; you might have only one thread moving among several application domains. - - An operating system that supports preemptive multitasking creates the effect of simultaneous execution of multiple threads from multiple processes. It does this by dividing the available processor time among the threads that need it, allocating a processor time slice to each thread one after another. The currently executing thread is suspended when its time slice elapses, and another thread resumes running. When the system switches from one thread to another, it saves the thread context of the preempted thread and reloads the saved thread context of the next thread in the thread queue. - - The length of the time slice depends on the operating system and the processor. Because each time slice is small, multiple threads appear to be executing at the same time, even if there is only one processor. This is actually the case on multiprocessor systems, where the executable threads are distributed among the available processors. - -## When To Use Multiple Threads - Software that requires user interaction must react to the user's activities as rapidly as possible to provide a rich user experience. At the same time, however, it must do the calculations necessary to present data to the user as fast as possible. If your application uses only one thread of execution, you can combine [asynchronous programming](../../../docs/standard/asynchronous-programming-patterns/calling-synchronous-methods-asynchronously.md) with [.NET Framework remoting](https://docs.microsoft.com/previous-versions/dotnet/netframework-4.0/kwdt6w2k(v=vs.100)) or [XML Web services](https://docs.microsoft.com/previous-versions/dotnet/netframework-4.0/7bkzywba(v=vs.100)) created using ASP.NET to use the processing time of other computers in addition to that of your own to increase responsiveness to the user and decrease the data processing time of your application. If you are doing intensive input/output work, you can also use I/O completion ports to increase your application's responsiveness. - -### Advantages of Multiple Threads - Using more than one thread, however, is the most powerful technique available to increase responsiveness to the user and process the data necessary to get the job done at almost the same time. On a computer with one processor, multiple threads can create this effect, taking advantage of the small periods of time in between user events to process the data in the background. For example, a user can edit a spreadsheet while another thread is recalculating other parts of the spreadsheet within the same application. - - Without modification, the same application would dramatically increase user satisfaction when run on a computer with more than one processor. Your single application domain could use multiple threads to accomplish the following tasks: - -- Communicate over a network, to a Web server, and to a database. - -- Perform operations that take a large amount of time. - -- Distinguish tasks of varying priority. For example, a high-priority thread manages time-critical tasks, and a low-priority thread performs other tasks. - -- Allow the user interface to remain responsive, while allocating time to background tasks. - -### Disadvantages of Multiple Threads - It is recommended that you use as few threads as possible, thereby minimizing the use of operating-system resources and improving performance. Threading also has resource requirements and potential conflicts to be considered when designing your application. The resource requirements are as follows: - -- The system consumes memory for the context information required by processes, **AppDomain** objects, and threads. Therefore, the number of processes, **AppDomain** objects, and threads that can be created is limited by available memory. - -- Keeping track of a large number of threads consumes significant processor time. If there are too many threads, most of them will not make significant progress. If most of the current threads are in one process, threads in other processes are scheduled less frequently. - -- Controlling code execution with many threads is complex, and can be a source of many bugs. - -- Destroying threads requires knowing what could happen and handling those issues. - - Providing shared access to resources can create conflicts. To avoid conflicts, you must synchronize, or control the access to, shared resources. Failure to synchronize access properly (in the same or different application domains) can lead to problems such as deadlocks (in which two threads stop responding while each waits for the other to complete) and race conditions (when an anomalous result occurs due to an unexpected critical dependence on the timing of two events). The system provides synchronization objects that can be used to coordinate resource sharing among multiple threads. Reducing the number of threads makes it easier to synchronize resources. - - Resources that require synchronization include: - -- System resources (such as communications ports). - -- Resources shared by multiple processes (such as file handles). - -- The resources of a single application domain (such as global, static, and instance fields) accessed by multiple threads. - -### Threading and Application Design - In general, using the class is the easiest way to handle multiple threads for relatively short tasks that will not block other threads and when you do not expect any particular scheduling of the tasks. However, there are a number of reasons to create your own threads: - -- If you need a task to have a particular priority. - -- If you have a task that might run a long time (and therefore block other tasks). - -- If you need to place threads into a single-threaded apartment (all **ThreadPool** threads are in the multithreaded apartment). - -- If you need a stable identity associated with the thread. For example, you should use a dedicated thread to abort that thread, suspend it, or discover it by name. - -- If you need to run background threads that interact with the user interface, the .NET Framework version 2.0 provides a component that communicates using events, with cross-thread marshaling to the user-interface thread. - -### Threading and Exceptions - Do handle exceptions in threads. Unhandled exceptions in threads, even background threads, generally terminate the process. There are three exceptions to this rule: - -- A is thrown in a thread because was called. - -- An is thrown in a thread because the application domain is being unloaded. - -- The common language runtime or a host process terminates the thread. - - For more information, see [Exceptions in Managed Threads](../../../docs/standard/threading/exceptions-in-managed-threads.md). - +# Threads and threading + +Multithreading allows you to increase the responsiveness of your application and, if your application runs on a multiprocessor or multi-core system, increase its throughput. + +## Processes and threads + +A *process* is an executing program. An operating system uses processes to separate the applications that are being executed. A *thread* is the basic unit to which an operating system allocates processor time. Each thread has a [scheduling priority](scheduling-threads.md) and maintains a set of structures the system uses to save the thread context when the thread's execution is paused. The thread context includes all the information the thread needs to seamlessly resume execution, including the thread's set of CPU registers and stack. Multiple threads can run in the context of a process. All threads of a process share its virtual address space. A thread can execute any part of the program code, including parts currently being executed by another thread. + > [!NOTE] -> In the .NET Framework versions 1.0 and 1.1, the common language runtime silently traps some exceptions, for example in thread pool threads. This may corrupt application state and eventually cause applications to hang, which might be very difficult to debug. - +> The .NET Framework provides a way to isolate applications within a process with the use of *application domains*. (Application domains are not available on .NET Core.) For more information, see the [Application domains and threads](../../framework/app-domains/application-domains.md#application-domains-and-threads) section of the [Application domains](../../framework/app-domains/application-domains.md) article. + +By default, a .NET program is started with a single thread, often called the *primary* thread. However, it can create additional threads to execute code in parallel or concurrently with the primary thread. These threads are often called *worker* threads. + +## When to use multiple threads + +You use multiple threads to increase the responsiveness of your application and to take advantage of a multiprocessor or multi-core system to increase the application's throughput. + +Consider a desktop application, in which the primary thread is responsible for user interface elements and responds to user actions. Use worker threads to perform time-consuming operations that, otherwise, would occupy the primary thread and make the user interface non-responsive. You also can use a dedicated thread for network or device communication to be more responsive to incoming messages or events. + +If your program performs operations that can be done in parallel, the total execution time can be decreased by performing those operations in separate threads and running the program on a multiprocessor or multi-core system. On such a system, use of multithreading might increase throughput along with the increased responsiveness. + +## How to use multithreading in .NET + +Starting with the .NET Framework 4, the recommended way to utilize multithreading is to use [Task Parallel Library (TPL)](../parallel-programming/task-parallel-library-tpl.md) and [Parallel LINQ (PLINQ)](../parallel-programming/parallel-linq-plinq.md). For more information, see [Parallel programming](../parallel-programming/index.md). + +Both TPL and PLINQ rely on the threads. The class provides a .NET application with a pool of worker threads. You also can use thread pool threads. For more information, see [The managed thread pool](the-managed-thread-pool.md). + +At last, you can use the class that represents a managed thread. For more information, see [Using threads and threading](using-threads-and-threading.md). + +Multiple threads might need to access a shared resource. To keep the resource in a uncorrupted state and avoid race conditions, you must synchronize the thread access to it. You also might want to coordinate the interaction of multiple threads. .NET provides a range of types that you can use to synchronize access to a shared resource or coordinate thread interaction. For more information, see [Overview of synchronization primitives](overview-of-synchronization-primitives.md). + +Do handle exceptions in threads. Unhandled exceptions in threads generally terminate the process. For more information, see [Exceptions in managed threads](exceptions-in-managed-threads.md). + ## See also -- -- -- [Synchronizing Data for Multithreading](../../../docs/standard/threading/synchronizing-data-for-multithreading.md) -- [The Managed Thread Pool](../../../docs/standard/threading/the-managed-thread-pool.md) +- [Threading objects and features](threading-objects-and-features.md) +- [Managed threading best practices](managed-threading-best-practices.md) +- [Parallel Processing, Concurrency, and Async Programming in .NET](../parallel-processing-and-concurrency.md) +- [About Processes and Threads](/windows/desktop/procthread/about-processes-and-threads) \ No newline at end of file diff --git a/docs/toc.md b/docs/toc.md index 007df9b4e0cb0..ec5d7574b267a 100644 --- a/docs/toc.md +++ b/docs/toc.md @@ -510,7 +510,6 @@ ### [Object-Oriented Programming](visual-basic/programming-guide/concepts/object-oriented-programming.md) ### [Reflection](visual-basic/programming-guide/concepts/reflection.md) ### [Serialization](visual-basic/programming-guide/concepts/serialization/index.md) -### [Threading](visual-basic/programming-guide/concepts/threading/index.md) ## [Program Structure and Code Conventions](visual-basic/programming-guide/program-structure/program-structure-and-code-conventions.md) ### [Structure of a Program](visual-basic/programming-guide/program-structure/structure-of-a-visual-basic-program.md) diff --git a/docs/visual-basic/language-reference/statements/event-statement.md b/docs/visual-basic/language-reference/statements/event-statement.md index b16f1e6dea068..3218d4012af10 100644 --- a/docs/visual-basic/language-reference/statements/event-statement.md +++ b/docs/visual-basic/language-reference/statements/event-statement.md @@ -109,7 +109,7 @@ End Event Press F5 to run the previous example, and click the button labeled **Start**. The first text box starts to count down the seconds. When the full time (10 seconds) has elapsed, the first text box displays "Done". > [!NOTE] -> The `My.Application.DoEvents` method does not process events in the same way the form does. To enable the form to handle the events directly, you can use multithreading. For more information, see [Threading](../../programming-guide/concepts/threading/index.md). +> The `My.Application.DoEvents` method does not process events in the same way the form does. To enable the form to handle the events directly, you can use multithreading. For more information, see [Managed Threading](../../../standard/threading/index.md). ## See Also [RaiseEvent Statement](../../../visual-basic/language-reference/statements/raiseevent-statement.md) diff --git a/docs/visual-basic/language-reference/statements/raiseevent-statement.md b/docs/visual-basic/language-reference/statements/raiseevent-statement.md index 530c978112d38..81348e64fdb06 100644 --- a/docs/visual-basic/language-reference/statements/raiseevent-statement.md +++ b/docs/visual-basic/language-reference/statements/raiseevent-statement.md @@ -67,7 +67,7 @@ RaiseEvent eventname[( argumentlist )] Press F5 to run the preceding example, and click the button labeled **Start**. The first text box starts to count down the seconds. When the full time (10 seconds) has elapsed, the first text box displays "Done". > [!NOTE] -> The `My.Application.DoEvents` method does not process events in exactly the same way as the form does. To allow the form to handle the events directly, you can use multithreading. For more information, see [Threading](../../programming-guide/concepts/threading/index.md). +> The `My.Application.DoEvents` method does not process events in exactly the same way as the form does. To allow the form to handle the events directly, you can use multithreading. For more information, see [Managed Threading](../../../standard/threading/index.md). ## See Also [Events](../../../visual-basic/programming-guide/language-features/events/index.md) diff --git a/docs/visual-basic/programming-guide/concepts/index.md b/docs/visual-basic/programming-guide/concepts/index.md index 0f37594e0c2cb..6bf0c0d076eaa 100644 --- a/docs/visual-basic/programming-guide/concepts/index.md +++ b/docs/visual-basic/programming-guide/concepts/index.md @@ -22,7 +22,6 @@ This section explains programming concepts in the Visual Basic language. |[Object-Oriented Programming (Visual Basic)](../../../visual-basic/programming-guide/concepts/object-oriented-programming.md)|Describes common object-oriented concepts, including encapsulation, inheritance, and polymorphism.| |[Reflection (Visual Basic)](../../../visual-basic/programming-guide/concepts/reflection.md)|Explains how to use reflection to dynamically create an instance of a type, bind the type to an existing object, or get the type from an existing object and invoke its methods or access its fields and properties.| |[Serialization (Visual Basic)](../../../visual-basic/programming-guide/concepts/serialization/index.md)|Describes key concepts in binary, XML, and SOAP serialization.| -|[Threading (Visual Basic)](../../../visual-basic/programming-guide/concepts/threading/index.md)|Provides an overview of the .NET threading model and shows how to write code that performs multiple tasks at the same time to improve the performance and responsiveness of your applications.| ## Related Sections diff --git a/docs/visual-basic/programming-guide/concepts/threading/index.md b/docs/visual-basic/programming-guide/concepts/threading/index.md deleted file mode 100644 index 99023810f9c32..0000000000000 --- a/docs/visual-basic/programming-guide/concepts/threading/index.md +++ /dev/null @@ -1,34 +0,0 @@ ---- -title: "Threading (Visual Basic)" -ms.date: 07/20/2015 -ms.assetid: 704bb04b-ff23-471d-ab12-3cec1c2bca59 ---- -# Threading (Visual Basic) -Threading enables your Visual Basic program to perform concurrent processing so that you can do more than one operation at a time. For example, you can use threading to monitor input from the user, perform background tasks, and handle simultaneous streams of input. - - Threads have the following properties: - -- Threads enable your program to perform concurrent processing. - -- The .NET Framework namespace makes using threads easier. - -- Threads share the application's resources. For more information, see [Using Threads and Threading](../../../../standard/threading/using-threads-and-threading.md). - - By default, a Visual Basic program has one thread. However, auxiliary threads can be created and used to execute code in parallel with the primary thread. These threads are often called *worker threads*. - - Worker threads can be used to perform time-consuming or time-critical tasks without tying up the primary thread. For example, worker threads are often used in server applications to fulfill incoming requests without waiting for the previous request to be completed. Worker threads are also used to perform "background" tasks in desktop applications so that the main thread--which drives user interface elements--remains responsive to user actions. - - Threading solves problems with throughput and responsiveness, but it can also introduce resource-sharing issues such as deadlocks and race conditions. Multiple threads are best for tasks that require different resources such as file handles and network connections. Assigning multiple threads to a single resource is likely to cause synchronization issues, and having threads frequently blocked when waiting for other threads defeats the purpose of using multiple threads. - - A common strategy is to use worker threads to perform time-consuming or time-critical tasks that do not require many of the resources used by other threads. Naturally, some resources in your program must be accessed by multiple threads. For these cases, the namespace provides classes for synchronizing threads. These classes include , , , , and . - - You can use some or all these classes to synchronize the activities of multiple threads, but some support for threading is supported by the Visual Basic language. For example, the [SyncLock Statement](../../../../visual-basic/language-reference/statements/synclock-statement.md) provides synchronization features through implicit use of . - -> [!NOTE] -> Beginning with the [!INCLUDE[net_v40_long](~/includes/net-v40-long-md.md)], multithreaded programming is greatly simplified with the and classes, [Parallel LINQ (PLINQ)](../../../../standard/parallel-programming/parallel-linq-plinq.md), new concurrent collection classes in the namespace, and a new programming model that is based on the concept of tasks rather than threads. For more information, see [Parallel Programming](../../../../standard/parallel-programming/index.md). - -## Related Topics - -|Title|Description| -|-----------|-----------------| -|[Threading](../../../../standard/threading/index.md)|Describes how to implement threading in the .NET Framework.| diff --git a/docs/visual-basic/programming-guide/concepts/threading/toc.md b/docs/visual-basic/programming-guide/concepts/threading/toc.md deleted file mode 100644 index c87126d6163f6..0000000000000 --- a/docs/visual-basic/programming-guide/concepts/threading/toc.md +++ /dev/null @@ -1 +0,0 @@ -# [Threading](index.md) diff --git a/docs/visual-basic/programming-guide/language-features/events/walkthrough-handling-events.md b/docs/visual-basic/programming-guide/language-features/events/walkthrough-handling-events.md index 1206374ac1173..ef5e615c928e0 100644 --- a/docs/visual-basic/programming-guide/language-features/events/walkthrough-handling-events.md +++ b/docs/visual-basic/programming-guide/language-features/events/walkthrough-handling-events.md @@ -89,7 +89,7 @@ This is the second of two topics that demonstrate how to work with events. The f 3. Click the **Cancel** button to stop the task. Notice that the appearance of the **Cancel** button does not change immediately when you click it. The `Click` event cannot happen until the `My.Application.DoEvents` statement allows event processing. > [!NOTE] - > The `My.Application.DoEvents` method does not process events in exactly the same way as the form does. For example, in this walkthrough, you must click the **Cancel** button twice. To allow the form to handle the events directly, you can use multithreading. For more information, see [Threading](../../../../visual-basic/programming-guide/concepts/threading/index.md). + > The `My.Application.DoEvents` method does not process events in exactly the same way as the form does. For example, in this walkthrough, you must click the **Cancel** button twice. To allow the form to handle the events directly, you can use multithreading. For more information, see [Managed Threading](../../../../standard/threading/index.md). You may find it instructive to run the program with F11 and step through the code a line at a time. You can clearly see how execution enters `LongTask`, and then briefly re-enters `Form1` each time the `PercentDone` event is raised.