multiple threads calling wcf service

below is my simple code to start 5 threads, each one calls a wcf service which returns the value sent in, my problem is that the :

public void clien_GetDataCompleted(object sender, GetDataCompletedEventArgs e)
        {
            lock (sync)
            {
                count += e.Result;
            }
        }

works ok and increments the count, but how do i capture when all the threads have completed, does anybody have simple example code on how to call multiple wcf services which use async methods.

public partial class Threading : Form
{
        public int count;
        ServiceReference1.Service1Client clien = new ServiceReference1.Service1Client();

        public Threading()
        {
            InitializeComponent();
        }

        private void GetData()
        {
            clien.GetDataAsync(1);
        }

        public void DisplayResults()
        {
            MessageBox.Show(count.ToString());
        }

        private object sync = new object();

        public void clien_GetDataCompleted(object sender, GetDataCompletedEventArgs e)
        {
            lock (sync)
            {
                count += e.Result;
            }
        }

        public List<Thread> RunThreads(int count, ThreadStart start)
        {
            List<Thread> list = new List<Thread>();
            for (int i = 0; i <= count - 1; i++)
            {
                dynamic thread = new Thread(start);
                thread.Start();
                list.Add(thread);
            }
            return list;
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            clien.GetDataCompleted += new EventHandler<GetDataCompletedEventArgs>(clien_GetDataCompleted);
            ThreadStart WcfCall = new ThreadStart(GetData);
            IList<Thread> threads = RunThreads(5, WcfCall);
        }

    }

many thanks

Answers


If you are using .NET 4.0 you can use Task Parallel Library (TPL) and use Tasks instead of Threads. Tasks has more flow control. What you can do with tasks something like

  // Wait for all the tasks to finish.
  Task.WaitAll(tasks);

Here is example on how to use Tasks and wait for all tasks to finish. here


I have implemented the solution using tasks, the code is below, its works well, let me know if theres any improvement i could do.

public partial class Tasks : Form
{
    static ServiceReference1.Service1Client clien = new ServiceReference1.Service1Client();
    int count = 0;

    public Tasks()
    {
        InitializeComponent();
    }

    // Define a delegate that prints and returns the system tick count
    Func<object, int> action = (object obj) =>
    {
        int i = (int)obj;
        clien.GetDataAsync(i);

        Console.WriteLine("Task={0}, i={1}, Thread={2}", Task.CurrentId, i, Thread.CurrentThread.ManagedThreadId);

        return i;
    };

    public void clien_GetDataCompleted(object sender, GetDataCompletedEventArgs e)
    {
        count += e.Result;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        const int n = 5;

        // create async callback delegate from wcf.
        clien.GetDataCompleted += new EventHandler<GetDataCompletedEventArgs>(clien_GetDataCompleted);

        // Construct started tasks
        Task<int>[] tasks = new Task<int>[n];
        for (int i = 0; i < n; i++)
        {
            tasks[i] = Task<int>.Factory.StartNew(action, i);
        }

        try
        {
            // Wait for all the tasks to finish.
            Task.WaitAll(tasks);

            MessageBox.Show(count.ToString());
        }
        catch 
        {

        }

    }
}

cheers


Need Your Help

Open an Oracle .dmp file from excel 2007

oracle excel excel-2007 dmp

I have an oracle .dmp file which I would like to open from excel 2007. How do I use the 'get external data' feature to do so? Is it possible in the first place?

SQL Delete based on condition in join

sql join sql-delete

It is possible to delete records based on a satisfied condition with a join query?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.