Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Terminating asynchronous delegate threads when not complete

I have C# code here:

    dlgCardLimit dlg = new dlgCardLimit(GetCardLimit);
    IAsyncResult res = dlg.BeginInvoke(cardNo, null, null);
    res.AsyncWaitHandle.WaitOne(timeOut);
    if (res.IsCompleted)
    {
        cardLimit = dlg.EndInvoke(res);
    }
    else
    {
        //Terminate the async thread.
    }

In "Else" case, how can I terminate the thread that IAsyncResult is pointing?

like image 333
Ahmet Altun Avatar asked Mar 20 '26 07:03

Ahmet Altun


1 Answers

Your dlgCardLimit class should have another method that would allow a graceful cancellation of the pending operation, such as Cancel or Close so as to give a chance for Gracefully handling the error. The thread on which this is running is hidden from you.

Checkout the Socket.EndReceive for a similar pattern in cancelling a pending operation.

like image 145
Anastasiosyal Avatar answered Mar 22 '26 21:03

Anastasiosyal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!