简体   繁体   中英

How to call a method Asynchronously?

I have created an application in windows form. Once I submit the application, the application get processed. I have created a class library which process the application and move the submitted application to different workflows. For this I have called the Class library from the click event of the Submit button. Everything is working fine, but the only problem is that once I submit the application and it calls the class library, it takes some time as it processes it. I want that the application should get closed and it calls the library method asynchronously. Below is the code:

private void OnPASubmit_Click(object sender, EventArgs e)
{
    if ((ApplAcct.AcctID == 0) || CheckForChanges())
    {
        UIHelper.ShowMessage("Please Save Application first");
        return;
    }
    try
    {
        if (!AOTHelper.ValidateCheckOut(ApplAcct.AcctID))
        {
            return;
        }
        WorkflowTask.PutAccountWorkflowTask(ApplAcct.AcctID, AOTHelper.FindAcctGUID(Main.objAccountGUID, Acct.AcctID), Environment.UserName, 2);
        AOTHelper.checkInAccount(ApplAcct.AcctID);
        AOTHelper.AccountToProcess(Acct.AcctID);
        UIHelper.ShowMessage("Application has been submitted for processing.");
        this.Close();
    }
    catch (Exception ex)
    {
         AOTHelper.WriteLog(ex, "Can not submit application for processing ");
    }

    // ...
}

The AotHelper.AccountToProcess(Acct.AcctID), method calls the class library and I want to do this with the help of Asunchronous calling so that the application doesn't have to wait for processing once it get submitted.

How will I do it. Please help!

Multiple ways to run asynchronous, such as TPL, starting your own thread (and in the 4.5 framework await), but for winforms perhaps the easiest way is to add a BackGroundWorker component. You can just drag/drop one from the toolbox on your designer.

Double clicking the added component, automatically creates a method that catches the DoWork event of the backgroundworker, you can place your code there. Then in the submit button you only have to call

  backgroundWorker.RunWorkerAsync();

然后你应该使用BackgroundWorker类。

You can use BackgroundWorker thread...

BackgroundWorker makes threads easy to implement in Windows Forms . Intensive tasks need to be done on another thread so that the UI doesn't freeze . It is necessary to post messages and update the user interface when the task is done.

When you use the BackgroundWorker class, you can indicate operation progress, completion, and cancellation in the user interface. For example, you can check whether the background operation is completed or canceled and display a message to the user.

Read a simple tutorial

Several ways. You can start a background worker thread that calls the process and just ends when it's done. You could create a delegate and use the BeginInvoke. You could send a message that a listener in a service picks up on a depatches a process to run it. Lots of ways to skin that cat.

Here is an old but useful MSDN ref http://msdn.microsoft.com/en-us/magazine/cc301332.aspx

You simply need to start it on a separate thread. For instance:

Thread thread = new Thread(new ParameterizedThreadStart(AOTHelper.AccountToProcess));
thread.Start(Acct.AcctID);

If you are going to be starting many threads at the same time, though, you should use a thread pool, instead.

You need to be careful, though. The method you call on the separate thread cannot do anything with the UI or it will throw an exception. If it needs to do something to the UI, it will need a reference to a form or control so it can call that object's Invoke method to get back on the UI thread. Also, since it is happening asynchronously, you will no longer be able to show that message box immediately after calling it because that will show immediately before the work is done.

I want that the application should get closed and it calls the library method asynchronously.

If you want to submit the data before the application is closed then modify the event that handles when a form is about to close. Please understand this event only happens if the form is closed by the user. If you want to cover when the process is forced to exit you have to subscribe to that event and so something similar.

Furthermore there are ways to close a process and for none of these events to happen. Basically this solution only works if the process reports back to Windows that it is closing.

Of course you shouldn't submit data asynchronously if your program's process is about to end.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM