首页 > 其他 > 详细

wcf使用task实现异步调用

时间:2016-02-25 22:37:03      阅读:284      评论:0      收藏:0      [点我收藏+]
private async void btnGetEmployees_Click(object sender, RoutedEventArgs e)
{
txtInfo.Text = "Data is Not Received Yet....";
MyRef.ServiceClient Proxy = new MyRef.ServiceClient();
var Result = await Proxy.GetEmployeesAsync();
dgEmp.ItemsSource = Result;
txtInfo.Text = "Data Received....";
}

自定义客户

If you open the proxy class, the method ‘GetEmployeeAsync’ is generated as follows:

public System.Threading.Tasks.Task<WPF_Client.MyRef.EmployeeInfo[]>
GetEmployeesAsync()
{
return base.Channel.GetEmployeesAsync();
}

或参考

http://www.codeproject.com/Articles/613678/Task-based-Asynchronous-Operation-in-WCF

class Program
 {
    static void Main(string[] args)
    {
       GetResult();
       Console.ReadLine();
    }

    private async static void GetResult()
    {
       var client = new Proxy("BasicHttpBinding_IMessage");
       var task = Task.Factory.StartNew(() => client.GetMessages("Hello"));
       var str = await task;
       str.ContinueWith(e =>
       {
          if (e.IsCompleted)
           {
              Console.WriteLine(str.Result);
           }
       });
      Console.WriteLine("Waiting for the result");
    }
 }


public Task<string> GetMessages(string msg)
    {
      return Channel.GetMessages(msg);
    }

 

wcf使用task实现异步调用

原文:http://www.cnblogs.com/zeroone/p/5218498.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!