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

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 夢在天涯 閱讀(1027) 評論(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

搜索

  •  

積分與排名

  • 積分 - 1816895
  • 排名 - 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>
              亚洲一区二区三区乱码aⅴ| 久久成人这里只有精品| 欧美激情一区在线| 亚洲国产综合在线看不卡| 久热精品视频在线观看| 久久久欧美精品| 亚洲精品日本| 一区二区三区成人精品| 国产欧美一区二区三区沐欲 | 久久久久一区二区| 亚洲日本成人网| 亚洲国产精品一区二区第一页| 欧美成人亚洲成人| 亚洲免费中文字幕| 午夜精品影院在线观看| 在线日韩精品视频| 亚洲毛片av| 国产日产亚洲精品| 欧美国产欧美亚洲国产日韩mv天天看完整 | 最新精品在线| 欧美香蕉大胸在线视频观看| 欧美在线国产精品| 久久久免费精品| 亚洲网址在线| 久久精品首页| 亚洲午夜精品在线| 久久精品国产77777蜜臀| 一本色道婷婷久久欧美| 亚洲免费一在线| 亚洲日本一区二区三区| 欧美亚洲尤物久久| 一本色道久久88综合日韩精品| 午夜久久资源| 亚洲天堂免费在线观看视频| 久久久999国产| 亚洲免费视频观看| 美日韩精品免费| 久久狠狠久久综合桃花| 欧美日精品一区视频| 美女诱惑黄网站一区| 国产精品久久久久久久9999| 欧美aa在线视频| 国产亚洲欧洲997久久综合| 亚洲欧洲在线视频| 亚洲大黄网站| 欧美伊久线香蕉线新在线| 日韩一级免费观看| 麻豆91精品91久久久的内涵| 久久国产手机看片| 国产精品h在线观看| 91久久国产精品91久久性色| 国产亚洲免费的视频看| 亚洲一级片在线观看| 99在线热播精品免费| 免费看精品久久片| 欧美国产精品中文字幕| 国产综合欧美| 欧美在线999| 久久久久久**毛片大全| 国产美女扒开尿口久久久| 一区二区三区精品国产| 亚洲视频一区二区免费在线观看| 女人天堂亚洲aⅴ在线观看| 欧美大秀在线观看| 亚洲国产va精品久久久不卡综合| 久久精品亚洲一区| 久久久www成人免费无遮挡大片| 国产老肥熟一区二区三区| 亚洲欧美国产77777| 欧美一级播放| 国精产品99永久一区一区| 亚洲制服av| 久久久久88色偷偷免费| 国内精品嫩模av私拍在线观看| 久久成人精品无人区| 久久夜精品va视频免费观看| 伊伊综合在线| 欧美成人一区在线| 亚洲三级免费| 亚洲综合导航| 国产日韩欧美在线播放| 久久久精品一区二区三区| 久久综合久久综合这里只有精品 | 欧美激情网友自拍| 亚洲免费av网站| 亚洲男人av电影| 国产视频在线观看一区二区三区 | 欧美国产在线电影| 日韩视频第一页| 性欧美大战久久久久久久久| 国产区亚洲区欧美区| 久久一二三四| 99精品免费| 久久久人成影片一区二区三区观看| 伊人久久亚洲美女图片| 欧美激情视频一区二区三区在线播放| 一本色道久久88精品综合| 久久精品主播| 夜夜精品视频| 国内视频一区| 欧美日韩免费一区| 欧美一区二区三区久久精品茉莉花 | 国产精品一区久久久| 久久影音先锋| 亚洲婷婷综合色高清在线| 久久综合色综合88| 亚洲无线一线二线三线区别av| 国产偷国产偷亚洲高清97cao| 嫩模写真一区二区三区三州| 亚洲综合社区| 91久久国产自产拍夜夜嗨| 欧美专区亚洲专区| 亚洲美女视频在线免费观看| 国产精品一区二区a| 欧美好骚综合网| 欧美在线视频免费| 亚洲视频一二| 欧美国产亚洲精品久久久8v| 欧美亚洲一区二区在线| 99精品欧美一区| 在线看片欧美| 国产一区二区三区久久久久久久久| 欧美精品一级| 久久人体大胆视频| 欧美影院一区| 亚洲欧美日韩天堂一区二区| 亚洲精品孕妇| 欧美激情一二三区| 美国十次成人| 久久国产精品99国产精| 亚洲欧美日本日韩| 国产精品99久久久久久有的能看 | 国产精品久久| 欧美日韩视频在线观看一区二区三区| 久久久精品午夜少妇| 欧美一区二区三区四区在线观看| 日韩午夜电影av| 亚洲三级观看| 亚洲国产成人av| 亚洲高清免费在线| 欧美1区3d| 奶水喷射视频一区| 久久综合一区二区三区| 欧美在线视频免费| 亚洲欧洲99久久| 性欧美xxxx视频在线观看| 亚洲永久在线| 亚洲一级二级在线| 亚洲影院在线观看| 亚洲欧美日韩国产综合| 亚洲一区二区少妇| 亚洲一区二区三区视频| 亚洲一区影音先锋| 羞羞视频在线观看欧美| 欧美中文字幕在线视频| 久久精品国产亚洲高清剧情介绍| 欧美在线播放视频| 久久久噜噜噜久噜久久| 老鸭窝91久久精品色噜噜导演| 玖玖综合伊人| 免费成人av在线| 亚洲欧洲一区二区三区久久| 一区二区久久久久| 亚洲欧美日本在线| 久久精品导航| 欧美成va人片在线观看| 欧美日本亚洲视频| 国产精品制服诱惑| 国产亚洲综合在线| 亚洲日本免费| 亚洲自啪免费| 久久综合色88| 亚洲看片网站| 欧美自拍偷拍午夜视频| 六月丁香综合| 欧美亚洲第一页| 激情久久五月天| 一区二区三区国产在线| 欧美在线免费| 亚洲国产视频一区| 亚洲欧美精品伊人久久| 久久尤物视频| 国产欧美日韩激情| 亚洲靠逼com| 欧美综合二区| 亚洲人成高清| 久久人人爽人人爽| 国产精品成人国产乱一区| 精品51国产黑色丝袜高跟鞋| 在线视频欧美日韩精品| 久久久国产一区二区三区| 亚洲片在线资源| 久久免费视频这里只有精品| 欧美三级欧美一级| 亚洲国产老妈| 欧美一区午夜精品| 一本色道久久综合一区| 巨胸喷奶水www久久久免费动漫| 国产精品久久久久久影视| 亚洲麻豆视频|