如何让两个线程相互调用?

gauzxj 2008-04-17 12:21:34
 Thread th;
Thread th1;
public test()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
th1 = new Thread(new ThreadStart(bbb));
th = new Thread(new ThreadStart(test1));
th.Priority = ThreadPriority.Normal;
th.Start();
}
private void bbb()
{
MessageBox.Show("ttttttttttttttt");
th.Resume();
th1.Suspend();
}
private void test1()
{
MessageBox.Show("test1");
if (th1.ThreadState == ThreadState.Unstarted)
{
th1.Start();
}
else
{
th1.Resume();
}

th.Suspend();

}

private void button2_Click(object sender, EventArgs e)
{
MessageBox.Show(th.ThreadState.ToString());
}

我点击button1之后 th.ThreadState 状态变成了stopped了

为什么?
...全文
197 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
gauzxj 2008-04-17
  • 打赏
  • 举报
回复
在线等待 自己顶一下!!!!!!!!!!!!!
gauzxj 2008-04-17
  • 打赏
  • 举报
回复
期待高手回答!!!!!!!!!!!!
wudichong 2008-04-17
  • 打赏
  • 举报
回复
Mark
gauzxj 2008-04-17
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 zhujiechang 的回复:]
因为th已经执行完毕了,当然stop了
[/Quote]

我在执行th1的时候不是把th.Resume();了吗?

按照正常的话 应该不断的弹出窗口才对的 但是并没有
zhujiechang 2008-04-17
  • 打赏
  • 举报
回复
因为th已经执行完毕了,当然stop了
gauzxj 2008-04-17
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 viena 的回复:]
LZ思维混乱~
[/Quote]

怎么说????
viena 2008-04-17
  • 打赏
  • 举报
回复
LZ思维混乱~
懒牛科技 2008-04-17
  • 打赏
  • 举报
回复
顶,顺便学习
gauzxj 2008-04-17
  • 打赏
  • 举报
回复
怎么没人回答??
财富实验室 2008-04-17
  • 打赏
  • 举报
回复
搂住是该考虑换种方式了。
那样处理逻辑就有点乱。再者通过状态控制,不太好。
wenbin 2008-04-17
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 qiqihua 的回复:]
MSDN中说的在任何情况下都不应使用线程状态来同步线程的活动
[/Quote]

可以使用事件或者别的东西实现

如果是学习的话,看它执行到哪,可以使用
Debug类,去把信息写到调试窗口上
yilanwuyu123 2008-04-17
  • 打赏
  • 举报
回复
互相调用要用进程锁的吧 要不死锁了怎么办
honkerhero 2008-04-17
  • 打赏
  • 举报
回复
肯定是执行完了,你往test1里加个大数量级循环,再试试看
larry_jun 2008-04-17
  • 打赏
  • 举报
回复
c#要解决循环嵌套调用的问题可以用委托来解决,你的那也可以类似如此的...
qiqihua 2008-04-17
  • 打赏
  • 举报
回复
MSDN中说的在任何情况下都不应使用线程状态来同步线程的活动
gauzxj 2008-04-17
  • 打赏
  • 举报
回复
没解决!!!
bingshan24 2008-04-17
  • 打赏
  • 举报
回复
学习一下..线程的问题比较麻烦..~
virusswb 2008-04-17
  • 打赏
  • 举报
回复
要用委托吧,否则会提示线程不安全
gauzxj 2008-04-17
  • 打赏
  • 举报
回复
谢谢楼上 我试试!
fengsfx 2008-04-17
  • 打赏
  • 举报
回复
using System;
using System.Threading;

// Simple threading scenario: Start a static method running
// on a second thread.
public class ThreadExample {
// The ThreadProc method is called when the thread starts.
// It loops ten times, writing to the console and yielding
// the rest of its time slice each time, and then ends.
public static void ThreadProc() {
for (int i = 0; i < 10; i++) {
Console.WriteLine("ThreadProc: {0}", i);
// Yield the rest of the time slice.
Thread.Sleep(0);
}
}

public static void Main() {
Console.WriteLine("Main thread: Start a second thread.");
// The constructor for the Thread class requires a ThreadStart
// delegate that represents the method to be executed on the
// thread. C# simplifies the creation of this delegate.
Thread t = new Thread(new ThreadStart(ThreadProc));
// Start ThreadProc. On a uniprocessor, the thread does not get
// any processor time until the main thread yields. Uncomment
// the Thread.Sleep that follows t.Start() to see the difference.
t.Start();
//Thread.Sleep(0);

for (int i = 0; i < 4; i++) {
Console.WriteLine("Main thread: Do some work.");
Thread.Sleep(0);
}

Console.WriteLine("Main thread: Call Join(), to wait until ThreadProc ends.");
t.Join();
Console.WriteLine("Main thread: ThreadProc.Join has returned. Press Enter to end program.");
Console.ReadLine();
}
}

输出结果: Main thread: Start a second thread.
Main thread: Do some work.
ThreadProc: 0
Main thread: Do some work.
ThreadProc: 1
Main thread: Do some work.
ThreadProc: 2
Main thread: Do some work.
ThreadProc: 3
Main thread: Call Join(), to wait until ThreadProc ends.
ThreadProc: 4
ThreadProc: 5
ThreadProc: 6
ThreadProc: 7
ThreadProc: 8
ThreadProc: 9
Main thread: ThreadProc.Join has returned. Press Enter to end program.

好好理解下下面的话:
// The constructor for the Thread class requires a ThreadStart
// delegate that represents the method to be executed on the
// thread. C# simplifies the creation of this delegate.
加载更多回复(1)

110,533

社区成员

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

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

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