C# 线程问题

问题描述

各位大鸟,有个线程问题困扰我好久啦,请大鸟指导,感谢!有个相机类,里面声明啦一个类,一个委托,一个信号。然后实例化啦2个相机,对应的线程也是2个,对应的2个委托同时指向啦个方法msg(),想让2个相机的Grab()方法都执行完后,在循环下一轮。现在的问题是,只执行啦一轮,就卡在哪里啦!,并且执行啦3次,看图片,感觉AutoResetEvent.WaitAll(revents);没有用!///这是一个相机累publicclassCamera{privateThreadcamthread;//创建一个类publicAutoResetEventaevent=newAutoResetEvent(false);//创建个信号publicAction<Camera>Amsg;//创建一个委托,用于结果输出publicstringresult="";publicstringName{get;set;}publicCamera(stringName){this.Name=Name;camthread=newThread(Grab);camthread.Name=this.Name;}publicvoidRun(){camthread.Start();}publicvoidstop(){camthread.Abort();}publicvoidGrab(){intcount=0;while(true){count++;if(count%5==0){result="OK";Thread.Sleep(50);}else{result="NG";Thread.Sleep(50);}aevent.Set();//执行到这里信号打开Amsg(this);//委托值传出}}}}//调用publicpartialclassForm1:Form{publicForm1(){InitializeComponent();}staticintcamcount=2;//定义相机个数Camera[]cam=newCamera[camcount];//定义相机数组AutoResetEvent[]revents=newAutoResetEvent[camcount];//定义信号数组privatevoidForm1_Load(objectsender,EventArgse){for(inti=0;i<cam.Length;i++){cam[i]=newCamera("CAM"+i);//相机实例化revents[i]=newAutoResetEvent(false);//信号实例化为关revents[i]=cam[i].aevent;//信号对应赋值cam[i].Amsg+=msg;//委托指向msg}}///<summary>///运行所有相机///</summary>///<paramname="sender"></param>///<paramname="e"></param>privatevoidbutton1_Click(objectsender,EventArgse){for(inti=0;i<cam.Length;i++){cam[i].Run();}}intiii=0;voidmsg(CameraT)//____________________委托指向{iii++;Invoke(newAction(()=>{richTextBox1.AppendText(T.Name+"="+T.result+"nr");}));AutoResetEvent.WaitAll(revents);//等待信号都有的时候,进行下一轮工作//<<<<<<<<<<<---问题点在这里,为什么二个线程只跑了一次就停止了???????????????stringaa=iii.ToString();}privatevoidbutton2_Click(objectsender,EventArgse){for(inti=0;i<cam.Length;i++){cam[i].stop();}}}}

解决方案

解决方案二:
卡在哪里?你不会进入调试吗?或者直接暂停运行看一看?
解决方案三:
你的事件都是AutoResetEvent。因此,当多个Camera调用msg,并间接调用WaitAll的时候:cam0调用AutoResetEvent.WaitAll(revents)cam1调用AutoResetEvent.WaitAll(revents)cam2调用AutoResetEvent.WaitAll(revents)3个WaitAll只能有一个成功,因为WaitAll把AutoResetEvent的信号量重置了(AutoReset)。其他WaitAll不成功的Camera就此阻塞,再也没有机会进行下一次aevent.Set()。
解决方案四:
每个线程执行一次AutoResetEvent.WaitAll(revents);

这里是不妥当的这行代码可以防止运行相机的for循环之外
解决方案五:
http://blog.csdn.net/qq_17486399/article/details/51210053祝你理解多线程
解决方案六:
跟帖mark一下,学习
解决方案七:
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.Threading;namespaceTest_AutoEvent{publicpartialclassForm1:Form{publicForm1(){InitializeComponent();}staticintcamcount=2;//定义相机个数Camera[]cam=newCamera[camcount];//定义相机数组ManualResetEvent[]revents=newManualResetEvent[camcount];//定义信号数组privatevoidForm1_Load(objectsender,EventArgse){for(inti=0;i<cam.Length;i++){cam[i]=newCamera("CAM"+i);//相机实例化revents[i]=newManualResetEvent(false);//信号实例化为关revents[i]=cam[i].aevent;//信号对应赋值cam[i].Amsg+=msg;//委托指向msg}}///<summary>///运行所有相机///</summary>///<paramname="sender"></param>///<paramname="e"></param>privatevoidbutton1_Click(objectsender,EventArgse){for(inti=0;i<cam.Length;i++){cam[i].Run();//注0,这里有多个cam在一起跑。由于cam[0]比cam[1]先执行,所以输出结果总是cam[0]在cam[1]的前面。}}privateintiii=0;voidmsg(CameraT)//____________________委托指向{iii++;Invoke(newAction(()=>{richTextBox1.AppendText(T.Name+"="+T.result+"nr");}));ManualResetEvent.WaitAll(revents);//等待信号都有的时候,进行下一轮工作//由于cam[i].Run();这里的有多个cam在同时跑,他们执行aevent.Set();是不同时的,等到所有cam都执行了aevent.Set();以后,//这句:ManualResetEvent.WaitAll(revents);才会结束等待,//具体解释,请参见注0-注3。}privatevoidbutton2_Click(objectsender,EventArgse){for(inti=0;i<cam.Length;i++){cam[i].stop();}}}publicclassCamera{privateThreadcamthread;publicManualResetEventaevent=newManualResetEvent(false);publicAction<Camera>Amsg;publicstringresult="";publicstringName{get;set;}publicCamera(stringName){this.Name=Name;camthread=newThread(Grab);camthread.Name=this.Name;}publicvoidRun(){camthread.Start();}publicvoidstop(){camthread.Abort();}publicvoidGrab(){intcount=0;while(true){count++;if(count%5==0){result="OK";Thread.Sleep(50);}else{result="NG";Thread.Sleep(50);}aevent.Set();//注1,由于cam[i].Run();这里的有多个cam在同时跑,他们执行aevent.Set();是不同时的,Amsg(this);//注2,上面虽然执行了set(),但是在这个委托对应的方法里面会停止在:这句:ManualResetEvent.WaitAll(revents);等到所有cam都执行了aevent.Set();以后,才会结束等待。aevent.Reset();//注3,上面执行了waitall,这里要reset(),这句也可以放在aevent.Set();之前。}}}}

其实也可以考虑使用.net4.0的并行计算,用task.waitall,比用这个锁来同步要简单明了得多。
解决方案八:
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.Threading;usingSystem.Collections.Concurrent;usingSystem.Threading.Tasks;namespaceTest_AutoEvent{publicpartialclassForm1:Form{staticintcamcount=5;//定义相机个数Camera[]cam=newCamera[camcount];//定义相机数组privateboolEnableRun=true;Thread_thd=null;publicForm1(){InitializeComponent();}privatevoidForm1_Load(objectsender,EventArgse){for(inti=0;i<cam.Length;i++){cam[i]=newCamera("CAM"+i);//相机实例化cam[i]._Act+=DoAction;}_thd=newThread(RunThread);_thd.IsBackground=true;}///<summary>///运行所有相机///</summary>///<paramname="sender"></param>///<paramname="e"></param>privatevoidbutton1_Click(objectsender,EventArgse){if((_thd.ThreadState&ThreadState.Suspended)!=0){EnableRun=true;_thd.Resume();}else{_thd.Start();}}privatevoidRunThread()//task要放到后台线程里面,否则会出现界面假死,程序无法响应。{while(EnableRun){//tsk01=newTask(cam[0].Grab);//tsk02=newTask(cam[1].Grab);//tsk01=newTask(()=>cam[0].Grab());//tsk02=newTask(()=>cam[1].Grab());或者这样,这两种写法都是一样的。//tsk01.Start();//tsk02.Start();//tsk01=Task.Factory.StartNew(cam[0].Grab);//tsk02=Task.Factory.StartNew(cam[1].Grab);这样写也是可以的。Task[]_tsk=newTask[camcount];for(inti=0;i<camcount;i++){_tsk[i]=newTask(cam[i].Grab);_tsk[i].Start();}Task.WaitAll(_tsk);for(inti=0;i<camcount;i++){_tsk[i].Dispose();_tsk[i]=null;}}}privatevoidbutton2_Click(objectsender,EventArgse){EnableRun=false;_thd.Suspend();}privatevoidDoAction(Camera_cam){this.Invoke(newAction(()=>{richTextBox1.AppendText(_cam.Name+"="+_cam.result+"rn");}));}}publicclassCamera{publicstringresult="";publicstringName{get;set;}publicAction<Camera>_Act;publicCamera(stringName){this.Name=Name;}privateintcount=0;publicvoidGrab(){count++;if(count%5==0){result="OK";Thread.Sleep(50);}else{result="NG";Thread.Sleep(50);}_Act(this);}}}

以上是用task的例子,供你参考。之所以没有按照严格的cam0-cam4来执行,可能是task内部的调度机制决定的。
解决方案九:
..建议你用异步委托去做....线程这里的执行顺序有时候很乱..

时间: 2024-08-31 16:13:14

C# 线程问题的相关文章

新人一枚,请问怎样从B线程调用A线程的内容?

问题描述 新人一枚,请问怎样从B线程调用A线程的内容? 具体要求:写一个线程A,得到0-10的随机数,再写一个线程B来了调用线程A!!!!! 解决方案 int a;//全局变量,如果多个线程同步访问需要加锁 A...//线程A B...//线程B 代码结构大致如上,访问的时候,因为变量a是全局数据,所以在线程A和B中都可以直接访问,就好像C中的任何函数都可以访问和使用全局变量一样 解决方案二: 放是全局变量来交换数据

并发集合(三)使用阻塞线程安全的列表

使用阻塞线程安全的列表 列表(list)是最基本的集合.一个列表中的元素数量是不确定的,并且你可以添加.读取和删除任意位置上的元素.并发列表允许不同的线程在同一时刻对列表里的元素进行添加或删除,而不会产生任何数据不一致的问题. 在这个指南中,你将学习如何在你的并发应用程序中使用阻塞的列表.阻塞列表与非阻塞列表的主要区别是,阻塞列表有添加和删除元素的方法,如果由于列表已满或为空而导致这些操作不能立即进行,它们将阻塞调用的线程,直到这些操作可以进行.Java包含实现阻塞列表的LinkedBlocki

[转载]Linux 线程实现机制分析

  自从多线程编程的概念出现在 Linux 中以来,Linux 多线应用的发展总是与两个问题脱不开干系:兼容性.效率.本文从线程模型入手,通过分析目前 Linux 平台上最流行的 LinuxThreads 线程库的实现及其不足,描述了 Linux 社区是如何看待和解决兼容性和效率这两个问题的.   一.基础知识:线程和进程 按照教科书上的定义,进程是资源管理的最小单位,线程是程序执行的最小单位.在操作系统设计上,从进程演化出线程,最主要的目的就是更好的支持SMP以及减小(进程/线程)上下文切换开

你真的了解:IIS连接数、IIS并发连接数、IIS最大并发工作线程数、应用程序池的队列长度、应用程序池的最大工作进程数 吗?

原文:你真的了解:IIS连接数.IIS并发连接数.IIS最大并发工作线程数.应用程序池的队列长度.应用程序池的最大工作进程数 吗? IIS连接数   一般购买过虚拟主机的朋友都熟悉购买时,会限制IIS连接数,这边先从普通不懂代码用户角度理解IIS连接数 顾名思义即为IIS服务器可以同时容纳客户请求的最高连接数,准确的说应该叫"IIS限制连接数" 这边客户请求的连接内容包括: 1.网站html请求,html中的图片资源,html中的脚本资源,其他需要连接下载的资源等等,任何一个资源的请求

我的代码是怎么进入死循环的,为什么有两个线程运行一会就唤不醒了

问题描述 classResource{privateStringname;privateintcount=1;privatebooleanflag=false;publicsynchronizedvoidset(Stringname){while(flag){notify();try{wait();}catch(InterruptedExceptione){}System.out.println(Thread.currentThread().getName()+"....循环唤醒..."

线程基础之JAVA和C++0x的特性

译文连接   译文地址  译者:衣着时   校对:丁一    (有兴趣参与试译或校对的同学,请加入并发网试译者QQ群:369468545) JAVA特性 JAVA线程通常是一个带有run()方法的java.lang.Thread的子类,然后调用这个子类对象的start()方法.我们之前定义过,数据竞争是因为两个线程同时访问内存单元,在JAVA中,内存单元是一个对象字段或数组元素. 由于JAVA旨在支持运行不受信任代码作为受信任的应用程序的一部分,必须限制不受信任代码的数据争用造成的破坏.因此不允

ios-在背景线程中处理数据

问题描述 在背景线程中处理数据 我写了一方法如下: - (BOOL)shouldDoSomeWork { BOOL result = // here I need do hard work with data in background thread and return result so main thread should wait until the data is calculated and then return result; return result;} 如何实现? 解决方案

线程同步 卖票问题 三个车站卖七张票

问题描述 线程同步 卖票问题 三个车站卖七张票 正确的写法:public class mainclass { public static void main(String[] args) { Train A=new Train(); Train B=new Train(); Train C=new Train(); A.start(); B.start(); C.start(); } } class Train extends Thread{ public static int ticket=7

在非UI线程处理Bitmap

http://my.oschina.net/ryanhoo/blog/88344 译者:Ryan Hoo 来源:https://developer.android.com/develop/index.html 译者按: 在Google最新的文档中,提供了一系列含金量相当高的教程.因为种种原因而鲜为人知,真是可惜!Ryan将会细心整理,将之翻译成中文,希望对开发者有所帮助.         本系列是Google关于展示大Bitmap(位图)的官方演示,可以有效的解决内存限制,更加有效的加载并显示图

基本线程同步(四)在同步代码中使用条件

在同步代码中使用条件 在并发编程中的一个经典问题是生产者与消费者问题,我们有一个数据缓冲区,一个或多个数据的生产者在缓冲区存储数据,而一个或多个数据的消费者,把数据从缓冲区取出. 由于缓冲区是一个共享的数据结构,我们必须采用同步机制,比如synchronized关键字来控制对它的访问.但是我们有更多的限制因素,如果缓冲区是满的,生产者不能存储数据,如果缓冲区是空的,消费者不能取出数据. 对于这些类型的情况,Java在Object对象中提供wait(),notify(),和notifyAll()