Megosztás a következőn keresztül:


Threading (C# Programming Guide)

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. The System.Threading namespace provides classes and interfaces that support multithreaded programming and enable you to easily perform tasks such as creating and starting new threads, synchronizing multiple threads, suspending threads, and aborting threads.

To incorporate threading in your C# code, create a function to be executed outside the main thread and point a new Thread object at it. The following code example creates a new thread in a C# application:

System.Threading.Thread newThread;
newThread = new System.Threading.Thread(anObject.AMethod);

The following code example starts a new thread in a C# application:

newThread.Start();

Multithreading solves problems with responsiveness and multi-tasking, but can also introduce resource sharing and synchronization issues because threads are interrupted and resumed without warning according to a central thread that schedules mechanism. For more information, see Thread Synchronization (C# Programming Guide). See Using Threads and Threading for overview information.

Overview

Threads have the following properties:

  • Threads enable your C# program to perform concurrent processing.

  • The .NET Framework's System.Threading namespace makes using threads easier.

  • Threads share the application's resources. For more information, see Using Threads and Threading.

See the following topics for more information:

C# Language Specification

For more information, see the following sections in the C# Language Specification:

  • 3.10 Execution order

  • 8.12 The lock statement

  • 10.5.3 Volatile fields

  • 10.8.1 Field-like events

Threading in C# 3.0 in a Nutshell, Third Edition: A Desktop Quick Reference

Asynchronous Methods in C# 3.0 in a Nutshell, Third Edition: A Desktop Quick Reference

Threading and Synchronization in C# 3.0 Cookbook, Third Edition: More than 250 solutions for C# 3.0 programmers

See Also

Tasks

Monitor Synchronization Technology Sample

Wait Synchronization Technology Sample

Concepts

C# Programming Guide

Mutexes

Monitors

Interlocked Operations

AutoResetEvent

Reference

Thread

Mutex

Delegates (C# Programming Guide)

Other Resources

HOW TO: Synchronize Access to a Shared Resource in a Multithreading Environment by Using Visual C# .NET