Click here to Skip to main content
12,822,179 members (42,016 online)
Rate this:
Please Sign up or sign in to vote.
See more: VB VB.NET
Can anybody tell me how to abort the asynchronous operation?

Problem such.

I have created the application which gathers some information from different databases. And this process can be very durable, therefore I call procedure of collection asynchronous that there was a possibility it to interrupt at any time.

That I have made:
Public class Manager

    Public Sub ImportTransfers
        'Collect information
    End Sub

End class

Public Class frm_Main

    Delegate Sub dlg_Importer()
    Dim Importer As dlg_Importer
    Dim CallBackImport As AsyncCallback

    Private Sub frm_Main_Load
          Call Init_Delegates()
    End Sub

        Private Sub Init_Delegates()
          Importer = AddressOf Manager.ImportTransfers
          CallBackImport = AddressOf CallBack_Import
        End Sub

        Private Sub btn_ImportTransfers_Click
          Importer.BeginInvoke(CallBackImport, Nothing)
    End Sub

        Sub CallBack_Import(ByVal ar As IAsyncResult)
        End Sub

        Private Sub btn_StopProcess_Click
           'I do not know what to write here to stop process...
        End Sub

End Class

The CallBack_Import procedure is fulfilled after ImportTransfers procedure end, but how to stop ImportTransfers by button btn_StopProcess Click?
Posted 26-Apr-10 0:49am
Updated 26-Apr-10 1:10am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Makso, I don't think that asynchronous call will solve your problem. The only solution you have is to send a reference of that asynchronous thread back to the main thread, a very very dirty solution.

What I mean is setting up a form level variable
System.Threading.Thread asynchOperation;

And in your asynchronous method just set it like this.
this.asynchOperation = System.Threading.Thread.CurrentThread;

And then in your btn_StopProcess_Click()

Also don't forget to wrap your asynchronous operation in try catch and catching System.Threading.ThreadAbortException.

But i will strictly advise against this solution. Aborting a ThreadPool thread is not an idea, anyone will favor. It's reported to be recreated anyway. I myself has aborted as an experiment about 70 threads and everytime i got back a thread with managedID 6. I haven't confirmed the unmanaged status, but at least it was recreating these threads.

A better solution would be using Thread class Explicitly

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170308.1 | Last Updated 26 Apr 2010
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100