小编Ana*_*tha的帖子

在WPF应用程序中使用任务和回调实现自定义异步WCF调用处理时,UI冻结

我有一个WPF MVVM应用程序.视图模型具有绑定到视图的几个属性,这些属性由来自数据库的数据直接填充,或者通过驻留在视图模型和数据库之间的wcf服务填充.数据连接模式的选择取决于客户端应用程序的App.config文件中的应用程序设置.我想实现我自己的异步调用服务方法并处理它们的返回值的方法.如果我使用Tasks以下列方式实现它,我想知道是否存在线程问题的可能性:

服务调用流:ViewModel> ServiceAgent>(MyWCFServiceClient或MyBusinessClient)> MyBusinessClass> Database Inorder使用服务操作我有一个实现IMyWCFService的MyWCFServiceClient类(在添加服务引用时生成).

此外,我有一个MyBusinessClassClient类,它从同一个IMyWCFService接口实现.因此,MyWCFService和MyBusinessClient都具有相同的方法签名.我选择在生成服务客户端时不生成任何异步方法,因为,如果我这样做,我可能需要在MyBusinessClient中实现由IMyWCFService生成的许多不必要的东西.

假设我有一个方法GetEmployee(int id),它返回一个在IMyWCFService中定义的Employee对象.因此,MyWCFServiceClient和MyBusinessClient类都将具有其实现.

在我的ViewModel中,我有:

private void btnGetEmployee_Click()
        {
            ServiceAgent sa = new ServiceAgent (); 

            //this call/callback process the service call result

            sa.GetEmployee(1673, (IAsyncResult ar) =>
            {
                Task<Employee> t1 = (Task<Employee>)ar;
                Employee = t1.Result;
                //do some other operation using the result
                //do some UI updation also
            });
        }  


        //this property is bound a label in the view
      private Employee _employee;
        public Employee Employee
        {
            get
            {
                return _ employee;
            }
            set
            {
                _ employee …
Run Code Online (Sandbox Code Playgroud)

c# wpf wcf asynchronous task-parallel-library

7
推荐指数
1
解决办法
1756
查看次数

标签 统计

asynchronous ×1

c# ×1

task-parallel-library ×1

wcf ×1

wpf ×1