Thanks for Pete, William and Jeff's help on this issue. Your answers are
very helpful.
Hi Max,
Thanks for your post. I am Hongye Sun [MSFT] and it is my pleasure to work
with you on this issue.
I think that you are asking for a best practice to set a timeout for
waiting for an operation completes, while the operation is waiting for an
external resource. Please let me know if there is anything missing in my
understanding.
A general pattern, which is widely used in .NET framework, is to create a
new thread to run the operation and the main thread waits for its
completion or timeout. If timeout is over, however it has not been
completed, the main thread will throw an TimeoutException. To implement
it, please write a method as below, which is a generic method for most of
the situations:
-------------------------------------------------------
static void CallAndWait(Action action, int timeout)
{
Thread subThread = null;
Action wrappedAction = () =>
{
subThread = Thread.CurrentThread;
action();
};
IAsyncResult result = wrappedAction.BeginInvoke(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);
}
}
-------------------------------------------------------
Action is a delegate for method without parameters and return types. You
can also replace the Action type with your own delegate.
If this method does not work for you, please provide the following
information for us, so that we can provide you the right suggestion for
your specific scenario.
1. What is the external resource you are using?
2. Can you provide code snippet of the method?
Please let me know if you have anything unclear here. Thanks.
Regards,
Hongye Sun (
[email protected], remove 'online.')
Microsoft Online Community Support
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.
Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.