none
difference between synchronous and asynchronous programming

    Question

  • this is the synchronous and asynchronous programming examples.

    in below program both asynchronous and synchronous methods are executing simillarly, i cant understand what is the main difference between two methods.

    plzzzzz ... explain.

    using System;

    using System.Text;

    using System.Threading;

    using System.Collections;

    using System.Windows.Forms;

     

    namespace practise

    {

    class Program

    {

    string longrunningmethod(int icalltime,out int iexethread)

    {

    Thread.Sleep(icalltime);

    iexethread = AppDomain.GetCurrentThreadId();

    return "mycall time was" + icalltime.ToString ();

    }

    delegate string methoddelegate(int icalltime, out int iexethread);

    public void asyncall()

    {

    string s;

    int iexecthread;

    methoddelegate obj = new methoddelegate(this.longrunningmethod);

    IAsyncResult ar = obj.BeginInvoke(5000, out iexecthread, null, null);

    s = obj.EndInvoke(out iexecthread, ar);

    Console.WriteLine(String.Format("the delegate call return the string: \"{0}\", and the thread ID {1}", s, iexecthread.ToString()));

    }

    public void syncall()

    {

    string s;

    int iexecthread;

    methoddelegate obj = new methoddelegate(this.longrunningmethod);

    s = obj(5000, out iexecthread);

    Console.WriteLine(String.Format("the delegate call returns the string: \"{0}\" and the thread id {1} ", s, iexecthread.ToString()));

     

    }

    static void Main(string[] args)

    {

     

    Program ad = new Program();

    ad.asyncall();

    ad.syncall();

    }

    }

    Friday, April 18, 2008 4:03 AM

Answers

  • Well, one of the main points of asynchronous programming is that while something is running, you can go do something else while you wait for it to complete.

    For instance, let's say you're reading images off of a disk, and submitting them to a web server. With synchronouse programming, you could do this:

    - read image 1
    - submit image 1
    - read image 2
    - submit image 2
    - read image 3
    - submit image 3
    - etc.

    However, with asynchronous programming, you could do this:

    - read image 1
    - submit image 1, read image 2
    - submit image 2, read image 3
    - submit image 3

    this is just using 2 threads, with more knowledge about the cpu you're running on you might be able to increase this:

    - read image 1
    - submit image 1, read image 2 and 3
    - submit image 2 and 3

    which would use 3 threads.

    If all you're going to do is start the thread and wait for it to complete, then you're only adding overhead, which was your example.
    Friday, April 18, 2008 7:15 AM

All replies

  • Well, one of the main points of asynchronous programming is that while something is running, you can go do something else while you wait for it to complete.

    For instance, let's say you're reading images off of a disk, and submitting them to a web server. With synchronouse programming, you could do this:

    - read image 1
    - submit image 1
    - read image 2
    - submit image 2
    - read image 3
    - submit image 3
    - etc.

    However, with asynchronous programming, you could do this:

    - read image 1
    - submit image 1, read image 2
    - submit image 2, read image 3
    - submit image 3

    this is just using 2 threads, with more knowledge about the cpu you're running on you might be able to increase this:

    - read image 1
    - submit image 1, read image 2 and 3
    - submit image 2 and 3

    which would use 3 threads.

    If all you're going to do is start the thread and wait for it to complete, then you're only adding overhead, which was your example.
    Friday, April 18, 2008 7:15 AM
  •  

    thank you lasse
    Friday, April 18, 2008 8:16 AM
  • A very good place to start learning asynchronous programming is the Backgroundworker class.

    It will provide you with functionality for most of your async requirements and help to get you head wrapped around the whole async idea - I know it helped me alot.

     

    Friday, April 18, 2008 9:03 AM
  • Hi Jeevan Mummadi,

     

    The basic difference between synchronous and asynchronous programming is that if you call synchronous ( ad.syncall(); method ) that time your application's compiler have to wait till, ad.syncall() method does not return the Acknowledgement, after returning acknowledgement your compiler will going next instrucion. In Asynchronous ( ad.asyncall(); method ) that time not necessary to wait for any acknowledgement.


    Note : In Synchronous  programming we have to always wait for return Acknowledgement. and  Asynchronous programming you can call another method symultaniously.

     


    Hope that, this may be help.

     

    Friday, April 18, 2008 11:26 AM
  • thank you pavan...

    Saturday, April 19, 2008 4:18 AM
  • Once you start doing real work asynchronously, you have to be aware that you cannot update GUI elements in a different thread than the GUI thread.

    The moment you do a beginInvoke, the framework creates a thread in which the execution will continue of the thing you are invoking, in parallel with the normal execution.

    But many people, you can find the questions in this newsgroup, then try to update some kind of GUI element and will get an exception.

    The backgroundworker is a good alternative, in case you want to show progress in a progressbar in your GUI.

    Another way is to use a check : InvokeRequired in combination with reinvoking on a GUI element: this.beginInvoke(...)

     

    You might not need this at the point where you are now, but please remember, you surely going to reach this point somewhere in the future, and then I hope you remember this and know where to look.

    Saturday, April 19, 2008 7:50 AM