Click here to Skip to main content
15,884,298 members
Articles / Programming Languages / C#
Article

Advanced Asynchoronous WebServices

Rate me:
Please Sign up or sign in to vote.
4.00/5 (20 votes)
20 Apr 20044 min read 79.1K   410   35   12
Advanced asynchoronous WebServices.

Introduction

This application shows how to use asynchronous web services in your application and how you can use the callback methods in your application. I made this application to calculate the speedup for the case when you want to run long processes, in different machine versus single machine.

This machine displays which job is running, which task and latest task display in red color.

Image 1

My Approach

I used .NET/ C# / SOAP and web services technique to implement this. The neat part of using asynchronous .NET web services is that they call the callback function when the function at the other machine is completed, which solves lots of complicacy from the project. Specially, when we need to dispatch 400 jobs, then I just check about which machine calls the callback, means that machine is free now, so I send the next task dispatch to that machine.

Asynchronous Web Service

When you create any web method, it creates an asynchronous method also for you, which has the “Begin” keyword before the web method name.

For example, I have SortMachine1 web method, so I call it like this:

C#
BeginSortMachine1(parameter,CallBackFunction,0);

This is a call back function, when an asynchronous web service completes, this function gets called.

C#
public void OnCBMachine1(IAsyncResult ar)
{
  try
  {
    if (false == bIsBtn400Clicked)
    {
      mylist1[0] = st.EndSortMachine1(ar);
      bMachineDone[0] = true; 
      eTime[0] = System.DateTime.Now;
      timeDiff[0] = eTime[0].Subtract(sTime[0]);
      lblMachine1Time.Text = timeDiff[0].TotalMilliseconds.ToString();
      lblMsg1.Text = "Finished Machine 1 Job.";

      if ( bMachineDone[1] == true && bMachineDone[2] == true && 
                                           bMachineDone[3] == true )
      {
        timeDiff[4] = eTime[0].Subtract(sTime[0]);
        lblTimeTaken.Text = timeDiff[4].TotalMilliseconds.ToString();
        double dTime = timeDiff[4].TotalMilliseconds / Int32.Parse(datasize4.Text);
        avg4.Text = dTime.ToString();
        task4.Text = (timeDiff[4].TotalMilliseconds / 4).ToString();
        lblInfo.Text = "Job Completed";
      }
    }
    else
    {
      mylist400[nCounterMachine1] = st.EndSortMachine1(ar);
      bMachineDone[0] = true;
      eTime[0] = System.DateTime.Now;
      timeDiff[0] = eTime[0].Subtract(sTime[0]);
      TotalTimeFor400Tasks += timeDiff[0].TotalMilliseconds;
      lblMachine1Time.Text = timeDiff[0].TotalMilliseconds.ToString();
      lblMsg1.Text = "Finished Machine 1 Job # " + (nCounterMachine1+1).ToString();

      lblTimeTaken.Text = TotalTimeFor400Tasks.ToString();
      double dTime = TotalTimeFor400Tasks / Int32.Parse(datasize400.Text);
      avg400.Text = dTime.ToString();
      task400.Text = (TotalTimeFor400Tasks / nCurrentJobIndex).ToString();
      if ( nCurrentJobIndex == 400)
      {
        lblInfo.Text = "Job Completed";
        dTime = TotalTimeFor400Tasks / Int32.Parse(datasize400.Text);
        avg400.Text = dTime.ToString();
        task400.Text = (TotalTimeFor400Tasks / nCurrentJobIndex).ToString();
      }
      else
        ServiceJobs();
    }
  }
  catch(Exception ex)
  {
    lblInfo.Text = ex.Message.ToString();
  }
}

Part of the Projects

I created the project in two parts.

Server: This is an XML web service, which has four web methods, that sorts the data using (O)n2 algorithm. Creating and XML web service is a very easy task in .NET. Tricky part is, I want to use these methods asynchronously. When you create a web service in .NET, it creates that method asynchronous and synchronous both. All you need to add is a Begin keyword, if you want to use that method as asynchronous. Also, you need to assign a callback function, which is called by web method, when web method is completed.

I create XML web services that are running on four different machines, and these web services contain the function for the sorting of data (On2) algorithm. This web service is called DataProcess and has four functions.

Image 2

Client Application (Windows Based Application): This is the second application I made. This is a simple Windows based application in C# .NET. And I added web reference for the services I put on the different computer on my office LAN. Searching for all the available web services is very easy in .NET.

Screen shot of an application:

Image 3

Clicking on Add Reference button will show up this dialog:

Image 4

If you click on local machine, it searches in your machine and gives you a list of web services available on your computer.

It’s a Windows based application. It’s a simple application. It has 3 options:

  • Single thread : This means application runs on same machine as a full task and estimates the time.
  • 4 tasks : Since we have four machines, create 4 tasks, and assign the job to each machine and start the timer, and when all the jobs are finished then count that time. Subtract this time from start time, that is total time taken by this method.
  • 400 Tasks : This is a little tricky part. I solved this part in the following order. Create 400 tasks.
    1. Start the timer.
    2. Send first four tasks to four machines, and keep the counter of current job.
    3. We call all jobs asynchronously, so as soon as I get a callback function from machine, I send another job on line and increment the counter.
    4. Keep doing this until counter reaches 400 (means all jobs are dispatched).
    5. Now wait for last job to complete and as soon as you get that callback, mark that time.
    6. Take a difference of this time from the start time, that is your total time taken from this method.

Analysis

  • Single machine Test Analysis:

    All times are in milliseconds.

    Image 5

    Image 6

    Single Machine Test

    When I first read the problem, I assumed the performance for 400 tasks will be better than 4 task, even for single machine. But the results are completely opposite. Reason: When I run 400 tasks on the same machine, it takes up the whole CPU time, CPU usage is 100%, which dramatically slows down the whole system performance. This project is designed to run on 4 different machines, so you will not achieve the result by running on the same machine.

  • Multiple machine Test Analysis:

    Image 7

    Image 8

    In this case, we gets much better results for 400 tasks, but for 4 tasks, it's not much better improvement. I think there are two reasons for that.

    1. For four tasks, we create a connection object for each machine, so that we can call the web service, which introduces a slowdown for the first call.
    2. Each slave machine called the function only one time, so nothing is in cache at this point.

    Reason of getting better results in case of 400 tasks.

    1. We create a connection object only once and than keep calling the web service again and again, which enhances the performance.
    Tt  = Total time to execute job one as single program.
    Tp = Time of that part of job, that can be broken 
                             down into independent tasks.
    Speed Up =  S(k) = Tt / Tk = 7232.6224 / 2214.7874 = 3.265

    Theoretical speedup:

                     1
    S(k) = ---------------------
               (1-b) + b/k
    
    Here K = 4, lets assume b = 1, which is an ideal case, 
                               when everything is distributed.
    Then 
    S(k) = 4.
    
    If b = .96
    S(k) = 1/ (.04+ .24) = 1/ .28 = 3.57

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States

Comments and Discussions

 
GeneralShowing error in <authentication mode="Windows" /> Pin
rajasingam28-Apr-08 23:52
rajasingam28-Apr-08 23:52 
GeneralForm freezes on async webservice call Pin
Talal Sultan6-Jun-06 20:44
Talal Sultan6-Jun-06 20:44 
GeneralRe: Form freezes on async webservice call Pin
Shail_Srivastav8-Sep-06 3:58
Shail_Srivastav8-Sep-06 3:58 
QuestionIt is not running on my machine??? Pin
Michellesfs21-Oct-05 1:20
sussMichellesfs21-Oct-05 1:20 
AnswerRe: It is not running on my machine??? Pin
Shail_Srivastav21-Oct-05 4:34
Shail_Srivastav21-Oct-05 4:34 
GeneralPerfomance Question Pin
is0butane4-Apr-05 12:04
is0butane4-Apr-05 12:04 
GeneralGreat Article Pin
Jeffrey Scott Flesher13-Sep-04 18:58
Jeffrey Scott Flesher13-Sep-04 18:58 
GeneralRe: Great Article Pin
Shail_Srivastav14-Sep-04 9:21
Shail_Srivastav14-Sep-04 9:21 
GeneralHere's one Pin
Andrew McCarter21-Apr-04 8:48
Andrew McCarter21-Apr-04 8:48 
GeneralRe: Here's one Pin
Anonymous22-Apr-04 5:12
Anonymous22-Apr-04 5:12 
GeneralRe: Here's one Pin
Andrew McCarter22-Apr-04 7:30
Andrew McCarter22-Apr-04 7:30 
GeneralRe: Here's one Pin
Nish Nishant29-Apr-04 1:33
sitebuilderNish Nishant29-Apr-04 1:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.