Windows Phone C#中的多线程? [英] Multi threading in windows phone C# ?

查看:97
本文介绍了Windows Phone C#中的多线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Windows Phone中的多线程C#?



当我调用wcf服务时如何进行多线程处理。一个线程应该调用服务,一个线程应该显示处理。

解决方案

嗨。我建议你使用Reactive Extensions for windows phone。

借助这个第三方库Rx(顺便由LINQ创始人之一编写),您可以在一个线程上调用服务并在另一个线程上观察结果。



例如,让我们从服务中获取json数据:



 WebClient webClient =  new  WebClient(); 
webClient.Headers.Add( uid,LoginData.LoginName);
webClient.Headers.Add( pass,LoginData.Password);
webClient.Headers.Add( logonType 0);
var eventStream = Observable.FromEventPattern< DownloadDataCompletedEventArgs>(webClient, DownloadDataCompleted)。
SubscribeOn(Scheduler.NewThread).Select(newData = > newData.EventArgs.Result);

eventStream.ObserveOn(System.Threading.SynchronizationContext.Current).Subscribe(OnDatareceived,
// 有错误
ex = >
{
System.Windows.MessageBox.Show( ex.Message);
ViewModel.ViewModelLocator.Logger.Error( string .Empty,ex);
});

webClient.DownloadDataAsync( new Uri(ConfigurationManager.AppSettings [ loginUrl]));
weakClient.Dispose();
weakClient = null ;

void OnDatareceived( byte [] data)
{
if (data!= null ){
// 将字节数组转换为字符串并将其反序列化为对象
}

}


Multi threading in windows phone C# ?

How to do multithreading when i call a wcf service . One thread should call service and one thread should show processing.

解决方案

Hi. I''m recommend to U to use Reactive Extensions for windows phone.
With help of this third-party library Rx(by the way it was wrriten by one of the LINQ founder ) you can achive such ability to call service on one thread and observe result on another.

For example let get json data from service:

WebClient webClient = new WebClient();
            webClient.Headers.Add("uid", LoginData.LoginName);
            webClient.Headers.Add("pass", LoginData.Password);
            webClient.Headers.Add("logonType", "0");
            var eventStream = Observable.FromEventPattern<DownloadDataCompletedEventArgs>(webClient, "DownloadDataCompleted").
                SubscribeOn(Scheduler.NewThread).Select(newData => newData.EventArgs.Result);

eventStream.ObserveOn(System.Threading.SynchronizationContext.Current).Subscribe(OnDatareceived,
                //on error
                ex =>
                {
                    System.Windows.MessageBox.Show(ex.Message);
                    ViewModel.ViewModelLocator.Logger.Error(string.Empty, ex);
                });

            webClient.DownloadDataAsync(new Uri(ConfigurationManager.AppSettings["loginUrl"]));
            weakClient.Dispose();
            weakClient = null;

void OnDatareceived(byte[] data)
		{
			if(data!=null){
//convert byte array to string and deserialize it to object
}

}


这篇关于Windows Phone C#中的多线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆