2013-05-28 34 views
0

我有WPF控制託管在winform中有菜單和一些標籤。 WPF控件連接到互聯網以下載一些數據。 我把代碼分成兩步 第一隻是設置控制屬性 第二連接到網 第二次運行在線程內, 但Winform控件不會發生在窗體上,直到WPF控件完成他的兩個步驟。WPF控制託管在WinForm塊WinForm控件從加載甚至與線程

我已經嘗試了很多方法使它可以穿線 但是所有的方法都會到達相同的目的地。

CODE 1-負載WPF控件

private void MDI_Load(object sender, EventArgs e) 
    { 
     MenuManager.FillMenu(MainMenu); // I have filled WinForm Menu first, but it doesn't appear until WPF finish 

     #region = WPF Control = 

     wpfManager.AddweatherControl(); 
     wpfManager.weatherManager.Start(); // This have to run in another thread 

     #endregion 
} 

2- wpfManager.weatherManager.Start

public void Start() 
{ 
    //var tsk = System.Threading.Tasks.Task.Factory.StartNew(GetWeather); 
    //tsk.ContinueWith(t => { MessageBox.Show(t.Exception.InnerException.Message); }, 
    // System.Threading.CancellationToken.None, System.Threading.Tasks.TaskContinuationOptions.OnlyOnFaulted, 
    // System.Threading.Tasks.TaskScheduler.FromCurrentSynchronizationContext()); 

    //System.Threading.Thread t = new System.Threading.Thread(
    // () => weatherControl.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(GetWeather)) 
    // ); 
    //t.SetApartmentState(System.Threading.ApartmentState.STA); 
    //t.Start(); 

    weatherControl.Dispatcher.BeginInvoke(new Action(GetWeather), new object[] { }); 
} 

void GetWeather() 
{ 
    #region = Weather = 
    Yweather.Getweather(UserManager.CurrentUser.Preferences.WeatherCity); 

    if (Yweather.Online && Yweather.IDayForecast.Count > 0) 
    { 
     weatherControl.CurrentDegree.Text = Yweather.IDayForecast[0].CurrentTemperature.ToString(); 
     weatherControl.WeatherTypeName.Text = Yweather.IDayForecast[1].WeatherText; 
     weatherControl.AllDayDegree.Text = Yweather.IDayForecast[1].LowTemperature + " - " + Yweather.IDayForecast[1].HighTemperature; 
     weatherControl.WeatherType.Source = wpfManager.BitmapToImageSource(Yweather.IDayForecast[0].Image); 

     xWeatherDay weatherday1 = weatherControl.OhterDaysPanel.Children[0] as xWeatherDay; 
     weatherday1.AllDayDegree.Text = Yweather.IDayForecast[2].LowTemperature + " - " + Yweather.IDayForecast[2].HighTemperature; 
     weatherday1.WeatherType.Source = wpfManager.BitmapToImageSource(Yweather.IDayForecast[2].Image); 
    } 
    else 
    { 
     weatherControl.CurrentDegree.Text = "0"; 
     weatherControl.WeatherTypeName.Text = "NAN"; 
     weatherControl.AllDayDegree.Text = "0 - 0"; 
     weatherControl.WeatherType.Source = wpfManager.BitmapToImageSource(Yweather.OfflineImage); 
    } 

    #endregion 
} 

回答

1

它看起來像你的代碼張貼的延遲是由於在運行GetWeather UI線程。假設weatherControl是WPF控件的一個實例,它將在UI線程上運行,因爲它是其調度程序所屬的線程。

如果您想要在後臺線程上運行代碼,一個簡單的方法是使用BackgroundWorker。你可以使用它是這樣的:

public void Start() 
{ 
    var worker = new BackgroundWorker(); 
    worker.DoWork += (sender, args) => 
    { 
     GetWeather(); 
     // put the results of getting the weather in to args.Result 
    }; 
    worker.RunWorkerCompleted += (sender, args) => 
    { 
     // use args.Result to update the UI 
    }; 
    worker.RunWorkerAsync(); 
} 

DoWork事件處理程序的代碼在後臺線程上運行,而在RunWorkerCompleted事件處理程序的代碼在UI線程上運行。

+0

我不知道該如何謝謝你,它的功能就像一個魅力。 :) –