多个线程调用wcf服务

时间:2010-12-02 14:16:28

标签: multithreading wcf

下面是我的简单代码,启动5个线程,每个调用一个wcf服务,返回发送的值,我的问题是:

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

工作正常并增加计数,但是如何在所有线程完成时捕获,是否有人有关于如何调用使用异步方法的多个wcf服务的简单示例代码。

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);
        }

    }
非常感谢

2 个答案:

答案 0 :(得分:0)

如果您使用的是.NET 4.0,则可以使用任务并行库(TPL)并使用Tasks而不是Threads。任务有更多的流量控制。您可以使用

等任务执行的操作
  // Wait for all the tasks to finish.
  Task.WaitAll(tasks);

以下是有关如何使用Tasks并等待所有任务完成的示例。 here

答案 1 :(得分:0)

我已经使用任务实现了解决方案,代码如下,其运行良好,如果我能做任何改进,请告诉我。

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 
        {

        }

    }
}

欢呼声