Forcefully aborting asychronous call.

A

archana

Hi all,

I am having application in which i am doing asynchronous call.I am
using manualresetevent to wait for asynchronous call to complete.

I want to stop asynchronous call after certain period of time.

I want something like thread.abort for aborting aynchronous call.

Can someone tell me way of aborting asynchronous call.

Any help will be truely appreciated.

thanks in advance.
 
S

Samuel R. Neff

ManualResetEvent.WaitOne() has three overloads, two of which take a
timeout parameter as either an int (milliseconds) or TimeSpan.

Also you could call Set() yourself from a different thread if
something happens that causes you not to want to wait any more--the
waiting code will continue execution just like the wait had timed out.

HTH,

Sam
 
A

archana

Hi,

thanks for your reply.

In method i am calling set method at the end of whole procedure.

But what i want is if process is not completed say withing 2 mins then
i need to forcefully abort it. So i called waitone on event with
passing time as 2 mins.

So My question is after waitone will that function continue its
execution.

Means like if we call thread.abort it stop execution of thread, is
there any similar functionlity for asychrnonous call abort.

Any help will be truely appreciated.
 
W

Willy Denoyette [MVP]

Please try to answer following questions: what exactly is your async. call
doing and more importantly - why does it not return in a timely fashion.
If your call has transitioned into unmanaged code (say a synch. socket call
or any other blocking call), and this unmanaged function waits for an IO
completion which never happens, you are stuck. In this scenario, even a
Thread.Abort will not terminate the thread, as Thread.Abort cannot abort a
thread that is executing outside the CLR. All you can do in such case is
unload the Application domain, or terminate the process, Thread.Abort is
never a solution and is only applicable if you are going to throw away the
AD anyway.

Willy.


| Hi,
|
| thanks for your reply.
|
| In method i am calling set method at the end of whole procedure.
|
| But what i want is if process is not completed say withing 2 mins then
| i need to forcefully abort it. So i called waitone on event with
| passing time as 2 mins.
|
| So My question is after waitone will that function continue its
| execution.
|
| Means like if we call thread.abort it stop execution of thread, is
| there any similar functionlity for asychrnonous call abort.
|
| Any help will be truely appreciated.
|
| Thanks
| Samuel R. Neff wrote:
| > ManualResetEvent.WaitOne() has three overloads, two of which take a
| > timeout parameter as either an int (milliseconds) or TimeSpan.
| >
| > Also you could call Set() yourself from a different thread if
| > something happens that causes you not to want to wait any more--the
| > waiting code will continue execution just like the wait had timed out.
| >
| > HTH,
| >
| > Sam
| >
| >
| >
| > ------------------------------------------------------------
| > We're hiring! B-Line Medical is seeking Mid/Sr. .NET
| > Developers for exciting positions in medical product
| > development in MD/DC. Work with a variety of technologies
| > in a relaxed team environment. See ads on Dice.com.
| >
| >
| >
| >
| > On 21 Aug 2006 00:22:06 -0700, "archana" <[email protected]>
| > wrote:
| >
| > >Hi all,
| > >
| > >I am having application in which i am doing asynchronous call.I am
| > >using manualresetevent to wait for asynchronous call to complete.
| > >
| > >I want to stop asynchronous call after certain period of time.
| > >
| > >I want something like thread.abort for aborting aynchronous call.
| > >
| > >Can someone tell me way of aborting asynchronous call.
| > >
| > >Any help will be truely appreciated.
| > >
| > >thanks in advance.
|
 
A

archana

Hi,

thanks for your reply.

I am calling begingGetresponse method of webrequest asynchronously.
Actually i am calling 5 begingetresponse at a time to proess 5 urls and
i want all 5 to gets aborted depending on option selected.

I set timeout for this using registerwaitforsingleobject but this is
not working properly.

I want to abort this asynchronous call.

How can i do this?

thanks in advance.
 
W

Willy Denoyette [MVP]

| Hi,
|
| thanks for your reply.
|
| I am calling begingGetresponse method of webrequest asynchronously.
| Actually i am calling 5 begingetresponse at a time to proess 5 urls and
| i want all 5 to gets aborted depending on option selected.
|
| I set timeout for this using registerwaitforsingleobject but this is
| not working properly.
|
| I want to abort this asynchronous call.
|
| How can i do this?
|
| thanks in advance.
|

RegisterWaitForSingleObject should work, can you please post a complete
samle that illustrates the issue?

Willy.
 
W

William Stacey [MVP]

The new CCR is very well suited for this kind or arbitration. The runtime
is used in their new robotics stuff as that is heavily threaded. It is not
RTM yet, but the current bits work great. It is worth playing with anyway
and getting to know the Port<T> abstraction.
http://msdn.microsoft.com/msdnmag/issues/06/09/ConcurrentAffairs/default.aspx

--
William Stacey [MVP]

| Hi,
|
| thanks for your reply.
|
| I am calling begingGetresponse method of webrequest asynchronously.
| Actually i am calling 5 begingetresponse at a time to proess 5 urls and
| i want all 5 to gets aborted depending on option selected.
|
| I set timeout for this using registerwaitforsingleobject but this is
| not working properly.
|
| I want to abort this asynchronous call.
|
| How can i do this?
|
| thanks in advance.
|
| Willy Denoyette [MVP] wrote:
| > Please try to answer following questions: what exactly is your async.
call
| > doing and more importantly - why does it not return in a timely fashion.
| > If your call has transitioned into unmanaged code (say a synch. socket
call
| > or any other blocking call), and this unmanaged function waits for an IO
| > completion which never happens, you are stuck. In this scenario, even a
| > Thread.Abort will not terminate the thread, as Thread.Abort cannot abort
a
| > thread that is executing outside the CLR. All you can do in such case is
| > unload the Application domain, or terminate the process, Thread.Abort is
| > never a solution and is only applicable if you are going to throw away
the
| > AD anyway.
| >
| > Willy.
| >
| >
| > | > | Hi,
| > |
| > | thanks for your reply.
| > |
| > | In method i am calling set method at the end of whole procedure.
| > |
| > | But what i want is if process is not completed say withing 2 mins then
| > | i need to forcefully abort it. So i called waitone on event with
| > | passing time as 2 mins.
| > |
| > | So My question is after waitone will that function continue its
| > | execution.
| > |
| > | Means like if we call thread.abort it stop execution of thread, is
| > | there any similar functionlity for asychrnonous call abort.
| > |
| > | Any help will be truely appreciated.
| > |
| > | Thanks
| > | Samuel R. Neff wrote:
| > | > ManualResetEvent.WaitOne() has three overloads, two of which take a
| > | > timeout parameter as either an int (milliseconds) or TimeSpan.
| > | >
| > | > Also you could call Set() yourself from a different thread if
| > | > something happens that causes you not to want to wait any more--the
| > | > waiting code will continue execution just like the wait had timed
out.
| > | >
| > | > HTH,
| > | >
| > | > Sam
| > | >
| > | >
| > | >
| > | > ------------------------------------------------------------
| > | > We're hiring! B-Line Medical is seeking Mid/Sr. .NET
| > | > Developers for exciting positions in medical product
| > | > development in MD/DC. Work with a variety of technologies
| > | > in a relaxed team environment. See ads on Dice.com.
| > | >
| > | >
| > | >
| > | >
| > | > On 21 Aug 2006 00:22:06 -0700, "archana"
<[email protected]>
| > | > wrote:
| > | >
| > | > >Hi all,
| > | > >
| > | > >I am having application in which i am doing asynchronous call.I am
| > | > >using manualresetevent to wait for asynchronous call to complete.
| > | > >
| > | > >I want to stop asynchronous call after certain period of time.
| > | > >
| > | > >I want something like thread.abort for aborting aynchronous call.
| > | > >
| > | > >Can someone tell me way of aborting asynchronous call.
| > | > >
| > | > >Any help will be truely appreciated.
| > | > >
| > | > >thanks in advance.
| > |
|
 
A

archana

hi,

thanks for your reply.

I am having following function to create request

public static System.IAsyncResult MakeHttpRequest(string str)
{

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(str);
req.Timeout = 600;
System.IAsyncResult ar = req.BeginGetResponse(new
AsyncCallback(AfterExecution), req);

return ar;
}

public static void AfterExecution(System.IAsyncResult ar)
{
// Return the response.
HttpWebRequest req = (HttpWebRequest) ar.AsyncState;
HttpWebResponse resp = (HttpWebResponse) req.EndGetResponse(ar);

//Wrap the response stream with a text-based reader
StreamReader sr = new StreamReader(resp.GetResponseStream());

string strStream = sr.ReadToEnd();

// Close the response to free resources.
sr.Close();
resp.Close();
System.Console.WriteLine("after execution reach here for " +
resp.ResponseUri.ToString());
}

in main code i am doing following things:-
System.IAsyncResult ar1 = MakeHttpRequest("http://www.msdfdn.com");
System.IAsyncResult ar2 = MakeHttpRequest"http://www.yadfdhoo.com");
WaitHandle[] wh = {ar1.AsyncWaitHandle,ar2.AsyncWaitHandle};
if (WaitHandle.WaitAll(wh, 10, false))
{
console.writeline(''break');
}


So my question is will afterexecution function execute if i called
waithandle.waitall for 10 sec as i set timeout as 600.

I read on msdn that when you are calling asynchronous webrequest
tiemout property doesn't work so i added registeredtimeout to set
timeout for asynchronous webrequest.

My second question if i want to execute different function on timeout,
means say after calling waithandle.waitall if call doesn;t completed
within that time i want some different processing so i wrote another
function and called that function with passing one parameter that is
ar1 or ar2. And in that function i called endwebresponse.

Will it clash with 'afterexecution' function?

And if i set timeout using registedwaitforobject with timeout say 100
seconds but in waithandle.waitlall i set timeout to 10 which will gets
consider.

Please correct me if i am wrong.
Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top