Skip to content

C# .NET 2.0 Web Service - Sync + Async

Published: 2007-10-19

Git Repository

I wanted to learn how to use web services in .NET with C#, so I created this tiny program that goes out and gets the "Dilbert of the Day" comic strip and puts it on your desktop in the form of a jpeg file.

There are two versions to this program. One calls the web service synchronously, while the other calls the web services asynchronously. Synchronous calls are what most programmers would be familiar with. When a synchronous function/method is called it doesn't return control back to the calling program until it's finished, while an asynchronous function/method returns control immediately while it runs in a separate thread.

The synchronous version of this program is much simpler than the asynchronous version, but the later allows for more flexibility. For example, in the asynchronous version the program can display a progress indicator while it's waiting for the call to complete.

You could use the synchronous call to the web service and put it in a separate thread yourself. You would achieve the same result as the asynchronous call. It just makes the code a little longer and slightly more complex.

Synchronous Code

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using DilbertADay.com.esynaps.www;

namespace DilbertADay
{
    class Program
    {
        public static void Main(string[] args)
        {
            DailyDilbert wsDd;
            Byte[] imageData;
            MemoryStream strm;
            Bitmap bmp;           

            try
            {
                wsDd = new DailyDilbert();
                imageData = wsDd.DailyDilbertImage();
                strm = new MemoryStream(imageData);
                bmp = new Bitmap(strm);
                bmp.Save(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)
                    + "\\dilbert.jpg",ImageFormat.Jpeg);
                bmp.Dispose();
                strm.Close();
                wsDd.Dispose();
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
            }
        }
    }
}

Asynchronous Code

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using DilbertADay.com.esynaps.www;

namespace DilbertADay
{
    class Program
    {
        public static int isCompleted = 0;
        public static Byte[] imgData;
        public static int timeout = 10000;

        public static void Main(string[] args)
        {
            DailyDilbert wsDd;
            MemoryStream strm;
            Bitmap bmp;

            int start, currentSecond;

            try
            {
                wsDd = new DailyDilbert();
                wsDd.DailyDilbertImageCompleted += new DailyDilbertImageCompletedEventHandler(
                    Program.DailyDilbertImageCompleted_Handler);
                wsDd.DailyDilbertImageAsync();

                start = DateTime.Now.Millisecond;
                currentSecond = start;

                Console.WriteLine("Sending request");
                while (Program.isCompleted == 0 && (currentSecond - start) < Program.timeout)
                {
                    if (currentSecond < DateTime.Now.Millisecond)
                    {
                        currentSecond = DateTime.Now.Millisecond;
                        Console.Write(".");
                    }
                }
                Console.Write("\n");

                if (Program.isCompleted == 1)
                {
                    Console.WriteLine("Generating image");
                    strm = new MemoryStream(Program.imgData);
                    bmp = new Bitmap(strm);
                    bmp.Save(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)
                        + "\\dilbert.jpg", ImageFormat.Jpeg);

                    bmp.Dispose();
                    strm.Close();
                    wsDd.Dispose();
                }
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
            }
        }

        public static void DailyDilbertImageCompleted_Handler(object sender, DailyDilbertImageCompletedEventArgs args)
        {
            try
            {
                Program.imgData = args.Result;
                Program.isCompleted = 1;
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
                Program.isCompleted = 2;
            }
        } 
    }
}