青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

C++ Programmer's Cookbook

{C++ 基礎} {C++ 高級} {C#界面,C++核心算法} {設計模式} {C#基礎}

c# Threads

1 使用多線程情況想必大家都知道拉:更好的界面交互...

2 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.

3 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.

4 Threading and Application Design

In general, using the ThreadPool 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 BackgroundWorker component that communicates using events, with cross-thread marshaling to the user-interface thread.

5 線程的使用方法:
In Win32 In the common language runtime

CreateThread

Combination of Thread and ThreadStart

TerminateThread

Thread.Abort

SuspendThread

Thread.Suspend

ResumeThread

Thread.Resume

Sleep

Thread.Sleep

WaitForSingleObject on the thread handle

Thread.Join

ExitThread

No equivalent

GetCurrentThread

Thread.CurrentThread

SetThreadPriority

Thread.Priority

No equivalent

Thread.Name

No equivalent

Thread.IsBackground

Close to CoInitializeEx (OLE32.DLL)

Thread.ApartmentState

6 線程狀態的改變:
Action Resulting new state

Another thread calls System.Thread.Start.

Unchanged

The thread responds to System.Threading.Thread.Startand starts running.

Running

The thread callsSystem.Threading.Thread.Sleep.

WaitSleepJoin

The thread calls System.Threading.Monitor.Wait on another object.

WaitSleepJoin

The thread calls System.Threading.Thread.Join on another thread.

WaitSleepJoin

Another thread calls System.Threading.Thread.Suspend.

SuspendRequested

The thread responds to aSystem.Threading.Thread.Suspend request.

Suspended

Another thread calls System.Threading.Thread.Resume.

Running

Another thread calls System.Threading.Thread.Suspend.

Running

Another thread calls System.Threading.Abort.

AbortRequested

The thread responds to an System.Threading.Abort.

Aborted

7 線程的優先級:
?emun: AboveNormal ...
8 線程的異常:

9?啟動帶參數的線程:(無參數的更簡單,與之類似)
ParameterizedThreadStart Delegate

using ?System;
using ?System.Threading;

public ? class ?Work
{
????
public ? static ? void ?Main()
????
{
????????
// ?To?start?a?thread?using?a?shared?thread?procedure,?use
????????
// ?the?class?name?and?method?name?when?you?create?the?
????????
// ?ParameterizedThreadStart?delegate.
????????
//
????????Thread?newThread? = ? new ?Thread(
????????????
new ?ParameterizedThreadStart(Work.DoWork));
????????
????????
// ?Use?the?overload?of?the?Start?method?that?has?a
????????
// ?parameter?of?type?Object.?You?can?create?an?object?that
????????
// ?contains?several?pieces?of?data,?or?you?can?pass?any?
????????
// ?reference?type?or?value?type.?The?following?code?passes
????????
// ?the?integer?value?42.
????????
//
????????newThread.Start( 42 );

????????
// ?To?start?a?thread?using?an?instance?method?for?the?thread?
????????
// ?procedure,?use?the?instance?variable?and?method?name?when?
????????
// ?you?create?the?ParameterizedThreadStart?delegate.
????????
//
????????Work?w? = ? new ?Work();
????????newThread?
= ? new ?Thread(
????????????
new ?ParameterizedThreadStart(w.DoMoreWork));
????????
????????
// ?Pass?an?object?containing?data?for?the?thread.
????????
//
????????newThread.Start( " The?answer. " );
????}

?
????
public ? static ? void ?DoWork( object ?data)
????
{
????????Console.WriteLine(
" Static?thread?procedure.?Data='{0}' " ,
????????????data);
????}


????
public ? void ?DoMoreWork( object ?data)
????
{
????????Console.WriteLine(
" Instance?thread?procedure.?Data='{0}' " ,
????????????data);
????}

}


/* ?This?code?example?produces?the?following?output?(the?order?
???of?the?lines?might?vary):

Static?thread?procedure.?Data='42'
Instance?thread?procedure.?Data='The?answer'
*/



10?控制線程的執行:
using?System;
using?System.Threading;

public?class?ThreadControlExample?{

????
private?static?void?DisplayMessage()?{

????????
//?Repeatedly?display?a?message?to?the?console.
????????while?(true)?{

????????????
try?{

????????????????Console.WriteLine(
"{0}?:?Second?thread?running.?Enter"
????????????????????
+?"?(S)uspend,?(R)esume,?(I)nterrupt,?or?(E)xit.",
????????????????????DateTime.Now.ToString(
"HH:mm:ss.ffff"));

????????????????
//?Sleep?for?2?seconds.
????????????????Thread.Sleep(2000);

????????????}
?catch?(ThreadInterruptedException)?{

????????????????
//?Thread?has?been?interrupted.?Catching?the?
????????????????
//?ThreadInterruptedException?allows?the?example?to?
????????????????
//?take?appropriate?action?and?continue?execution.
????????????????Console.WriteLine("{0}?:?Second?thread?interrupted.",
????????????????????DateTime.Now.ToString(
"HH:mm:ss.ffff"));

????????????}
?catch?(ThreadAbortException?abortEx)?{

????????????????
//?The?object?in?the?ThreadAbortException.ExceptionState
????????????????
//?property?is?provided?by?the?thread?that?called?
????????????????
//?Thread.Abort.?In?this?case?it?contains?a?string?that?
????????????????
//?describes?the?reason?for?the?abort.
????????????????Console.WriteLine("{0}?:?Second?thread?aborted?({1})",??
????????????????????DateTime.Now.ToString(
"HH:mm:ss.ffff"),
????????????????????abortEx.ExceptionState);

????????????????
//?Even?though?ThreadAbortException?has?been?handled,?the
????????????????
//?runtime?will?throw?it?again?to?ensure?the?thread?
????????????????
//?terminates.
????????????}

????????}

????}


????
public?static?void?Main()?{

????????
//?Create?a?new?Thread?object?and?pass?it?a?ThreadStart
????????
//?delegate?instance?that?references?DisplayMessage.
????????Thread?thread?=?new?Thread(new?ThreadStart(DisplayMessage));

????????Console.WriteLine(
"{0}?:?Starting?second?thread.",??
????????????DateTime.Now.ToString(
"HH:mm:ss.ffff"));

????????
//?Start?the?second?thread.
????????thread.Start();

????????
//?Loop?and?process?the?command?entered?by?the?user.
????????char?command?=?'?';

????????
do?{

????????????
string?input?=?Console.ReadLine();
????????????
if?(input.Length?>?0)?command?=?input.ToUpper()[0];
????????????
else?command?=?'?';

????????????
switch?(command)?{

????????????????
case?'S':
????????????????????
//?Suspend?the?second?thread.
????????????????????Console.WriteLine("{0}?:?Suspending?second?thread.",
????????????????????????DateTime.Now.ToString(
"HH:mm:ss.ffff"));
????????????????????thread.Suspend();
????????????????????
break;

????????????????
case?'R':
????????????????????
//?Resume?the?second?thread.
????????????????????try?{?
????????????????????????Console.WriteLine(
"{0}?:?Resuming?second?thread.",
????????????????????????????DateTime.Now.ToString(
"HH:mm:ss.ffff"));
????????????????????????thread.Resume();
????????????????????}
?catch?(ThreadStateException)?{
????????????????????????Console.WriteLine(
"{0}?:?Thread?wasn't?suspended.",
????????????????????????????DateTime.Now.ToString(
"HH:mm:ss.ffff"));
????????????????????}

????????????????????
break;

????????????????
case?'I':
????????????????????
//?Interrupt?the?second?thread.
????????????????????Console.WriteLine("{0}?:?Interrupting?second?thread.",
????????????????????????DateTime.Now.ToString(
"HH:mm:ss.ffff"));
????????????????????thread.Interrupt();
????????????????????
break;

????????????????
case?'E':
????????????????????
//?Abort?the?second?thread?and?pass?a?state?object?to
????????????????????
//?the?thread?being?aborted,?in?this?case?a?message.
????????????????????Console.WriteLine("{0}?:?Aborting?second?thread.",??
????????????????????????DateTime.Now.ToString(
"HH:mm:ss.ffff"));

????????????????????thread.Abort(
"Terminating?example.");

????????????????????
//?Wait?for?the?second?thread?to?terminate.
????????????????????thread.Join();
????????????????????
break;
????????????}

????????}
?while?(command?!=?'E');

????????
//?Wait?to?continue.
????????Console.WriteLine("Main?method?complete.?Press?Enter.");
????????Console.ReadLine();
????}

}

posted on 2006-04-20 18:31 夢在天涯 閱讀(1021) 評論(0)  編輯 收藏 引用 所屬分類: C#/.NET

公告

EMail:itech001#126.com

導航

統計

  • 隨筆 - 461
  • 文章 - 4
  • 評論 - 746
  • 引用 - 0

常用鏈接

隨筆分類

隨筆檔案

收藏夾

Blogs

c#(csharp)

C++(cpp)

Enlish

Forums(bbs)

My self

Often go

Useful Webs

Xml/Uml/html

搜索

  •  

積分與排名

  • 積分 - 1811734
  • 排名 - 5

最新評論

閱讀排行榜

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
      <noscript id="pjuwb"></noscript>
            <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
              <dd id="pjuwb"></dd>
              <abbr id="pjuwb"></abbr>
              久久免费的精品国产v∧| 久久www免费人成看片高清| 欧美成人激情在线| 亚洲国产日韩欧美在线图片| 亚洲电影在线免费观看| 欧美粗暴jizz性欧美20| 一区二区免费在线视频| 中文网丁香综合网| 国产一区二区三区免费不卡| 美女黄色成人网| 欧美精品久久99| 午夜久久美女| 久久综合久久久久88| 一本色道久久99精品综合| 亚洲专区一区二区三区| 1769国产精品| 亚洲视频中文| 在线观看三级视频欧美| 9i看片成人免费高清| 国内精品久久久久影院色| 亚洲国产一区二区a毛片| 国产精品男女猛烈高潮激情| 久久免费午夜影院| 欧美日韩在线亚洲一区蜜芽| 久久免费国产| 欧美日韩久久| 免费观看不卡av| 国产精品a级| 欧美激情va永久在线播放| 国产精品成人免费| 欧美激情女人20p| 国产深夜精品| 99re6这里只有精品视频在线观看| 国产日韩精品入口| 亚洲美女黄网| 亚洲国产99| 欧美在线视频导航| 亚洲欧美另类中文字幕| 欧美mv日韩mv国产网站app| 欧美有码在线观看视频| 欧美日本一道本| 欧美激情片在线观看| 国产亚洲人成a一在线v站 | 久久精品99国产精品| 欧美刺激性大交免费视频| 久久中文字幕一区| 国产女人水真多18毛片18精品视频| 亚洲激情视频网| 经典三级久久| 久久激情视频免费观看| 久久国产欧美| 国产日韩欧美一区二区三区四区 | 免费观看在线综合| 精品69视频一区二区三区| 性高湖久久久久久久久| 亚洲免费在线观看| 国产精品久久久久久一区二区三区| 亚洲精品韩国| 一本久久知道综合久久| 欧美精品久久天天躁| 91久久国产综合久久| 亚洲美女av在线播放| 欧美大片专区| 亚洲精品系列| 亚洲欧美日韩中文在线制服| 欧美亚男人的天堂| 亚洲无亚洲人成网站77777 | 在线不卡免费欧美| 玖玖在线精品| 最新国产成人在线观看| 亚洲免费激情| 国产精品国产馆在线真实露脸 | 日韩午夜精品视频| 亚洲欧美日韩国产一区二区| 国产精品试看| 午夜精品视频网站| 免费av成人在线| 亚洲另类春色国产| 欧美日韩色综合| 性伦欧美刺激片在线观看| 另类av一区二区| 日韩亚洲欧美综合| 国产精品免费福利| 久久久久久久999| 亚洲人被黑人高潮完整版| 亚洲免费网址| 红桃视频国产精品| 欧美精品久久久久久| 亚洲影院色无极综合| 免费成人性网站| 亚洲图片在线| 今天的高清视频免费播放成人| 欧美福利在线| 亚洲欧美影院| 亚洲精品三级| 美女精品视频一区| 亚洲淫片在线视频| 伊人婷婷欧美激情| 国产精品久久97| 老司机aⅴ在线精品导航| 中文国产一区| 亚洲福利视频一区| 久久精品理论片| av成人免费| 亚洲国产高清在线观看视频| 欧美亚一区二区| 免费成人黄色片| 欧美一级欧美一级在线播放| 亚洲日本成人| 免费试看一区| 欧美伊人久久| 亚洲一区二区成人在线观看| 亚洲激情影院| 激情久久综合| 国产日韩精品一区| 欧美三区不卡| 欧美久久久久久久| 久久免费视频网站| 午夜欧美不卡精品aaaaa| 99在线热播精品免费99热| 亚洲激情一区二区| 欧美成年网站| 欧美gay视频| 蜜桃久久精品一区二区| 久久久精品动漫| 久久精品九九| 欧美在线在线| 欧美在线免费视频| 欧美在线视频播放| 亚洲欧美日韩国产综合精品二区| 亚洲美女精品成人在线视频| 亚洲电影在线播放| 亚洲电影观看| 亚洲国产天堂久久综合| 亚洲第一黄网| 91久久久国产精品| 亚洲人成人77777线观看| 亚洲国产精品美女| 亚洲国产美女| 亚洲美女在线看| 一本色道久久88精品综合| 亚洲最黄网站| 亚洲免费在线电影| 欧美一区二区精品在线| 欧美在线视频免费| 久久蜜桃香蕉精品一区二区三区| 久久国产日韩| 美女日韩在线中文字幕| 欧美国产日韩免费| 亚洲欧洲日本在线| 在线中文字幕不卡| 性色av一区二区三区| 久久精品日产第一区二区| 久久午夜视频| 欧美精品在欧美一区二区少妇| 欧美日韩国内自拍| 国产模特精品视频久久久久 | 欧美二区在线播放| 欧美网站在线| 国产一区再线| 亚洲免费av片| 亚洲欧美成人一区二区在线电影| 欧美一区二区三区免费看| 久久天天躁狠狠躁夜夜爽蜜月| 欧美福利影院| 正在播放亚洲一区| 久久久久天天天天| 欧美日韩免费在线| 国产亚洲精品综合一区91| 亚洲精品日韩欧美| 欧美在线视频二区| 亚洲人成毛片在线播放| 亚洲一区二区三区在线看| 久久久另类综合| 欧美视频一区二区三区四区| 国内精品久久久| 夜夜狂射影院欧美极品| 久久久成人网| 亚洲免费观看高清完整版在线观看熊| 亚洲在线观看视频| 免费久久99精品国产自在现线| 国产精品国产三级国产a| 亚洲国产成人不卡| 欧美在线观看一区二区| 亚洲电影网站| 久久国产福利国产秒拍| 欧美日韩三区| 亚洲破处大片| 久久视频免费观看| 亚洲香蕉在线观看| 欧美理论电影网| 在线成人小视频| 欧美在线|欧美| 一片黄亚洲嫩模| 欧美a级在线| 在线观看av不卡| 久久国产精品久久久久久久久久| 亚洲另类一区二区| 欧美大片免费观看| 亚洲国产va精品久久久不卡综合|