winform程序 多个线程运行,当挂起一个线程,其它也停止运行,为什么呢?

gidiyi 2010-07-13 12:41:37
 
if (chatLog.ThreadState.ToString() != "Stopped")
{
chatLog.Suspend();//挂起线程
if (!closing(strLblName))//判断是否继续,如果是false,继续执行线程
chatLog.Resume();
else
chatLog.Suspend();
}

break;


我的程序里有5个线程,chatLog 是其中一个线程的对象名,我在窗体上挂起其中的一个线程,那么其余的4个线程也都停止执行了,当挂起的线程继续执行的话其它的线程也会继续执行,请大家给我指导一下为什么呢?,谢谢!
我想实现的是当我挂起一个线程不会影响其它的线程执行
...全文
441 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
xinkjack 2010-07-14
  • 打赏
  • 举报
回复
List4.ThreadState==ThreadState.Stopped
看看是否是这个判断问题
zhengqian529 2010-07-14
  • 打赏
  • 举报
回复
能否贴下你代码里面最重要的部分:staticClass.methed1
gidiyi 2010-07-14
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 zxp8819 的回复:]
貌似没错

有无什么异常?
[/Quote]

以上代码 在运行的时候都互不影响,但是当我挂起其中一个线程(比如挂起List1线程)的时候,其它的线程也被挂起了,但是当继续运行被挂起的线程(List1)的时候,其它的线程也都又开始运行了。这个就是郁闷的地方。
铛铛 2010-07-14
  • 打赏
  • 举报
回复
貌似没错

有无什么异常?
kyhude 2010-07-14
  • 打赏
  • 举报
回复
挂起线程之前,先等待方法 MsgAppend 执行完
ztenv 2010-07-14
  • 打赏
  • 举报
回复
调用公共方法MsgAppend把每个方法运行的详细步骤显示在richtextbox控件里,
是因为这个,你在线程中操作了UI控件导致的,在更新UI控件之前,最好同步一下,使用:
if(control.InvokeRequired)
{
....
}
else
{
直接更新;
}

网上有很多这样的例子;
gidiyi 2010-07-14
  • 打赏
  • 举报
回复
楼上的几位同志,我的每个方法里现在都是一个循环,调用公共方法MsgAppend把每个方法运行的详细步骤显示在richtextbox控件里,还是那个问题为什么挂起一个线程,其它的也停止运行呢??期待高手解决,谢谢

public static void methed1(object para)
{
for (int i = 0; i < 100; i++)
{
Console.WriteLine("运行了methed1方法 " + i);
API f = para as API;
f.MsgAppend("运行了" + i + "次", Color.Khaki);
}
}

public static void methed2(object para)
{
for (int i = 0; i < 100; i++)
{
Console.WriteLine("运行了methed2方法 " + i);
API f = para as API;
f.MsgAppend("运行了" + i + "次", Color.Khaki);
}
}

/// <summary>
/// 及时在窗体上现在程序的运行状态
/// </summary>
/// <param name="strMsg"></param>
/// <param name="c"></param>
public void MsgAppend(string strMsg, Color c)
{
this.BeginInvoke(new MethodInvoker(delegate()
{
int selectStart = rchtxt1.TextLength;
rchtxt1.AppendText(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " " + strMsg + Environment.NewLine);
rchtxt1.Select(selectStart, rchtxt1.TextLength - 1);
rchtxt1.SelectionColor = c;
}));
}
kyhude 2010-07-14
  • 打赏
  • 举报
回复
staticClass.methed1
staticClass.methed2
staticClass.methed3
staticClass.methed4
这些方法会不会相互干扰?比如操作同一个变量
hsghxm 2010-07-13
  • 打赏
  • 举报
回复
1.设置线程的级别
2.线程的创建不要基于主线程
gidiyi 2010-07-13
  • 打赏
  • 举报
回复

public partial class API : Form
{
Thread List1;
Thread List2;
Thread List3;
Thread List4;
//窗体加载
public TaoBaoAPI()
{
InitializeComponent();
List1= new Thread(new ParameterizedThreadStart(staticClass.methed1));
List2= new Thread(new ParameterizedThreadStart(staticClass.methed2));
List3= new Thread(new ParameterizedThreadStart(staticClass.methed3));
List4= new Thread(new ParameterizedThreadStart(staticClass.methed4));
}
/// <summary>
/// linklabel 【运行】的单击事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void linklabelRun_Click(object sender,EventArgs e)
{
string name = ((LinkLabel)sender).Name;
switch (name)
{
case "lnklbl1":
List1.IsBackground = true;
List1.Start(this);
break;
case "lnklbl2":
List2.IsBackground = true;
List2.Start(this);
break;

case "lnklbl3":
List3.IsBackground = true;
List3.Start(this);
break;

case "lnklbl4":
List4.IsBackground = true;
List4.Start(this);
break;
}
}

/// <summary>
/// linklabel 【暂停】的单击事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void linklabelStop_Click(object sender, EventArgs e)
{
string strLblName = ((LinkLabel)sender).Name.ToString();
switch (strLblName)
{
case "lnklbl1_1":
if (List1.ThreadState.ToString() != "Stopped")
{
List1.Suspend();//挂起线程
if (!closing(strLblName))//判断是否继续,如果是false,继续执行线程
List1.Resume();
else
List1.Suspend();
}
break;
case "lnklbl1_2":
if (List2.ThreadState.ToString() != "Stopped")
{
List2.Suspend();//挂起线程
if (!closing(strLblName))//判断是否继续,如果是false,继续执行线程
List2.Resume();
else
List2.Suspend();
}
break;
case "lnklbl1_3":
if (List3.ThreadState.ToString() != "Stopped")
{
List3.Suspend();//挂起线程
if (!closing(strLblName))//判断是否继续,如果是false,继续执行线程
List3.Resume();
else
List3.Suspend();
}
break;
case "lnklbl1_4":
if (List4.ThreadState.ToString() != "Stopped")
{
List4.Suspend();//挂起线程
if (!closing(strLblName))//判断是否继续,如果是false,继续执行线程
List4.Resume();
else
List4.Suspend();
}
break;
}
}

private bool closing(string threadName)
{
DialogResult result = MessageBox.Show("你确认要【暂停】该程序吗?", "暂停提示", MessageBoxButtons.YesNo, MessageBoxIcon.Hand);
if (result == DialogResult.Yes && threadName == "lnklbl1_1")
{
lblWait1.Text = "程序已暂停。";
return true;
}
if (result == DialogResult.Yes && threadName == "lnklbl1_2")
{
lblWait2.Text = "程序已暂停。";
return true;
}
if (result == DialogResult.Yes && threadName == "lnklbl1_3")
{
lblWait3.Text = "程序已暂停。";
return true;
}
if (result == DialogResult.Yes && threadName == "lnklbl1_4")
{
lblWait4.Text = "程序已暂停。";
return true;
}
}


大体的逻辑就是这样的,大家给我指导一下吧啊,尽量能帮我用代码的形式解释,谢谢了
g505149841 2010-07-13
  • 打赏
  • 举报
回复
贴代码。。。
husanbo110 2010-07-13
  • 打赏
  • 举报
回复
帮顶 哥也不晓得
铛铛 2010-07-13
  • 打赏
  • 举报
回复
贴出你的code
allen3010 2010-07-13
  • 打赏
  • 举报
回复
关注中
a184485789 2010-07-13
  • 打赏
  • 举报
回复
线程挂起这种方法不行,问题多,最好用互斥量
ztenv 2010-07-13
  • 打赏
  • 举报
回复
贴一下你线程相关的完整代码,线程函数,几个线程创建的代码,让线程睡觉的代码;
颤菊大师 2010-07-13
  • 打赏
  • 举报
回复
其他线程是在哪里创建的(哪里 new 的)?
air123456789 2010-07-13
  • 打赏
  • 举报
回复
我是来学习的。学了这么久 线程是什么意思都不知道呢。
gidiyi 2010-07-13
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 hsghxm 的回复:]
1.设置线程的级别
2.线程的创建不要基于主线程
[/Quote]

hsghxm 这位仁兄,能不能给点详细的指导或者例子呢??我对线程这一块不是很熟悉

110,533

社区成员

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

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

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