分享
 
 
 

在.NET中的线程处理(3)

王朝c#·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

使用异步 I/O 完成事件,线程池中的线程将只在收到数据时对数据进行处理,一旦处理完数据,该线程就会返回到线程池中。

若要进行异步 I/O 调用,必须将操作系统 I/O 句柄与线程池相关联,并且必须指定一个回调方法。当 I/O 操作完成后,线程池中的线程将调用该回调方法。

下面的 C# 代码示例说明了一个简单的异步 I/O 操作。

注意 该示例需要 100MB 以上的可用内存。

[C#]

using System;

using System.IO;

using System.Threading;

using System.Runtime.InteropServices;

public class BulkImageProcAsync{

public const String ImageBaseName = "tmpImage-";

public const int numImages = 200;

public const int numPixels = 512*512;

// ProcessImage has a simple O(N) loop, and you can vary the number

// of times you repeat that loop to make the application more CPU-bound

// or more I/O-bound.

public static int processImageRepeats = 20;

// Threads must decrement NumImagesToFinish, and protect

// their access to it through a mutex.

public static int NumImagesToFinish = numImages;

public static Object NumImagesMutex = new Object[0];

// WaitObject is signalled when all image processing is done.

public static Object WaitObject = new Object[0];

public class ImageStateObject{

public byte[] pixels;

public int imageNum;

public FileStream fs;

}

public static void MakeImageFiles(){

int sides = (int) Math.Sqrt(numPixels);

Console.Write("Making "+numImages+" "+sides+"x"+sides+" images... ");

byte[] pixels = new byte[numPixels];

for(int i=0; i<numPixels; i++)

pixels[i] = (byte) i;

for(int i=0; i<numImages; i++){

FileStream fs = new FileStream(

ImageBaseName+i+".tmp",

FileMode.Create,

FileAccess.Write,

FileShare.None,

8192,

false

);

fs.Write(pixels, 0, pixels.Length);

FlushFileBuffers(fs.Handle);

fs.Close();

}

Console.WriteLine("Done.");

}

public static void ReadInImageCallback(IAsyncResult asyncResult){

ImageStateObject state = (ImageStateObject) asyncResult.AsyncState;

Stream stream = state.fs;

int bytesRead = stream.EndRead(asyncResult);

if (bytesRead != numPixels)

throw new Exception("In ReadInImageCallback, got wrong number of bytes from the image! got: " + bytesRead);

ProcessImage(state.pixels, state.imageNum);

stream.Close();

// Now write out the image. No async here.

FileStream fs = new FileStream(

ImageBaseName + state.imageNum + ".done",

FileMode.Create,

FileAccess.Write,

FileShare.None,

4096,

false

);

fs.Write(state.pixels, 0, numPixels);

fs.Close();

// This application model uses too much memory.

// Releasing memory as soon as possible is a good idea, especially global

// state.

state.pixels = null;

// Record that an image is done now.

lock(NumImagesMutex){

NumImagesToFinish--;

if (NumImagesToFinish==0){

lock(WaitObject){

Monitor.Pulse(WaitObject);

}

}

}

}

public static void ProcessImage(byte[] pixels, int imageNum){

Console.WriteLine("ProcessImage "+imageNum);

// Do some CPU-intensive operation on the image.

for(int i=0; i<processImageRepeats; i++)

for(int j=0; j<numPixels; j++)

pixels[j] += 1;

Console.WriteLine("ProcessImage "+imageNum+" done.");

}

public static void ProcessImagesInBulk(){

Console.WriteLine("Processing images... ");

long t0 = Environment.TickCount;

NumImagesToFinish = numImages;

AsyncCallback readImageCallback = new AsyncCallback(ReadInImageCallback);

for(int i=0; i<numImages; i++){

ImageStateObject state = new ImageStateObject();

state.pixels = new byte[numPixels];

state.imageNum = i;

// Very large items are read only once, so the

// buffer on the file stream can be very small to save memory.

FileStream fs = new FileStream(

ImageBaseName+i+".tmp",

FileMode.Open,

FileAccess.Read,

FileShare.Read,

1,

true

);

state.fs = fs;

fs.BeginRead(state.pixels, 0, numPixels, readImageCallback, state);

}

// Determine whether all images are done being processed.

// If not, block until all are finished.

bool mustBlock = false;

lock (NumImagesMutex){

if (NumImagesToFinish > 0)

mustBlock = true;

}

if (mustBlock){

Console.WriteLine(

"All worker threads are queued... Blocking until they complete. numLeft: " + NumImagesToFinish

);

lock(WaitObject){

Monitor.Pulse(WaitObject);

}

}

long t1 = Environment.TickCount;

Console.WriteLine("Total time processing images: {0} ms", (t1-t0));

}

public static void Cleanup(){

for(int i=0; i<numImages; i++){

File.Delete(ImageBaseName+i+".tmp");

File.Delete(ImageBaseName+i+".done");

}

}

public static void TryToClearDiskCache(){

// Try to force all pending writes to disk, and clear the

// disk cache of any data.

byte[] bytes = new byte[100*(1<<20)];

for(int i=0; i<bytes.Length; i++)

bytes[i] = 0;

bytes = null;

GC.Collect();

Thread.Sleep(2000);

}

public static void Main(String[] args){

Console.WriteLine("Bulk image processing sample application, using asynchronous I/O");

Console.WriteLine("Simulates applying a simple transformation to "+numImages+" \"images\"");

Console.WriteLine("(ie, Async FileStream & Threadpool benchmark)");

Console.WriteLine("Warning - this test requires "+(numPixels * numImages * 2)+" bytes of tmp space");

if (args.Length==1){

processImageRepeats = Int32.Parse(args[0]);

Console.WriteLine("ProcessImage inner loop - "+processImageRepeats);

}

MakeImageFiles();

TryToClearDiskCache();

ProcessImagesInBulk();

Cleanup();

}

[DllImport("KERNEL32", SetLastError=true)]

private static extern void FlushFileBuffers(IntPtr handle);

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有