线程
Thread td = new Thread(new ThreadStart(TestMethod));
td.Isbackground = true;
td.Start();void TestMethod()
{
......
CallOtherComputer();//调用另外一台设备上的接口方法}现在碰到一个这样的问题,td线程在调用TestMethod时,CallOtherComputer方法操作导致被调用的设备死机,这个时候由于线程进入到这个方法后没有返回,导致调用的线程出现异常,请问有什么办法能够避免这样的问题吗?例如线程超时之类的,如果我的线程去调用一个方法,这个方法在3秒之内没有返回结果我就退出这个线程,然后继续做其他的工作,请问该如何来实现?
谢谢。
Thread td = new Thread(new ThreadStart(TestMethod));
td.Isbackground = true;
td.Start();void TestMethod()
{
......
CallOtherComputer();//调用另外一台设备上的接口方法}现在碰到一个这样的问题,td线程在调用TestMethod时,CallOtherComputer方法操作导致被调用的设备死机,这个时候由于线程进入到这个方法后没有返回,导致调用的线程出现异常,请问有什么办法能够避免这样的问题吗?例如线程超时之类的,如果我的线程去调用一个方法,这个方法在3秒之内没有返回结果我就退出这个线程,然后继续做其他的工作,请问该如何来实现?
谢谢。
解决方案 »
- Windows Mobile Pocket PC 连接数据库
- combox绑定enum问题
- WPF中怎么让panel能够包含一个windows
- 用C#怎么把一个string类型的字符串 截取成单独的字符字符并存到数组里面去
- [求助]WebBrowser控件,框架网页的Navigated事件处理
- 一个关于C/S CLIENT多线程中FORM的问题
- 动态产生按钮
- 建立连接数据库方法与调用的两种写法有何区别?
- 请教各位高手,发布自己写的winfrom程序哪种方式好
- 很菜的问题:C# 可以做什么,需要什么环境和条件?能做类似java applet的东西吗?
- 关于昨天那个C#验证邮箱的问题
- 如何通过类获取类名?各位帮帮忙?
private AutoResetEvent _autoReset = new AutoResetEvent(false);
//启动线程
Thread td = new Thread(new ThreadStart(TestMethod));
td.Isbackground = true;
td.Start();
//判超时
if (!_autoReset.WaitOne(2000, false))//超时时间设为2000毫秒,即2秒
{
MessageBox.Show("超时");
}
else
{
MessageBox.Show("未超时");
}void TestMethod()
{
......
CallOtherComputer();//调用另外一台设备上的接口方法
_autoReset.Set();//通知线程完成
}
td.Isbackground = true;
td.Start();
t1.Interrupt();//这个比较重要,重新唤醒阻塞的线程
void TestMethod()
{
......
CallOtherComputer();//调用另外一台设备上的接口方法
try
{
Thread.Sleep(3000);
}
catch (System.Threading.ThreadInterruptedException e)
{
Console.WriteLine(e);
}}
void TestMethod()
{
......
CallOtherComputer();//调用另外一台设备上的接口方法
try
{
Thread.Sleep(3000);
}
catch (System.Threading.ThreadInterruptedException e)
{
Console.WriteLine(e);
}
td.set();
}
查下这个。
{
System.Threading.Thread myThread = new System.Threading.Thread(new System.Threading.ThreadStart(test2));
myThread.IsBackground = true;
myThread.Start();
} bool runFlag = false;
bool endFlag = false;
private void test2()
{
runFlag = false;
endFlag = false;
System.Threading.Thread myThread = new System.Threading.Thread(new System.Threading.ThreadStart(test3));
myThread.IsBackground = true;
myThread.Start(); while (true)
{
if (runFlag)
{
System.Threading.Thread.Sleep(3000);//等3秒 if (!endFlag)
{
myThread.Abort();
}
break;
}
}
} private void test3()
{
runFlag = true; //---------------------模拟CallOtherComputer()
int i = 0;
int count = 1000 * 30;
while ((i += 1000) < count)
{
System.Threading.Thread.Sleep(1000);
}
//--------------------- endFlag = true;
}