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

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

搜索

  •  

積分與排名

  • 積分 - 1816909
  • 排名 - 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>
              亚洲国产福利在线| 亚洲美女毛片| 欧美影院久久久| 国产日韩欧美一区二区三区在线观看| 亚洲网址在线| 亚洲午夜精品久久久久久浪潮 | 久久精品国产精品 | 美女露胸一区二区三区| 久久久久一本一区二区青青蜜月| 国产一区导航| 欧美电影在线观看| 欧美日韩美女| 欧美一区二区国产| 久久综合伊人77777麻豆| 亚洲人体1000| 亚洲青涩在线| 国产精品久久精品日日| 久久先锋影音| 欧美日韩国产影片| 久久综合99re88久久爱| 欧美国产亚洲精品久久久8v| 亚洲在线成人| 蜜臀久久99精品久久久画质超高清 | 欧美欧美在线| 久久国产精品亚洲va麻豆| 久久人人爽人人爽| 亚洲一区激情| 免费成人性网站| 欧美在线视频一区| 欧美日韩成人在线| 久久影院午夜片一区| 欧美日韩国产黄| 美腿丝袜亚洲色图| 国产九区一区在线| 日韩视频免费| 亚洲国产天堂久久综合| 亚洲一区日韩在线| 一本色道婷婷久久欧美| 久久久久久一区二区| 亚洲欧美在线高清| 欧美激情欧美狂野欧美精品 | 永久免费毛片在线播放不卡| 夜夜狂射影院欧美极品| 亚洲精品视频在线观看网站| 欧美一区二区三区男人的天堂| 亚洲久久一区二区| 久久亚洲欧美| 久久久久高清| 国产欧美日韩专区发布| 亚洲视频999| 亚洲午夜高清视频| 欧美日韩精品一二三区| 亚洲国产合集| 亚洲国产第一| 久久久久一区| 另类尿喷潮videofree| 国产精品美女999| 99re视频这里只有精品| 99re热这里只有精品视频| 久久综合久色欧美综合狠狠| 欧美在线亚洲| 国产一区二区三区高清 | 久久蜜桃香蕉精品一区二区三区| 欧美视频网址| 宅男在线国产精品| 亚洲专区在线视频| 国产精品美女在线| 亚洲尤物视频网| 久久成人精品视频| 国产亚洲欧美aaaa| 久久久久久久久岛国免费| 久久久久国产精品一区二区| 国产一区二区日韩| 欧美一区成人| 乱人伦精品视频在线观看| 在线精品国精品国产尤物884a| 久久九九久久九九| 欧美激情中文字幕一区二区| 亚洲日韩成人| 国产精品www网站| 亚洲欧美另类在线| 麻豆久久精品| 日韩视频一区二区三区| 欧美日韩午夜剧场| 亚洲专区在线| 蜜臀99久久精品久久久久久软件| 亚洲黄色高清| 国产精品久久久久国产a级| 亚洲欧美日本国产专区一区| 久久久久久穴| 亚洲精品网址在线观看| 欧美性猛交xxxx免费看久久久| 亚洲在线第一页| 欧美好吊妞视频| 亚洲视频在线观看免费| 国产亚洲欧美日韩一区二区| 免费成人黄色av| 亚洲尤物在线| 亚洲国产高清自拍| 香蕉av福利精品导航| 狠狠色综合色区| 欧美人与禽性xxxxx杂性| 亚洲女优在线| 亚洲人www| 久久视频这里只有精品| 一本一本久久| 在线观看的日韩av| 国产精品久久久久永久免费观看| 欧美专区在线播放| 99re6热只有精品免费观看 | 欧美在线观看网站| 最新中文字幕亚洲| 国产日产精品一区二区三区四区的观看方式| 久久精品成人| 99riav1国产精品视频| 老鸭窝毛片一区二区三区| 亚洲欧美国产毛片在线| 日韩视频免费看| 在线不卡中文字幕| 在线视频免费在线观看一区二区| 国产日韩欧美综合精品| 欧美精品激情在线| 久久婷婷久久| 欧美一区二区三区视频| 中日韩视频在线观看| 亚洲高清视频一区二区| 久久综合色综合88| 久久精品99久久香蕉国产色戒| 亚洲深夜av| 一本大道久久a久久精品综合| 国外成人网址| 久热精品视频在线观看| 欧美在线关看| 欧美一区二区三区久久精品茉莉花 | 国产精品久久久久久久久久久久久 | 亚洲一区欧美二区| 一区二区三区欧美激情| 亚洲国产视频一区二区| 嫩草伊人久久精品少妇av杨幂| 久久久精品日韩欧美| 欧美怡红院视频| 欧美一区二区私人影院日本| 亚洲一区成人| 亚洲欧美在线免费观看| 亚洲一级在线| 午夜国产精品影院在线观看 | 国户精品久久久久久久久久久不卡| 国产精品激情| 国产乱码精品一区二区三区不卡 | 美女视频黄a大片欧美| 蜜桃av久久久亚洲精品| 美女被久久久| 欧美片第一页| 国产精品久久久久久超碰 | 欧美日韩国产综合网| 欧美日韩一区二区在线视频| 欧美日本亚洲| 国产精品你懂的在线| 国产一区二区视频在线观看| 一区二区三区我不卡| 亚洲国产精品成人va在线观看| 亚洲精品欧美| 亚洲欧美影音先锋| 久久久久久亚洲综合影院红桃 | 欧美在线一级va免费观看| 久久久亚洲综合| 亚洲国产天堂久久综合| 日韩亚洲欧美中文三级| 亚洲欧美日韩国产综合| 久久一区二区三区四区| 欧美激情亚洲一区| 国产精品久久久久久久久免费樱桃 | 你懂的一区二区| 欧美午夜视频在线| 狠狠网亚洲精品| 日韩视频免费观看| 香蕉av777xxx色综合一区| 欧美成人免费观看| 亚洲天堂偷拍| 男人的天堂亚洲在线| 国产精品久久福利| 亚洲高清在线精品| 欧美一进一出视频| 亚洲国产精品精华液2区45| 亚洲午夜精品一区二区三区他趣| 久久精品国产免费| 国产精品福利网| 亚洲国产视频一区| 久久成人综合视频| 99国产精品自拍| 久久中文字幕一区| 国产精品夜夜夜| 一区二区久久| 免费亚洲电影在线观看| 在线视频一区二区| 欧美人与性动交cc0o| 在线观看欧美日韩| 久久精品一区二区三区不卡| 夜夜爽www精品| 欧美精品成人|