程序执行生效时间的问题

yunhaiC QQ654777694 2009-04-29 12:05:18
private Thread th = null;

private void button2_MouseDown(object sender,MouseEventArgs e)
{
if(checkBox1.Checked)
{
write_filestream();
}
else
m_PCNet.Set(3523,88);


th = new Thread(new ThreadStart(step_for)); //step_for不执行
th.Start();
}

public void write_filestream()
{
byte[] byData;
char[] charData;
FileStream fsf = new FileStream("C:\\MyBlock", FileMode.Truncate, FileAccess.Write);
Encoder e = Encoding.UTF8.GetEncoder();

charData = "wahaha".ToCharArray();
byData = new byte[charData.Length];
e.GetBytes(charData, 0, charData.Length, byData, 0, true);

fsf.Seek(0, SeekOrigin.Begin);
fsf.Write(byData, 0, byData.Length);
}
问题一:
我的write_filestream()方法向MyBlock里面写"wahaha"字符串,MyBlock里面有"wahaha"才算成功,记为bool blSuccess = ture;初始值
blSucess = false,但是我step_for()的执行里面有一个条件必须在blSuccess = true的条件下才开始执行
问题就来了,虽然write_filestream()是写在step_for()之前的,但是成功执行有的时候却在step_for()以后才生效,请问怎么解决?

问题二:
没有问题一的前提,问题二是孤立的问题。我的step_for()为什么不执行,昨天我已经问过了,没有得到答案,请问怎么解决

...全文
92 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
Garnett_KG 2009-04-29
  • 打赏
  • 举报
回复

private Thread th = null;
private AutoResetEvent ev = null;
private void button2_MouseDown(object sender, MouseEventArgs e)
{
ev = new AutoResetEvent(false);//开始时没有信号

if (checkBox1.Checked)
{
write_filestream();
}
else
m_PCNet.Set(3523, 88);


th = new Thread(new ThreadStart(step_for));
th.Start();
}
public void step_for()
{
ev.WaitOne(); //等待信号的发生,在信号没有到达之前,下面的语句是不会执行的.
.....
}
public void write_filestream()
{
byte[] byData;
char[] charData;
FileStream fsf = new FileStream("C:\\MyBlock", FileMode.Truncate, FileAccess.Write);
Encoder e = Encoding.UTF8.GetEncoder();

charData = "wahaha".ToCharArray();
byData = new byte[charData.Length];
e.GetBytes(charData, 0, charData.Length, byData, 0, true);

fsf.Seek(0, SeekOrigin.Begin);
fsf.Write(byData, 0, byData.Length);

ev.Set(); //write_filestream执行成功,则发送信号.
}


Garnett_KG 2009-04-29
  • 打赏
  • 举报
回复
这个问题用信号量解决比较适合.
llsen 2009-04-29
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 llsen 的回复:]
引用 4 楼 yunhaiC 的回复:
引用 1 楼 llsen 的回复:
你这两个问题正好应该何在以前了

看看你的逻辑
你定义了一个线程th = new Thread(new ThreadStart(step_for)); //step_for不执
然后start,然后按钮事件里面有一个主线程

你等于操作了两个线程
系统操作多线程有个时间片,
因为同一时刻cpu只能执行一个线程

主线程执行到write_filestrea时候,系统执行了th这个线程
这个时候其实bool blSuccess 还为…
[/Quote]

优先级设为最低,或者先将这个线程挂起,等主线程执行完,再接着执行
但是这样的话,除非你再操作其他的,让这个线程在后台执行,不然的话,你多线程的用途就没体现出来
llsen 2009-04-29
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 yunhaiC 的回复:]
引用 1 楼 llsen 的回复:
你这两个问题正好应该何在以前了

看看你的逻辑
你定义了一个线程th = new Thread(new ThreadStart(step_for)); //step_for不执
然后start,然后按钮事件里面有一个主线程

你等于操作了两个线程
系统操作多线程有个时间片,
因为同一时刻cpu只能执行一个线程

主线程执行到write_filestrea时候,系统执行了th这个线程
这个时候其实bool blSuccess 还为false,因为write_filestrea没…
[/Quote]

把他的优先级设为最低试试
让主线程先执行
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 llsen 的回复:]
你这两个问题正好应该何在以前了

看看你的逻辑
你定义了一个线程th = new Thread(new ThreadStart(step_for)); //step_for不执
然后start,然后按钮事件里面有一个主线程

你等于操作了两个线程
系统操作多线程有个时间片,
因为同一时刻cpu只能执行一个线程

主线程执行到write_filestrea时候,系统执行了th这个线程
这个时候其实bool blSuccess 还为false,因为write_filestrea没执行,没将bool值更新为true。导致你感…
[/Quote]
那请问我该怎么解决这个问题,前提是我需要用th = new Thread(new ThreadStart(step_for));这个线程
llsen 2009-04-29
  • 打赏
  • 举报
回复
用多线程
然后代码的执行顺序不是按照你写的上下这样执行的

默认的是开启一个主线程
然后你重新定义一个新的线程的话

两个线程是同时执行的 (并发)
烈火蜓蜻 2009-04-29
  • 打赏
  • 举报
回复
路过看看
llsen 2009-04-29
  • 打赏
  • 举报
回复
你这两个问题正好应该何在以前了

看看你的逻辑
你定义了一个线程th = new Thread(new ThreadStart(step_for)); //step_for不执
然后start,然后按钮事件里面有一个主线程

你等于操作了两个线程
系统操作多线程有个时间片,
因为同一时刻cpu只能执行一个线程

主线程执行到write_filestrea时候,系统执行了th这个线程
这个时候其实bool blSuccess 还为false,因为write_filestrea没执行,没将bool值更新为true。导致你感觉step_for方法没执行,其实是执行了的。然后write_filestrea其实也是在step_for之后执行的

多线程操作不是按照你代码写的先后顺序执行的
建议楼主多看看多线程基础

你如果只是从上倒下执行
又开启一个线程干嘛那

111,126

社区成员

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

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

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