实现带参数调用泛型函数的超时
我试图了解如何使用 Action 委托类型,以便在第 3 方 COM dll 中调用的方法挂起时强制超时。经过大量搜索,我发现我可以使用 Action<>或 Func<>并传递最多 4 个泛型参数,具体取决于调用的方法是否返回参数。
对于本例,我希望对一系列返回 void 并采用 2 个参数的方法调用超时。下面是我正在组合的代码,但我无法确定如何正确编码 BeginInvoke,系统提示我放置“T arg1”和“T arg2”,但是当我输入 param1 或 param2 时,VS2008 告诉我这些值是不确定的。
这是到目前为止的代码:
static void CallAndWait(Action<T, T> action, int timeout)
{
Thread subThread = null;
Action<T, T> wrappedAction = (param1, param2) =>
{
subThread = Thread.CurrentThread;
action(param1, param2);
};
IAsyncResult result = wrappedAction.BeginInvoke(param1, param2, null, null);
if (((timeout != -1) && !result.IsCompleted) &&
(!result.AsyncWaitHandle.WaitOne(timeout, false) || !result.IsCompleted))
{
if (subThread != null)
{
subThread.Abort();
}
//TODO: close external resource.
throw new TimeoutException();
}
else
{
action.EndInvoke(result);
}
}
任何有关这里错误的想法将不胜感激。
下面是根据第一条评论重新编辑的代码
感谢您到目前为止的输入。编译如下。我似乎无法正确调用它的语法。
public static void CallAndWait<T1, T2>(Action<T1, T2> action, int timeout)
{
Thread subThread = null;
T1 param1 = default(T1);
T2 param2 = default(T2);
Action<T1, T2> wrappedAction = (p1, p2) =>
{
subThread = Thread.CurrentThread;
action(param1, param2);
};
IAsyncResult result = wrappedAction.BeginInvoke(param1, param2, null, null);
if (((timeout != -1) && !result.IsCompleted) &&
(!result.AsyncWaitHandle.WaitOne(timeout, false) || !result.IsCompleted))
{
if (subThread != null)
{
subThread.Abort();
}
//TODO: close external resource.
throw new TimeoutException();
}
else
{
action.EndInvoke(result);
}
}
我试图通过调用以下方法来测试它:
public void LongTimeProcess(int a, string b)
{
Thread.Sleep(a);
}
但以下代码不正确:
Action<int, string> action = (s1, s2) => LongTimeProcess(s1, s2);
CallAndWait<int, string>(action(1500, "hello"), 500);
更新的代码 我已经发布了代码以供论坛用户将来参考。下面的代码似乎可以工作。 唯一需要检查的一点是,当我们在“action.EndInvoke(result)”处第二次调用同一函数的例程时,我的单元测试会引发异常,因为结果与操作无关。这可能是因为我的 LongProcess 只是一个 Thread.sleep,在本例中这意味着在我第二次调用时它尚未中止。
public static void CallAndWait<T1, T2>(Action<T1, T2> action, T1 arg1, T2 arg2, int timeout)
{
Thread subThread = null;
Action<T1, T2> wrappedAction = (p1, p2) =>
{
subThread = Thread.CurrentThread;
action(arg1, arg2);
};
IAsyncResult result = wrappedAction.BeginInvoke(arg1, arg2, null, null);
if (((timeout != -1) && !result.IsCompleted) &&
(!result.AsyncWaitHandle.WaitOne(timeout, false) || !result.IsCompleted))
{
if (subThread != null)
{
subThread.Abort();
}
//TODO: close external resource.
throw new TimeoutException();
}
else
{
action.EndInvoke(result);
}
}
I am trying to get my head around the use of the Action delegate type for use in forcing a timeout when methods called in a 3rd party COM dll hang up. After much searching I find that I can use Action<> or Func<> and pass up to 4 generic parameters depending on whether the method called returns a parameter or not.
For this instance I wish to call a timeout on a series of methods that return void and take 2 parameters. What follows is the code that I am putting together, but I am unable to determine how to correctly code the BeginInvoke, I am prompted to place "T arg1" and "T arg2" but when I enter param1 or param2 VS2008 tells me that these values are indeterminate.
Here is the code as it is so far:
static void CallAndWait(Action<T, T> action, int timeout)
{
Thread subThread = null;
Action<T, T> wrappedAction = (param1, param2) =>
{
subThread = Thread.CurrentThread;
action(param1, param2);
};
IAsyncResult result = wrappedAction.BeginInvoke(param1, param2, null, null);
if (((timeout != -1) && !result.IsCompleted) &&
(!result.AsyncWaitHandle.WaitOne(timeout, false) || !result.IsCompleted))
{
if (subThread != null)
{
subThread.Abort();
}
//TODO: close external resource.
throw new TimeoutException();
}
else
{
action.EndInvoke(result);
}
}
Any ideas on what is wrong here would be much appreciated.
Below is a re-edited code based on the first comment
Thanks for the input so far. The following compiles. I just can't seem to get the syntax right in calling it.
public static void CallAndWait<T1, T2>(Action<T1, T2> action, int timeout)
{
Thread subThread = null;
T1 param1 = default(T1);
T2 param2 = default(T2);
Action<T1, T2> wrappedAction = (p1, p2) =>
{
subThread = Thread.CurrentThread;
action(param1, param2);
};
IAsyncResult result = wrappedAction.BeginInvoke(param1, param2, null, null);
if (((timeout != -1) && !result.IsCompleted) &&
(!result.AsyncWaitHandle.WaitOne(timeout, false) || !result.IsCompleted))
{
if (subThread != null)
{
subThread.Abort();
}
//TODO: close external resource.
throw new TimeoutException();
}
else
{
action.EndInvoke(result);
}
}
I am trying to test this by calling the following method with it:
public void LongTimeProcess(int a, string b)
{
Thread.Sleep(a);
}
But the following code is not correct:
Action<int, string> action = (s1, s2) => LongTimeProcess(s1, s2);
CallAndWait<int, string>(action(1500, "hello"), 500);
Updated code
I've posted the code for future reference by forum users. The code below appears to work.
The only point to check is that my unit test causes an exception to be thrown when calling the routine a second time on the same function at the point where we "action.EndInvoke(result)" as the result is not associated with the action. This is probably because my LongProcess is just a Thread.sleep, which in this instance will mean that it hasn't aborted by the time that my second call is made.
public static void CallAndWait<T1, T2>(Action<T1, T2> action, T1 arg1, T2 arg2, int timeout)
{
Thread subThread = null;
Action<T1, T2> wrappedAction = (p1, p2) =>
{
subThread = Thread.CurrentThread;
action(arg1, arg2);
};
IAsyncResult result = wrappedAction.BeginInvoke(arg1, arg2, null, null);
if (((timeout != -1) && !result.IsCompleted) &&
(!result.AsyncWaitHandle.WaitOne(timeout, false) || !result.IsCompleted))
{
if (subThread != null)
{
subThread.Abort();
}
//TODO: close external resource.
throw new TimeoutException();
}
else
{
action.EndInvoke(result);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
起初它可能应该
代替
and 如果参数具有不同的类型,甚至以下。
但我认为这还不是全部。打算再看一遍。
更新
现在我可以看到你的问题...当你尝试调用
CallAndWait()
时,你正在调用该操作。该调用必须是以下内容,而不是您的调用。
因此,您必须更改方法签名以
稍微
修改主体,您应该完成。
At first it should probably be
instead of
and if the parameters have different types even the following.
But I don't think that is all. Going to look at it again.
UPDATE
Now I can see your problem ... you are calling the action when you try to call
CallAndWait()
. The call must be the followinginstead of your call.
So you have to change the method signature from
to
modify the body a bit and you should be done.