关于ui线程异步方法回调函数ui线程阻塞的问题
先贴代码
internal Image back = new Image() { Stretch = Stretch.Fill }; //当前场景中的背景ServiceReference1.WebServiceSoapClient WSC = new SilverlightChart.ServiceReference1.WebServiceSoapClient(); public MainPage() { InitializeComponent(); //this.Width = back.Width = 800; //this.Height = back.Height = 500; this.Background = new SolidColorBrush(Colors.Transparent); Loaded += new RoutedEventHandler(MainPage_Loaded); this.LayoutRoot.Children.Add(container); Add(back, -1); } public void CallManage() { string[] parm = {"U","I"}; GetData(parm); } /// <summary> /// Function to create a chart /// </summary> public void CreateChart(Object obj) { //解决跨线程访问ui对象? this.Dispatcher.BeginInvoke(delegate { JsonObject json = (JsonObject)obj; Chart chart = new Chart(); chart.ZoomingEnabled = true; chart.ToolBarEnabled = true; chart.ScrollingEnabled = false; container.Children.Add(chart); Canvas.SetLeft(chart, 5); Canvas.SetTop(chart, 0); }); } private void GetData(string[] callStr) { SilverlightChart.ServiceReference1.ArrayOfString callArray = new SilverlightChart.ServiceReference1.ArrayOfString(); for (int i = 0; i < callStr.Length; i++) { callArray.Add(callStr[i]); } WSC.ChartCompleted += new EventHandler<SilverlightChart.ServiceReference1.ChartCompletedEventArgs>(WSC_ChartCompleted); WSC.ChartAsync(callArray); } //delegate void AsyncCreatChart(JsonObject obj); //SynchronizationContext syn; private void WSC_ChartCompleted(object sender, SilverlightChart.ServiceReference1.ChartCompletedEventArgs e) { //异步调用完毕 JsonObject jsonObject = (JsonObject)JsonObject.Parse(e.Result);//e.Result为JSON文本 //这里调用后silverlight出错,是否还是阻塞ui线程,如何处理? ThreadPool.QueueUserWorkItem(new WaitCallback(CreateChart), jsonObject); } void MainPage_Loaded(object sender, RoutedEventArgs e) { App myapp = (App)App.Current; //取得控件 HtmlElement he = HtmlPage.Document.GetElementById("serach"); //为控件注册事件 he.AttachEvent("onclick", (EventHandler)serachHanddle); //HtmlPage.Window.Invoke("setFrameHeight", this.Height); } //控件事件 void serachHanddle(object sender, EventArgs args) { //取得html输入控件 //beginDate = HtmlPage.Document.GetElementById("beginDate").GetProperty("Value").ToString(); CallManage(); }