异步等待(ManualResetEvent

时间:2023-03-09 05:55:07
异步等待(ManualResetEvent

ManualResetEvent实现异步等待,超过时间 不做处理,继续往下执行代码

(ManualResetEvent 涉及一个线程在其他线程进行之前必须完成的任务)

 ManualResetEvent[] mre = new ManualResetEvent[];

             mre[] = new ManualResetEvent(false);
shoppingbll spb = new shoppingbll(mre[], dict);
Thread thd1 = new Thread(new ThreadStart(spb.GetGPrice));
thd1.Start(); mre[] = new ManualResetEvent(false);
farebll pfb = new farebll(mre[], dict);
Thread thd2 = new Thread(new ThreadStart(pfb.GetfarePrice));
thd2.Start(); //停顿12s 超过12s后,执行后面代码,不再等待
WaitHandle.WaitAll(mre, , false);

GetGPrice 方法(GetfarePrice类似) ,调用ManualResetEvent 的set()方法  返回信号;

public void GetGPrice()
{
try
{
//todo
}
catch (Exception e)
{ }
finally
{
if (_mre != null)
{
_mre.Set();//返回完成信号
}
}
}

Task(任务)类似用法:

             Task t1 = new Task(func);//无返回值
Task<int> t2 = new Task<int>(()=>canshufun());//有返回值(带参数
Task<int> t3 = new Task<int>(() => intfun());//有返回值(无参数 Task[] tt = new Task[] { t1, t2 ,t3};
Task.WaitAll(tt, * );//10秒后 不再等待,往后执行
int result2 = t2.Result;//获取返回结果
int result3 = t3.Result;//获取返回结果 public void func()
{ } public int intfun()
{
return ;
}
public int canshufun(int i)
{
return i;
}

Task 通过CancellationTokenSource参数 取消超时线程任务

 /// <summary>
/// 返回政策
/// </summary>
/// <param name="dept">出发</param>
/// <param name="arr">到达</param>
/// <param name="depttime">出发日期</param>
/// <param name="arrtime">返程日期</param>
/// <param name="from">政策源</param>
/// <param name="carriers">航司</param>
/// <param name="cabinlevels">舱位等级(First, Business, Economy)</param>
/// <param name="ftype">1,直飞;2,所有</param>
/// <param name="number">返回数量</param>
/// <param name="second">超时时间</param>
/// <param name="iresponsetime">国际接口返回时间</param>
/// <param name="nresponsetime">国内接口返回时间</param>
/// <returns></returns>
public model.PolicyCacheModel Get(string dept, string arr, string depttime, string arrtime, string from, string carriers, string cabinlevels, string ftype, int number, int second, ref int iresponsetime, ref int nresponsetime)
{
List<string> policies = from.Split('|').ToList();
List<string> gj = new List<string>();
List<string> gn = new List<string>();
foreach (string s in policies)
{
if(s.Contains("12,")) gj.Add(s);
else if (s.Contains("18,")) gj.Add(s);
else if (s.Contains("20,")) gn.Add(s);
else if (s.Contains("50,")) gj.Add(s);
else gn.Add(s);
} if (gj.Count == && gn.Count == ) return null; Task<model.CompareModel> gjtask = null;
Task<model.CompareModel> gntask = null;
CancellationTokenSource gjct = null;
CancellationTokenSource gnct = null;
List<Task<model.CompareModel>> tasks = new List<Task<model.CompareModel>>(); if (gj.Count > )
{
gjct = new CancellationTokenSource();
gjtask = new Task<model.CompareModel>(() => GetInternational(dept, arr, depttime, arrtime, from, carriers, cabinlevels, ftype, number, second), gjct.Token);
tasks.Add(gjtask);
}
if (gn.Count > )
{
gnct = new CancellationTokenSource();
gntask = new Task<model.CompareModel>(() => GetNational(dept, arr, depttime, arrtime, from, carriers, cabinlevels, ftype, number, second), gnct.Token);
tasks.Add(gntask);
} if (gjtask != null) gjtask.Start();
if (gntask != null) gntask.Start();
Task.WaitAll(tasks.ToArray(), second * ); model.CompareModel m1 = null;
model.CompareModel m2 = null;
if (gjtask != null)
{
if (gjtask.Status == TaskStatus.RanToCompletion) m1 = gjtask.Result;
else gjct.Cancel();
}
if (gntask != null)
{
if (gntask.Status == TaskStatus.RanToCompletion) m2 = gntask.Result;
else gnct.Cancel();
} try
{
model.CompareModel d = InMatch(m1, m2);
model.PolicyCacheModel m3 = MatchRebate(d);
return m3;
}
catch (Exception ex)
{
return null;
}
}

参考:

http://www.cnblogs.com/li-peng/p/3291306.html

http://www.cnblogs.com/springyangwc/archive/2011/10/12/2208991.html