473,387 Members | 1,423 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How do I cancel an asynchronous operation?

I'm writing a class that communicates with a server using the TcpClient
class. Most of the methods I've written are intended to be used
synchronously and will block until they are completed. But these calls will
be made very seldom and between operations I would like to put the client
into a "listening mode" that will listen for other server messages. When
the client is ready to send/receive data again, take it back out of
listening mode and again begin synchronous operations. It's quite easy to
set up a BeginRead on the stream I created and have its callback process any
messages received from the server. But when I want to STOP listening and
start synchronous operations again I need to be able to cancel this. I
think the easiest way would be if I could just cancel the thread that I
started the BeginRead operation on. I'm sure there is a way to use the
IAsyncResult or it's WaitHandle to cancel the thread, but threading is not
my strongpoint and a solution is alluding me right now. Any assistance
would be appreciated. Here's the skeleton of what I'm going for.

private IAsyncResult ar;

private void Listen()
{
listenBuffer = new byte[1024];
ar = stream.BeginRead(listenBuffer, 0, listenBuffer.Length, new
AsyncCallback(ListenMessage), this);
}
private void ListenMessage(IAsyncResult ar)
{
// ..... interpret received message.
}
private void StopListen()
{
// ?????? ar...
}


Any help is appreciated,
Jacob
Nov 15 '05 #1
3 11240
Jacob,
started the BeginRead operation on. I'm sure there is a way to use the
IAsyncResult or it's WaitHandle to cancel the thread, but threading is not
my strongpoint and a solution is alluding me right now. Any assistance
would be appreciated. Here's the skeleton of what I'm going for.


Unfortunately, cancelling the async. operations is not supported by the .NET
framework. I have recently had to write my own replacement since
cancellation was a must.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

Nov 15 '05 #2
Well, that answers my question. Thanks for your help.

Jacob
"Dmitriy Lapshin [C# / .NET MVP]" <x-****@no-spam-please.hotpop.com> wrote
in message news:ep**************@TK2MSFTNGP12.phx.gbl...
Jacob,
started the BeginRead operation on. I'm sure there is a way to use the
IAsyncResult or it's WaitHandle to cancel the thread, but threading is not my strongpoint and a solution is alluding me right now. Any assistance
would be appreciated. Here's the skeleton of what I'm going for.
Unfortunately, cancelling the async. operations is not supported by the

..NET framework. I have recently had to write my own replacement since
cancellation was a must.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

Nov 15 '05 #3
On Mon, 29 Dec 2003 14:36:35 +0200, "Dmitriy Lapshin [C# / .NET MVP]"
<x-****@no-spam-please.hotpop.com> wrote:
Jacob,
started the BeginRead operation on. I'm sure there is a way to use the
IAsyncResult or it's WaitHandle to cancel the thread, but threading is not
my strongpoint and a solution is alluding me right now. Any assistance
would be appreciated. Here's the skeleton of what I'm going for.


Unfortunately, cancelling the async. operations is not supported by the .NET
framework. I have recently had to write my own replacement since
cancellation was a must.


I am just researching this same problem. In my case, it's a server
socket that calls "BeginAccept()". The MSDN doc sample "Using an
Asynchronous Server Socket" shows this code:

try {
listener.Bind(localEP);
s.Listen(10); // <-- typo in MSDN docs!

while (true) {
allDone.Reset();

Console.WriteLine("Waiting for a connection...");
listener.BeginAccept(
new AsyncCallback(SocketListener.acceptCallback),
listener );

allDone.WaitOne();
}
} catch (Exception e) {
Console.WriteLine(e.ToString());
}

An infinite loop of BeginAccept() calls is NOT what I would want in my
own server. As I have been trying to figure out how to cancel a
"pending" BeginAccept() call, I am finding out now that it can't be
done.

It's not too big a problem for me, as my own version has a way out of
the loop and closes the server socket. Either the server socket
closing OR the thread ending (which happens after the close) causes
the acceptCallback to be called ONCE the first time, and if I continue
hitting my server start and stop buttons, the acceptCallback will be
called TWICE everytime after that. Strange behaviour.

So I have been trying to figure out how to cancel the pending
BeginAccept() call. And now it seems it can't be done!

// CHRIS

Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: scott ocamb | last post by:
hello i am getting this error EndInvoke can only be called once for each asynchronous operation I have coded an app using the async method development pattern. This error occurred under...
1
by: Gopal Prabhakaran | last post by:
Dear Guys I developed a Win form application using C#. In my application, I used Login form to enter into application. In this login form I used three buttons( OK, Close and Cancel). The OK...
1
by: RJN | last post by:
Hi I have 2 operations to perform one is to upload data and the second is to generate report. Both take huge time(more than 20 mins and I believe by that time session would have expired) and...
7
by: Siv | last post by:
Hi, I have a stored procedure that I want to execute and then wait in a loop showing a timer whilst it completes and then carry on once I get notification that it has completed. The main reason...
2
by: Robinson | last post by:
I can start an Asynchronous operation against a data source with SQLCommand.BeginExecuteReader, allowing me to loop, checking for user cancellation before the operation has completed, but how then...
0
by: Bishoy George | last post by:
Hi, I have a asp.net 2.0 web application. I want to implement the asynchronous model through http handler in web.config ...
4
by: Morgan Cheng | last post by:
Since ASP.NET 2.0, asynchronous web service client can be implemented with event-based pattern, instead of original BeginXXX/EndXXX pattern. However, I didn't find any material about event-based...
16
by: parez | last post by:
I start a BackGroundWorker to populate a grid. It is started off in the ui layer The thread follows( cannot think of a better word) the path UI->Layer1->Layer2->Communication Layer and it...
0
by: karantech | last post by:
sir, can anybody plz give an easy explanation to me on how to cancel an asynchronous operation. i m using begininvoke, endinvoke async methods.
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.