17,740
社区成员
发帖
与我相关
我的任务
分享
public delegate void AppDomainHandler(string[] sourceFilesToProcess, string[] rules, string logPath, string sourceFilter, string sourceFileName, string fileType, ref bool tempResult); //定义委托
AppDomainHandler appDomainMonitor = new OSLEBotRun().StartCheck; //将方法名StartCheck 赋值给委托类型
IAsyncResult ar = appDomainMonitor.BeginInvoke(sourceFiles, filteredRules,
logPath, sourceFilter, AppDomain.CurrentDomain.GetData("SourceFileName").ToString(),
fileType.ToString(), ref tempResult, null, null);
if (!ar.AsyncWaitHandle.WaitOne(TimeSpan.FromMinutes(5), false)) //防止程序静止,等待5分钟,如果5分钟线程没有返回,就返回false。
{
tempResult = false;
InvokeFailed = true;
//EndInvoke
appDomainMonitor.EndInvoke(ref tempResult, ar);
}
if (!InvokeFailed) //EndInvoke
appDomainMonitor.EndInvoke(ref tempResult, ar);
if (!ar.AsyncWaitHandle.WaitOne(TimeSpan.FromMinutes(5), false))
using System;
using System.Threading;
using System.Diagnostics;
namespace ThreadDemo
{
class Program
{
//异步委托
static int OtherMethod(int data)
{
Console.WriteLine("OtherMethod Started");
while(true)
{
++data;
Console.Write(data + " ");
if (data > 1000)
{
data = 0;
}
if (data > 1001)
{
break;
}
}
Console.WriteLine("OtherMethod Completed");
return data;
}
public delegate int AsyncDelegate(int data);
static void Main()
{
AsyncDelegate ad = OtherMethod;
IAsyncResult ar = ad.BeginInvoke(1, null, null);
if (!ar.AsyncWaitHandle.WaitOne(3 * 1000, false))
{
Console.WriteLine("Can't get the result now");
}
//int result = ad.EndInvoke(ar);
//Console.WriteLine("result:{0}", result);
}
}
}