类代码
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace TxWeb.CnForums
{
public class TxThread
{
public delegate void Delegate(object obj);
/// <summary>
/// 执行指定的方法,如果在指定的时间之内没有完成,则中止
/// </summary>
/// <param name="func">任务过程</param>
/// <param name="threadID">帖子编号</param>
/// <param name="timeSpan">超时时间</param>
/// <param name="timeoutCallback">如果超时,则调用该方法</param>
/// <param name="updateID">变化主表记录编号</param>
/// <returns>是否正确执行完毕</returns>
public static bool Call(Delegate func, object threadID, TimeSpan timeSpan, Delegate timeoutCallback, object updateID)
{
if (func == null)
throw new ArgumentNullException("func");
ManualResetEvent resetEvent = new ManualResetEvent(false);
ManualResetEvent waitThreadEvent = new ManualResetEvent(false);
Exception error = null;
Thread thread = null;
// 将任务加到线程当中
ThreadPool.QueueUserWorkItem(delegate
{
thread = Thread.CurrentThread;
try { func(threadID); }
catch (ThreadAbortException) { }
catch (Exception ex) { error = ex; }
resetEvent.Set();
// 每次线程执行结束都等待后续的处理逻辑
waitThreadEvent.WaitOne();
});
try
{
// 等待任务的结束
bool result = resetEvent.WaitOne(timeSpan, false);
// 说明在执行过程中出现异常,直接抛出异常
if (error != null)
{
throw error;
}
if (!result)
{
if (thread != null)
{
// 此时可以确保该线程没有开始运行新的任务
thread.Abort();
waitThreadEvent.Set();
}
if (timeoutCallback != null)
timeoutCallback(updateID);
}
return result;
}
finally
{
// 最后确保释放线程池线程
waitThreadEvent.Set();
}
}
}
}
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace TxWeb.CnForums
{
public class TxThread
{
public delegate void Delegate(object obj);
/// <summary>
/// 执行指定的方法,如果在指定的时间之内没有完成,则中止
/// </summary>
/// <param name="func">任务过程</param>
/// <param name="threadID">帖子编号</param>
/// <param name="timeSpan">超时时间</param>
/// <param name="timeoutCallback">如果超时,则调用该方法</param>
/// <param name="updateID">变化主表记录编号</param>
/// <returns>是否正确执行完毕</returns>
public static bool Call(Delegate func, object threadID, TimeSpan timeSpan, Delegate timeoutCallback, object updateID)
{
if (func == null)
throw new ArgumentNullException("func");
ManualResetEvent resetEvent = new ManualResetEvent(false);
ManualResetEvent waitThreadEvent = new ManualResetEvent(false);
Exception error = null;
Thread thread = null;
// 将任务加到线程当中
ThreadPool.QueueUserWorkItem(delegate
{
thread = Thread.CurrentThread;
try { func(threadID); }
catch (ThreadAbortException) { }
catch (Exception ex) { error = ex; }
resetEvent.Set();
// 每次线程执行结束都等待后续的处理逻辑
waitThreadEvent.WaitOne();
});
try
{
// 等待任务的结束
bool result = resetEvent.WaitOne(timeSpan, false);
// 说明在执行过程中出现异常,直接抛出异常
if (error != null)
{
throw error;
}
if (!result)
{
if (thread != null)
{
// 此时可以确保该线程没有开始运行新的任务
thread.Abort();
waitThreadEvent.Set();
}
if (timeoutCallback != null)
timeoutCallback(updateID);
}
return result;
}
finally
{
// 最后确保释放线程池线程
waitThreadEvent.Set();
}
}
}
}
调用:
TxWeb.CnForums.TxThread.Delegate createHtml = new TxWeb.CnForums.TxThread.Delegate(CreateHtml);
TxWeb.CnForums.TxThread.Delegate dealOverTime = new TxWeb.CnForums.TxThread.Delegate(DealOverTime);
TxWeb.CnForums.TxThread.Call(createHtml, threadID, TimeSpan.FromSeconds(OverTime), dealOverTime, updateID);
TxWeb.CnForums.TxThread.Delegate dealOverTime = new TxWeb.CnForums.TxThread.Delegate(DealOverTime);
TxWeb.CnForums.TxThread.Call(createHtml, threadID, TimeSpan.FromSeconds(OverTime), dealOverTime, updateID);