源代码如下 public partial class Form1 : Form
{
public static bool started;
public static int result; public Form1()
{
InitializeComponent();
} private void SButton_Click(object sender, EventArgs e)
{
started = true;
Selectit sel = new Selectit();
Thread selthread = new Thread(sel.Startselect);
selthread.Start();
while (selthread.IsAlive)
{
selthread.Suspend();
numlable.Text = result.ToString();
this.Refresh();
selthread.Resume();
if (!started) { selthread.Abort(); }
}
} private void Stop_Click(object sender, EventArgs e)
{
started = false;
}
public class Selectit
{
public void Startselect()
{
while (Form1.started)
{
Random ran = new Random();
result = ran.Next(1, 126);
Thread.Sleep(100);
}
}
}
}
用于学校抽签的一个小程序,不知为何UI在点击开始按钮后就死了,一开始通过使用This.Refresh()让Lable可以刷新,但是停止按钮还是不能使用。BTW,Suspend和Resume两个函数VS一直说不推荐使用,是由于这个导致的么?多线程ui
{
public static bool started;
public static int result; public Form1()
{
InitializeComponent();
} private void SButton_Click(object sender, EventArgs e)
{
started = true;
Selectit sel = new Selectit();
Thread selthread = new Thread(sel.Startselect);
selthread.Start();
while (selthread.IsAlive)
{
selthread.Suspend();
numlable.Text = result.ToString();
this.Refresh();
selthread.Resume();
if (!started) { selthread.Abort(); }
}
} private void Stop_Click(object sender, EventArgs e)
{
started = false;
}
public class Selectit
{
public void Startselect()
{
while (Form1.started)
{
Random ran = new Random();
result = ran.Next(1, 126);
Thread.Sleep(100);
}
}
}
}
用于学校抽签的一个小程序,不知为何UI在点击开始按钮后就死了,一开始通过使用This.Refresh()让Lable可以刷新,但是停止按钮还是不能使用。BTW,Suspend和Resume两个函数VS一直说不推荐使用,是由于这个导致的么?多线程ui
解决方案 »
- this entity of type“---” does not support the edit operation
- 求本段的补完
- 跪求高手帮忙--统计分段数据的问题!!!
- 为什么不能为一个substring赋值??
- 求助:关于datagridview重写的问题
- .net安装程序类中的Uninstall问题
- 高手们快来帮忙阿!
- C# 制作课程表浏览
- 从今天开始,水晶报表将受到来自纺织服装界的强力挑战!我水晶报表很难实现工艺单这一类格式的打印!其格式内详,有兴趣的进来看看,发表
- 在C#的web开发中repeater和DataList有什么区别?
- C#如何利用正则表达式读取网页指定内容
- 向大神求助 “System.AccessViolationException”类型的未经处理的异常在 System.Data.dll 中发生
*****************************************************************************
签名档: http://feiyun0112.cnblogs.com/
换成 DoEvents试试。。
public partial class Form1 : Form
{
public static bool started;
public static int result; public Form1()
{
InitializeComponent();
} private void SButton_Click(object sender, EventArgs e)
{
started = true;
Selectit sel = new Selectit();
Thread selthread = new Thread(sel.Startselect);
selthread.Start();
if (!started) { selthread.Abort(); }
} private void Stop_Click(object sender, EventArgs e)
{
started = false;
}
public class Selectit
{
public void Startselect()
{
while (Form1.started)
{
Random ran = new Random();
Application.OpenForms[0].Invoke(new MethodInvoker(delegate
{
Form1.numlable.Text = ran.Next(1, 126).ToString();
}));
Thread.Sleep(100);
}
}
}
}再把numlable改成静态的,因为你又写了个类,这样访问方便点。还有selthread 最好定义成全局的,不用每次都实例化