none
Creating an async method RRS feed

  • Question

  • It is easy to create an async method if my method calls an async method in the .Net framework.

    If my method uses a class in .Net framework that does not have an async method, is there anything I can do to make my method async?


    Certified Geek

    Monday, May 20, 2013 4:01 PM

Answers

  • I would never fly an airplane the used Microsoft Windows.  The Timer Tick Service is automatically started by Windows and Windows would not work without it.  You don't program the Timer Ticker Service directly.  All you do is register Events in your VS application.

    C# - By registering the event handler using '+=' , or remove the handler using '-='

    VBnet - Use the keyword HANDLE on the declaration of the function.


    jdweng

    • Marked as answer by Arne MN Tuesday, May 21, 2013 1:30 PM
    Tuesday, May 21, 2013 1:10 PM

All replies

  • A Class is automatically ASYNC as long as you don't have any blocking functions.  Make sure the Class has Event(s) and they are registered in the class constructor.  windows wil automatically execute the class code when the Event(s) occur.

    jdweng

    Monday, May 20, 2013 9:16 PM
  • Some framework classes offer async methods, but many classes only have blocking methods.

    What kind of events are you talking about?


    Certified Geek

    Tuesday, May 21, 2013 11:56 AM
  • Lets not worry about methods or blocking.  Lets just talk about building a class library that runs asynchronously.  The class needs two items

    1) A constructor that initializes (register in windows) any Events

    2) Event Handler(s)

    The reason it runs asynchronously in Windows (the timer tick service), the class is allowed to register new events and then when the event occurs the timer tick calls the function.  When any event finishes processing it returns to the timer tick service which starts next process or handles another event.  So as long as nothing is blocking the function runs asynchronously.

    In a blocking function the code sits and waits for an event to complete before proceeeding. Windows is a real time operating system and is swapping processes all the time so many functions can be blocking simultaneously while other processes continue to run.


    jdweng

    Tuesday, May 21, 2013 12:45 PM
  • Joel,

    That is a very interesting answer. How do I program a timer tick service?

    If window is a real time operating system, would you fly an airplane that is operated by windows?


    Certified Geek


    • Edited by Arne MN Tuesday, May 21, 2013 1:04 PM
    Tuesday, May 21, 2013 1:03 PM
  • I would never fly an airplane the used Microsoft Windows.  The Timer Tick Service is automatically started by Windows and Windows would not work without it.  You don't program the Timer Ticker Service directly.  All you do is register Events in your VS application.

    C# - By registering the event handler using '+=' , or remove the handler using '-='

    VBnet - Use the keyword HANDLE on the declaration of the function.


    jdweng

    • Marked as answer by Arne MN Tuesday, May 21, 2013 1:30 PM
    Tuesday, May 21, 2013 1:10 PM
  • Joel,
    Can you provide a link to some sample code please?


    Certified Geek

    Tuesday, May 28, 2013 12:03 PM
  • Below the Form class is an async class.  When a load event occurs it processes the event and then returns.  Nothing is blocking.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    namespace ConsoleApplication1
    {
        class Program
        {
            [STAThread]
            static void Main(string[] args)
            {
                Form1 myClass = new Form1();
     
            }
        }
        partial class Form1 : Form
        {
            public Form1()
            {
                this.Load +=new EventHandler(Form1_Load);
            }
            
            private void Form1_Load(object sender, EventArgs e)
            {
                
            }
        }
    }


    jdweng

    Tuesday, May 28, 2013 12:59 PM
  • I understand how to create an event handler.

    How can I use an event handler to create an asyncrhonous method?


    Certified Geek

    Tuesday, May 28, 2013 1:03 PM
  • An event handle is an async method.  What do you want to use as a trigger for the event?  I often use a  System.Threading.EventWaitHandle.  The trigger can be a timer, a stream event, a port event, or a semiphore like the EventWaitHandle.  The EventWaitHandle can wait for a signal from another async process before continueing.

    What make a process async is simply nothing blocks the code from running, and these is a mechanism for running the process.  You can either call the process (class) from another process (class) or use an event to run the code.   The theory is not complicated.  You have to come up with a design concept for implimenting the async process.  The first thing you have to consider is what/when the async process is going to run.


    jdweng

    Tuesday, May 28, 2013 1:15 PM
  • I am writing an WPF application and some times a classic winforms application.
    The operator clicks a button and the application starts a long running task. The button event should not block the application. If the long running task calls a .NET method that is asynchronous it is a very easy the mark the  button event with the async modifier. If the long running task calls a .Net method that is synchronous I have to find a different method to make the long running task asynchronous.
    You suggest using an event handler. So far I have not been able to make the jump from theory to practical coding.

    Certified Geek

    Tuesday, May 28, 2013 1:28 PM
  • The async event simply has to return after a running a certain amount of time.  For example if you had a math function running one million times.  You may want to break every thousand operations.  Then set a timer to resume after 2 seconds.  The 2 second times would be the trigger.

    If you were processing an input stream you would set an event indicating when the stream had data.  Then after you hit a underflow (no data in the input stream) you can return.  The next time the stream had data your code would resume because the event would be the trigger.


    jdweng

    Tuesday, May 28, 2013 1:50 PM
  • I don't understand Joel in this thread either.  If you want an asynchronous method, you have to run the method on a thread different from the calling thread.   

    "If my method uses a class in .Net framework that does not have an async method, is there anything I can do to make my method async?"

    Call it from a thread.  If you have a UI, simplest is to use the BackgroundWorker.

    Tuesday, May 28, 2013 2:59 PM
  • There is no difference between a thread and a class when you are refereing to Async.  Using Backgroundworker will just create another process.  The processing of the event is identical.

    jdweng

    Tuesday, May 28, 2013 3:14 PM
  • The only thing I can report back from a Backgroundworker is an integer which can be used with a progress bar.

    With an async method I can pass in a delegate which can be used to report back any data I like.

    I understand how to create a thread, but I prefer to use an async method.


    Certified Geek

    Tuesday, May 28, 2013 3:48 PM
  • See if the webpage below helps.

    http://msdn.microsoft.com/en-us/library/vstudio/hh191443.aspx

    The integer is really just the handle which is the process id number.  Again the async is just creating a new process and you don't need the new process to process events.  The only time you neeed a new process is you are trying to run more than one thread in parallel.  If you PC has more than one core the parallel threads will run in parallel on seperate cores.  If you just have a form that is waiting for a thread to complete then the async method doesn't do anything for you.


    jdweng

    Tuesday, May 28, 2013 3:58 PM
  • "The only thing I can report back from a Backgroundworker is an integer which can be used with a progress bar."

    The BackgroundWorker wraps an AsyncOperation.  It not only reports the results of the operation in the Completed event, but provides for intermediate return of progress and data in the ProgressChanged event.  The use of the ProgressChanged event is not mandatory.

    Tuesday, May 28, 2013 4:04 PM