Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create an asynchronous method

I have simple method in my C# app, it picks file from FTP server and parses it and stores the data in DB. I want it to be asynchronous, so that user perform other operations on App, once parsing is done he has to get message stating "Parsing is done".

I know it can achieved through asynchronous method call but I dont know how to do that can anybody help me please??

like image 338
Prashant Cholachagudda Avatar asked Jul 21 '09 13:07

Prashant Cholachagudda


People also ask

How do you create asynchronous method?

You can use await Task. Yield(); in an asynchronous method to force the method to complete asynchronously. Insert it at beginning of your method and it will then return immediately to the caller and complete the rest of the method on another thread.

How do you create asynchronous method in Java?

Asynchronous Callback So in the context of Java, we have to Create a new thread and invoke the callback method inside that thread. The callback function may be invoked from a thread but is not a requirement. A Callback may also start a new thread, thus making themselves asynchronous.

How can create async method in C#?

A method in C# is made an asynchronous method using the async keyword in the method signature. You can have one or more await keywords inside an async method. The await keyword is used to denote the suspension point. An async method in C# can have any one of these return types: Task, Task<T> and void.

How do you run asynchronous method?

The simplest way to execute a method asynchronously is to start executing the method by calling the delegate's BeginInvoke method, do some work on the main thread, and then call the delegate's EndInvoke method. EndInvoke might block the calling thread because it does not return until the asynchronous call completes.


1 Answers

You need to use delegates and the BeginInvoke method that they contain to run another method asynchronously. A the end of the method being run by the delegate, you can notify the user. For example:

class MyClass {     private delegate void SomeFunctionDelegate(int param1, bool param2);     private SomeFunctionDelegate sfd;      public MyClass()     {         sfd = new SomeFunctionDelegate(this.SomeFunction);     }      private void SomeFunction(int param1, bool param2)     {         // Do stuff          // Notify user     }      public void GetData()     {         // Do stuff          sfd.BeginInvoke(34, true, null, null);     } } 

Read up at http://msdn.microsoft.com/en-us/library/2e08f6yc.aspx

like image 191
Callum Rogers Avatar answered Sep 21 '22 11:09

Callum Rogers