程序思想:在try...catch...块中当try中的工作阻塞或发生无限异常时另起进程
讨论中心:在catch块中添加代码,使之达到停止当前线程并另起一个与当前线程同一作用的线程public class test
{
Thread th1;
Thread th2;
private void main()
{
th1=new Thread(new ThreadStart(method));
th1.IsBackground=true;
th1.start();
}private void method()
{
try
{
while(true)
{
//dosomework
}
}
catch//捕捉到无法停止的异常,此异常通常使该进程阻塞或发生无限异常
{
//停止th1进程,启动th2进程,其中th2进程的工作与th1相同
//此处为我需要的代码,起大家集中讨论该块的代码。
}
}
}另一篇0回复的送分帖子,大家只要指出一点点技术,就可以得分
链接:http://topic.csdn.net/u/20100810/09/6161aa75-3eda-45c1-963c-3a50efa463c1.html
贴名: c#设计的GSM模块发送短信息时出现的问题
0回复,CSDN.c#块深深地伤了我的心 0.0
讨论中心:在catch块中添加代码,使之达到停止当前线程并另起一个与当前线程同一作用的线程public class test
{
Thread th1;
Thread th2;
private void main()
{
th1=new Thread(new ThreadStart(method));
th1.IsBackground=true;
th1.start();
}private void method()
{
try
{
while(true)
{
//dosomework
}
}
catch//捕捉到无法停止的异常,此异常通常使该进程阻塞或发生无限异常
{
//停止th1进程,启动th2进程,其中th2进程的工作与th1相同
//此处为我需要的代码,起大家集中讨论该块的代码。
}
}
}另一篇0回复的送分帖子,大家只要指出一点点技术,就可以得分
链接:http://topic.csdn.net/u/20100810/09/6161aa75-3eda-45c1-963c-3a50efa463c1.html
贴名: c#设计的GSM模块发送短信息时出现的问题
0回复,CSDN.c#块深深地伤了我的心 0.0
判断IsAlive为true,调用Abort方法
class Program
{
static Thread th1;
static Thread th2;
static Thread curThread;
static void Main(string[] args)
{
NewThread();
Console.Read();
}
static void NewThread()
{
if (curThread == null || (th2 != null && Thread.CurrentThread == th2))
{
Console.WriteLine("Start thread th1");
th1 = new Thread(new ThreadStart(Method));
th1.IsBackground = true;
curThread = th1;
}
else
{
Console.WriteLine("Start thread th2");
th2 = new Thread(new ThreadStart(Method));
th2.IsBackground = true;
curThread = th2;
}
curThread.Start();
} static void Method()
{
try
{
Thread.Sleep(1000);
throw new Exception("Thread exception");
}
catch
{
NewThread();
}
}
/*------------输出-----------------
Start thread th1
Start thread th2
Start thread th1
Start thread th2
.....
-----------------------------------*/
}
然后、停止错误线程是不需要的,只要你catch块中顺利执行完毕后,错误线程自动结束,那么你只要在catch块中再开启你要的新线程即可。