我的程序共开启了5个线程,其中有四个线程是访问同一个对象
另外一个线程是访问其他对象,请问我如何判断这几个线程已经全部执行完?
由于程序中涉及到其他操作,故不使用线程池,希望大家帮帮忙啊,谢谢了!
另外一个线程是访问其他对象,请问我如何判断这几个线程已经全部执行完?
由于程序中涉及到其他操作,故不使用线程池,希望大家帮帮忙啊,谢谢了!
解决方案 »
- 关于DataSet生存周期,DataGridView能否显示数据??
- 找不到 csc.exe
- 在类里访问session的问题,很多方法都试过,但还是无法访问,请指教
- “xtgl_xtgl_user_select”并不包含“TBryxm”的定义
- 兄弟不才,新学C#,想请教一个数据集的问题!!!!!!!!
- 如何开发一套相对完美的网站流量统计系统?
- C#中Socket多线程问题,求解。
- 如何得到当前函数或程序块的变量快照?就像Visual Studio.net调试器中的局部变量窗口一样。
- C调用C#?
- 编码规范。。var
- try..catch...finally能不能嵌套使用?
- 用过sideBar控件的高手请进!!
具体用法忘记了,不过这个好像是可以的
using System.Threading;class IsThreadPool
{
static void Main()
{
AutoResetEvent autoEvent = new AutoResetEvent(false); Thread regularThread =
new Thread(new ThreadStart(ThreadMethod));
regularThread.Start();
ThreadPool.QueueUserWorkItem(new WaitCallback(WorkMethod),
autoEvent); // Wait for foreground thread to end.
regularThread.Join(); // Wait for background thread to end.
autoEvent.WaitOne();
} static void ThreadMethod()
{
Console.WriteLine("ThreadOne, executing ThreadMethod, " +
"is {0}from the thread pool.",
Thread.CurrentThread.IsThreadPoolThread ? "" : "not ");
} static void WorkMethod(object stateInfo)
{
Console.WriteLine("ThreadTwo, executing WorkMethod, " +
"is {0}from the thread pool.",
Thread.CurrentThread.IsThreadPoolThread ? "" : "not "); // Signal that this thread is finished.
((AutoResetEvent)stateInfo).Set();
}
}
这样,主线程会等到全部的子线程结束后才继续执行
using System.Threading;class IsThreadPool
{
static void Main()
{
AutoResetEvent autoEvent = new AutoResetEvent(false); Thread regularThread = new Thread(new ThreadStart(ThreadMethod));
regularThread.Start(); ThreadPool.QueueUserWorkItem(new WaitCallback(WorkMethod),autoEvent); regularThread.Join();
autoEvent.WaitOne();
} static void ThreadMethod()
{
Console.WriteLine("ThreadOne, executing ThreadMethod, " + "is {0}from the thread pool.",
Thread.CurrentThread.IsThreadPoolThread ? "" : "not ");
} static void WorkMethod(object stateInfo)
{
Console.WriteLine("ThreadTwo, executing WorkMethod, " + "is {0}from the thread pool.",
Thread.CurrentThread.IsThreadPoolThread ? "" : "not "); // Signal that this thread is finished.
((AutoResetEvent)stateInfo).Set();
}
}
for(int i=0;i<5;i++)
{
Thread th=new Thread(
()=>{
线程体;
if(++count==5)...});
th.Start();
}
1.保存创建线程时返回的线程句柄。
2.在线程结束之前(线程return之前的一条语句),向主窗口发送一条线程结束的消息,将线程的标识通过wParam参数传递过去。主窗口在处理这条消息时,将相应线程句柄变量关闭(CloseHandle),并将相应变量值为NULL。
3.在需要判断线程是否结束的地方,只需要判断相应线程句柄变量是否为空即可
manuls[i].Reset();//线程起始
..子线程代码
manual[i].Set();//线程结束
主线程添加如下:
manuls.WaitAll();...线程结束后的代码.
弄个Bool数组,里面放5个false,当每个线程结束的时候将数组里的一个bool值写成true
主线程循环判断这个数组里的5个值是否都是true了,是就是都执行完了,不是就等待。
每个线程执行完成时,使用 Interlocked.Increment (Int32) 计数。
推荐采用20楼的方法。
using System.Threading;class IsThreadPool
{
static void Main()
{
AutoResetEvent autoEvent = new AutoResetEvent(false); Thread regularThread = new Thread(new ThreadStart(ThreadMethod));
regularThread.Start(); ThreadPool.QueueUserWorkItem(new WaitCallback(WorkMethod),autoEvent); regularThread.Join();
autoEvent.WaitOne();
} static void ThreadMethod()
{
Console.WriteLine("ThreadOne, executing ThreadMethod, " + "is {0}from the thread pool.",
Thread.CurrentThread.IsThreadPoolThread ? "" : "not ");
} static void WorkMethod(object stateInfo)
{
Console.WriteLine("ThreadTwo, executing WorkMethod, " + "is {0}from the thread pool.",
Thread.CurrentThread.IsThreadPoolThread ? "" : "not "); // Signal that this thread is finished.
((AutoResetEvent)stateInfo).Set();
}
}
foreach(Thread thread in threads)
{
thread.Join();
}
{
ManualResetEvent[] manuls = new ManualResetEvent[5];
for (int i = 0;i < manuls.Length;i++)
{
manuls[i] = new ManualResetEvent(false);
Thread thread = new Thread(new ParameterizedThreadStart(ChildThread));
thread.Start(manuls[i]);
}
WaitHandle.WaitAll(manuls);
//主过程的后续工作
}
void ChildThread(object obj)
{
ManualResetEvent muanl = obj as ManualResetEvent;
muanl.Reset();
//子过程 muanl.Set();
}
ASP.NET技术开发平台:http://www.singletowm.com