关于多线程问题(新开启的线程里面再开启线程)

d120759632 2014-01-09 01:48:00
先说下流程:首先运行主方法MainFun()这个方法会开启5个线程,每个线程调用DoSomething这方法,在DoSomething这个方法里面又会再开启线程(可能会开启多个),然后分别调用A、B、C、D、E5个方法。
现在的问题是,我测试的时候,我用一个按钮的单击事件调用MainFun(),发现有时候程序会崩溃(什么错都不报直接关闭了),有时候正常运行,如果我按钮点快了是100%程序会崩溃的。求解决,多线程不会,改怎么解决,还是我写的有问题?主要代码如下:

private void MainFun()
{
string[] chanArr = new string[] { "A", "B", "C", "D", "E" };
for (int i = 0; i < chanArr.Length; i++)
{
Thread th = new Thread(new ParameterizedThreadStart(DoSomething));
th.Start(chanArr[i]);
}
}
private void DoSomething(object nodeName)
{
List<Dictionary<string, string>> list = new List<Dictionary<string, string>>();
list = ReadFromXml(nodeName.ToString());
if (list.Count <= 0)
return;
for (int i = 0; i < list.Count; i++)
{
string chanId = list[i]["ChanId"];
string param = list[i]["param"];
ParameterizedThreadStart pts = null;
if (chanId == "9")
pts = new ParameterizedThreadStart(A);
if (chanId == "521")
pts = new ParameterizedThreadStart(B);
if (chanId == "525")
pts = new ParameterizedThreadStart(C);
if (chanId == "526")
pts = new ParameterizedThreadStart(D);
if (chanId == "532")
pts = new ParameterizedThreadStart(E);
Thread th = new Thread(pts);
th.Start(param);
}
}
private void A(object param)
{
}
private void B(object param)
{
}
private void C(object param)
{
}

private void D(object param)
{
}
private void E(object param)
{
}

...全文
1685 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
qldsrx 2014-01-09
  • 打赏
  • 举报
回复
检查你多线程操作内部是否有UI(界面)的操作,是否都委托回调了。对于公共列表操作是否有安全性的锁定。 你给的显然不是实际代码,问题部分已经被你隐藏掉了,仅开启线程并执行空方法,100%不会出错。
飞小猪 2014-01-09
  • 打赏
  • 举报
回复
加上日志输出吧,或者用messagebox把catch里的东西show出来看看。 我能告诉你的是,线程开多了程序不会直接崩溃退出,线程多了顶多是新开的线程不会运行,会处于等待状态,等有线程结束了,才会运行
human_2000 2014-01-09
  • 打赏
  • 举报
回复
不可能,你把开发工具的”输出“打开看看,正常退出和异常退出肯定有区别的

d120759632 2014-01-09
  • 打赏
  • 举报
回复
引用 2 楼 human_2000 的回复:
把每个函数都加上 try{ } catch(Exception ) { Console.WriteLine(ex.messge); } 然后在每句 Console.WriteLine(ex.messge);设置断点,看看到底是什么原因报错
事实证明不行,点了按钮之后程序没任何报错 直接就关闭了。就行正常的关闭程序一样!
d120759632 2014-01-09
  • 打赏
  • 举报
回复
引用 2 楼 human_2000 的回复:
把每个函数都加上 try{ } catch(Exception ) { Console.WriteLine(ex.messge); } 然后在每句 Console.WriteLine(ex.messge);设置断点,看看到底是什么原因报错
好像,有点道理 我试试!
d120759632 2014-01-09
  • 打赏
  • 举报
回复
引用 3 楼 kenfinal 的回复:
for (int i = 0; i < list.Count; i++) { string chanId = list[i]["ChanId"]; string param = list[i]["param"]; ParameterizedThreadStart pts = null; if (chanId == "9") pts = new ParameterizedThreadStart(A); if (chanId == "521") pts = new ParameterizedThreadStart(B); if (chanId == "525") pts = new ParameterizedThreadStart(C); if (chanId == "526") pts = new ParameterizedThreadStart(D); if (chanId == "532") pts = new ParameterizedThreadStart(E); Thread th = new Thread(pts); th.Start(param); } 如果pts没赋值怎么办?代码我没测试,不过很可能是这个问题。也可能是每次都开线程,资源耗完了。
pts没赋值可能会报错,我开始可能没考虑这个问题,但是我的chanid是从XML里面读出来的,是100%会有值的,因为值是固定的只有那几种可能。 我在想是不是你说的资源耗尽。如果想解决这个问题就是一个线程执行完了 不用了就关闭是吧,但是现在问题来了:我开启一个线程后会进入下一个方法,在下一个方法里面我如何判断这个线程是不是执行完了呢,也就是说我在下一个方法里面不能调用开启线程的那个变量啊,如果定义成全局的,我是通过一个变量来开启多个的,那又如何判断是某一个方法里的某一个线程执行完了呢?
kenfinal 2014-01-09
  • 打赏
  • 举报
回复
for (int i = 0; i < list.Count; i++) { string chanId = list[i]["ChanId"]; string param = list[i]["param"]; ParameterizedThreadStart pts = null; if (chanId == "9") pts = new ParameterizedThreadStart(A); if (chanId == "521") pts = new ParameterizedThreadStart(B); if (chanId == "525") pts = new ParameterizedThreadStart(C); if (chanId == "526") pts = new ParameterizedThreadStart(D); if (chanId == "532") pts = new ParameterizedThreadStart(E); Thread th = new Thread(pts); th.Start(param); } 如果pts没赋值怎么办?代码我没测试,不过很可能是这个问题。也可能是每次都开线程,资源耗完了。
human_2000 2014-01-09
  • 打赏
  • 举报
回复
把每个函数都加上 try{ } catch(Exception ) { Console.WriteLine(ex.messge); } 然后在每句 Console.WriteLine(ex.messge);设置断点,看看到底是什么原因报错
d120759632 2014-01-09
  • 打赏
  • 举报
回复
大神们呢,求解答!

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧