[英]multithreading in c# compact framework
我正在为Windows Mobile应用程序使用紧凑型框架,在该应用程序中我已将多个请求传递给服务器,并收到数组中每个请求的响应。
问题是当我应该访问这些数组时,因为我在for循环中启动了线程,而在完成循环之后,我必须访问这些数组。
我非常困惑,我怎么知道并且所有线程都已经完成,所以我开始对这些数组进行处理
需要尽快提供帮助。 请。
这个怎么样:
private readonly object syncRoot = new object();
private object[] responses;
public void StartThreads(int numberOfThreads)
{
this.responses = new object[numberOfThreads];
List<Thread> threads = new List<Thread>();
for (int i = 0; i < numberOfThreads; ++i)
{
Thread thread = new Thread(this.DoWork);
thread.Name = i.ToString();
threads.Add(thread);
thread.Start();
}
foreach (Thread thread in threads)
{
thread.Join();
}
// all threads are done.
}
private void DoWork()
{
// do web call
// object response = web.GetResponse();
int threadNumber = Convert.ToInt32(Thread.CurrentThread.Name);
lock (this.syncRoot)
{
this.responses[threadNumber] = response;
}
}
//Declare this in class
public delegate void delege();
//Write this lines when you want to background thread start
Thread thread = new Thread(new ThreadStart(() => {
//Do what you what with backgorund threading , can not use any interface comand here
BeginInvoke(new delege(() => {
//Do here any main thread thread job,this can do interface and control jobs without any error
}));
}));
thread.Start();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.