How do I cancel an asynchronous operation?


    Sponsored Links


  • 1. Search for a datarow
    Are you sure that the column is added to the DataTable ? put a breakpoint before executing the code , and see if the row does exist. if not post the code where you add the new row to the table. cheers, -- Ignacio Machin, ignacio.machin AT Florida Department Of Transportation "cashdeskmac" < XXXX@XXXXX.COM > wrote in message news: XXXX@XXXXX.COM ... > Hi, I am using the following code to search for a specific row in a datatable: > > register1 is the name of the dataSet, Student is the name of the table and > the column I am searching is called StudentID. > > DataRow [] studentRows = register1.Student.Select("StudentID =" > + txtStudentID.Text); > //If there is only one matching record. > if(studentRows.Length==1) > { > //Remove the row and update the datatable > register1.Student.Rows.Remove(studentRows[0]); > register1.WriteXml("C:\\Student.xml"); > > } > > I have 5 records in the table, with Student ID's from 1 to 5. Using the > code above, I can search for and delete any of these rows. However, if I > enter a new row with the ID of, say 12, it can no longer find it. Does the > select method only search for an index, or actually compare the strings in > the column? > > Please help! >
  • 2. REG_RESOURCE_LIST registry data type returns EXCEPTION, too
    REG_RESOURCE_LIST also fails GetValue(string vs) function call with exception. "John Hoffman" <> wrote in message news:O9I2P% XXXX@XXXXX.COM ... > When reading registry subkey values > > ... > String [] valstr = rksub.GetValueNames(); > foreach (String vs in valstr) { > String vstr = rksub.GetValue(vs).ToString(); // RETURNS > EXCEPTION > ... > } > > returns exception 'System.NullReferenceException' if reg value type is a > 'REG_FULL_RESOURCE_DESCRIPTOR' > > thanks for any input. > > > >
  • 3. Regexp Question: Two Nots Makes a Right to Left?
    There must be a simple regexp reason for this little question but it's driving me nuts. Below is a simple regexp to determine if a string contains only numbers. I'm running these two strings through the two very subtly different pieces of code: "0" and "0a" If I do the two "nots" on it, it works perfectly ("0" succeeds, "0a" fails). However, if I get rid of the two "nots", it appears to not do a global match properly on the string: "0" still succeeds, "a" fails, but "0a" also succeeds! Am I missing something simple? Most of the examples I see use the two "nots" which is not a problem at all - just understanding it would be nice! Thanks, Sped private void button1_Click(object sender, System.EventArgs e) { Regex reg=new Regex("[0-9]"); if (reg.IsMatch(textBox1.Text) ) { MessageBox.Show("Success!"); } else { MessageBox.Show("Failed"); } } private void button1_Click(object sender, System.EventArgs e) { Regex reg=new Regex("[^0-9]"); if (!reg.IsMatch(textBox1.Text) ) { MessageBox.Show("Success!"); } else { MessageBox.Show("Failed"); } }
  • 4. Need to call a C# DLL from WINAPI DLL
    I'm trying to extend the version of IAS server included in Windows Server 2003 with a custom DLL. IAS, unfortunately, insists on loading Extension DLLs with the Win32 LoadLibrary() function, which means that wrapping my DLL with COM or .Net is not supported. So, I'm wondering if there is a good way to call a C# method from an old school WINAPI DLL function. Any help would be greatly appreciated. Thanks, John
  • 5. propertygrid throwing error "Controls created on one thread cannot be parented to a control on a diffrent thread."
    Hi, hmmm... joys of multithreading :D well the gui seems to be very picky about what is done in which thread. Without knowing exactly what you're trying to do, its hard to give any exacy answers, but may i point you towards the Form.Invoke() method. What this does is invokes a given method from on thread in the thread of the Form (if that makes any sense). Check out the help about it. Greg "Pal B" < XXXX@XXXXX.COM > wrote in message news: XXXX@XXXXX.COM ... > prpertygrid throwing error "Controls created on one thread cannot be > parented to a control on a diffrent thread." > > any idea? any one? > > > > Thanx.PalB > >

How do I cancel an asynchronous operation?

Postby Jacob » Sun, 28 Dec 2003 13:16:18 GMT

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,

Re: How do I cancel an asynchronous operation?

Postby Dmitriy Lapshin [C# / .NET MVP] » Tue, 30 Dec 2003 21:36:35 GMT


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
Bring the power of unit testing to VS .NET IDE

Re: How do I cancel an asynchronous operation?

Postby Jacob » Wed, 31 Dec 2003 02:42:04 GMT

Well, that answers my question.  Thanks for your help.


"Dmitriy Lapshin [C# / .NET MVP]" < XXXX@XXXXX.COM > wrote


Re: How do I cancel an asynchronous operation?

Postby Chris Morse » Fri, 02 Jan 2004 14:21:58 GMT

On Mon, 29 Dec 2003 14:36:35 +0200, "Dmitriy Lapshin [C# / .NET MVP]"

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 {
        s.Listen(10);			// <-- typo in MSDN docs!

        while (true) {

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

    } catch (Exception e) {

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

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!


Similar Threads:

1.Cancel an Asynchronous operation?

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 to cancel the 
SQLCommand if the user wishes to?  The "Cancel" method states:

     ".......... The Cancel method cannot be used to cancel a pending 
asynchronous operation."

The scenario I'm thinking about here concerns a potentially long-running 
operation the user might start not realising how long it might take, despite 
a UI warning, then deciding to cancel it.  Because I don't know if the 
operation will take seconds or minutes, I can't set an appropriate timeout 
other than "0", which means the user must be allowed to cancel it.  Is there 
any way to stop an asynchronous operation "mid-flow" so to speak, without 
running it on a thread and aborting the thread (not a very clever thing to 
do)?  What about closing the underlying connection or generating an 
exception, will that stop the pending operation?


2.Cancel an asynchronous method call made to delegate using BeginInv

I would like to cancel a call to BeginInvoke of a delegate (i.e. kill the 
thread the call was made on). This would be done when a timeout occures for 
the waitone call to the WaitHandle object of the IAsyncResult interface 
returned by the BeginInvoke.

I have tried using the Close method of the WaitHandle object to end the call.

While I'm asking, please explain (in more detail than the MSDN Library) what 
the bool parameter of the waitOne method does.
Jeffrey Kingsley
Xerox Inovation Group
Xerox Corporation

3.KB192570 done right: Asynchronous sockets in threads

4.Newbie question on performing/handling Asynchronous operations..


   I have a dotnet form that basically writes to the webserver when a
button is clicked. Now based on the amount of data, the writing could
take about 5-10 minutes.

   I was wondering if there was a way for me to post a message back to
the user
as soon as the button is clicked and then have the webserver continue
to perform the operation. This way the user gets an immediate repsonse
saying that teh operationis being performed and does'nt have to wait
that long. This will also take care of potential timeout situations.

   Can someone give me some examples/ code snippets/links that will
help me out
if it indeed is possible. Please rememberthat I am new at this, so the
info the better and the more code with explanation the better.



5.asynchronous socket operations - question

I have a question about C#. How can I stop asynchronous read/write operation
( BeginReceive() / BeginSend() ) if timeout occurs?
The Socket class doesn't make any cancel method available.
I used CancelIo(HANDLE hFile) method (declared in Winbase.h) in C++.

Thanks for help.

6. asynchronous operations on objects

7. System.ComponentModel.Win32Exception: The operation was canceled by the user System.Windows.Forms.PrintControllerWithStatusDialog.OnStartPrint

8. Cancel a pending async operation?

Return to C#


Who is online

Users browsing this forum: No registered users and 52 guest