关于线程的问题 如果执行了Abort方法当然不能再执行Resume;把TransThread.Resume();放在else里。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 楼上的理解错我的意思了:ThreadStart myStart=new ThreadStart(myFunc);Thread myThread=new Thread(myStart);myThread.Start();。这个线程是执行myFunc操作的。1。现在我有两个button,其中一个button1是开始操作,另外一个button2是执行取消操作的。当点击取消操作的时候,弹出“是否取消”messageBox,如果选择yes,就取消执行,如果选择no就继续执行。2。停止以后我点击button1就可以重新开始操作。请问这该怎么编码,谢谢了!!!!解决了以后再加分。 CancelButton的Onclick事件时对话窗代码,如果确认的话就执行线程的abort操作。如果你的线程是一个while,那么可以设置一个bool变量,每次while的头一句就判断这个变量,那么,如果这个变量为false(这里表示停止线程),那就退出function. 如果你的线程里面有对象的执行,那么最好把new的过程放到线程内部执行,否则,再次执行的时候很容易有对象未引用的异常发生。 public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.Button button1; private System.Windows.Forms.Button button2; private System.Windows.Forms.ListBox listBox1; /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.Container components = null; private static long i=0; private Thread t=null; public Form1() { // // Windows 窗体设计器支持所必需的 // InitializeComponent(); t=new Thread(new ThreadStart(dowork)); } > protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } [STAThread] static void Main() { Application.Run(new Form1()); } private void button1_Click(object sender, System.EventArgs e) { if(t.ThreadState==System.Threading.ThreadState.Suspended) { t.Resume(); } else if (t.ThreadState==System.Threading.ThreadState.Unstarted) { t.Start(); } } private void dowork() { while(true) { this.listBox1.Items.Add(i.ToString()); Application.DoEvents(); if(i==long.MaxValue) i=0; else i++; Application.DoEvents(); } } //stop the thread private void button2_Click(object sender, System.EventArgs e) { if(MessageBox.Show("are you sure that suspend the thread","one",System.Windows.Forms.MessageBoxButtons.OKCancel)==DialogResult.OK) t.Suspend(); } } 看这样行不行://开始按钮private void button1_Click(object sender, System.EventArgs e) { t=new Thread(new ThreadStart(dowork)); if(t.ThreadState==System.Threading.ThreadState.Suspended) { t.Resume(); } else if (t.ThreadState==System.Threading.ThreadState.Unstarted) { t.Start(); }//停止按钮//stop the thread private void button2_Click(object sender, System.EventArgs e) { t.Suspend(); if(MessageBox.Show("are you sure that suspend the thread","one",System.Windows.Forms.MessageBoxButtons.OKCancel)==DialogResult.OK) { t=null; } else t.Resume(); if(MessageBox.Show("are you sure that suspend the thread","one",System.Windows.Forms.MessageBoxButtons.OKCancel)==DialogResult.OK) { t=null; }谢谢楼上的,要的就是上面这个。原来付null 就解决问题了。-----------------------------------------------------------------------------------------还有一个问题,如果线程的方法正在执行中,我关闭了窗口,那该进程在资源管理器里面还存在,该怎么解决这个问题啊?我这样写,还是不行: private void Form1_Closed(object sender,System.EventArgs e) { t=null; this.Dispose(); } private void Form1_Closed(object sender, System.EventArgs e) { if(t!=null) { if(t.IsAlive) { t.Abort(); } } } 求arrylist中相同的元素 C# process类使用 动画程序出了点错 寻求帮助:c#的remoting里,CallContext.GetData获得的对象老是空的? 关于HEX编码的问题 用C#编写的Windows帮助要写成CHM格式,请问如何实现啊? 一般的外网访问内网是怎么实现的?自己编写这个程序有没有可能? 如何屏蔽半角 及 ,' 输入 谁做过winfrom的打印呀我现在做机票打印呢请大家给点帮助 请问什么时候要用到类呢 winfrom - datagrid 數據源綁定為dataset如可將其數據源釋放! 我用C#编写的spacebbs论坛 1.0版提供下载(不含源码)
ThreadStart myStart=new ThreadStart(myFunc);
Thread myThread=new Thread(myStart);
myThread.Start();。
这个线程是执行myFunc操作的。
1。现在我有两个button,其中一个button1是开始操作,另外一个button2是执行取消操作的。当点击取消操作的时候,弹出“是否取消”messageBox,如果选择yes,就取消执行,如果选择no就继续执行。
2。停止以后我点击button1就可以重新开始操作。请问这该怎么编码,谢谢了!!!!解决了以后再加分。
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.ListBox listBox1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
private static long i=0;
private Thread t=null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); t=new Thread(new ThreadStart(dowork)); } >
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} [STAThread]
static void Main()
{
Application.Run(new Form1());
} private void button1_Click(object sender, System.EventArgs e)
{
if(t.ThreadState==System.Threading.ThreadState.Suspended)
{
t.Resume();
}
else if (t.ThreadState==System.Threading.ThreadState.Unstarted)
{
t.Start();
}
} private void dowork()
{
while(true)
{
this.listBox1.Items.Add(i.ToString());
Application.DoEvents();
if(i==long.MaxValue)
i=0;
else
i++;
Application.DoEvents();
}
}
//stop the thread
private void button2_Click(object sender, System.EventArgs e)
{
if(MessageBox.Show("are you sure that suspend the thread","one",System.Windows.Forms.MessageBoxButtons.OKCancel)==DialogResult.OK)
t.Suspend();
}
}
//开始按钮
private void button1_Click(object sender, System.EventArgs e)
{
t=new Thread(new ThreadStart(dowork));
if(t.ThreadState==System.Threading.ThreadState.Suspended)
{
t.Resume();
}
else if (t.ThreadState==System.Threading.ThreadState.Unstarted)
{
t.Start();
}//停止按钮
//stop the thread
private void button2_Click(object sender, System.EventArgs e)
{
t.Suspend();
if(MessageBox.Show("are you sure that suspend the thread","one",System.Windows.Forms.MessageBoxButtons.OKCancel)==DialogResult.OK)
{
t=null;
}
else
t.Resume();
{
t=null;
}谢谢楼上的,要的就是上面这个。原来付null 就解决问题了。-----------------------------------------------------------------------------------------
还有一个问题,如果线程的方法正在执行中,我关闭了窗口,那该进程在资源管理器里面还存在,该怎么解决这个问题啊?
我这样写,还是不行:
private void Form1_Closed(object sender,System.EventArgs e)
{
t=null;
this.Dispose();
}
{
if(t!=null)
{
if(t.IsAlive)
{
t.Abort();
}
}
}