在WinForm中托管的WPF控件阻止WinForm控件加载,即使是线程

本文关键字:控件 WinForm 加载 即使是 线程 WPF | 更新日期: 2023-09-27 18:15:42

我有WPF控件托管在winform有菜单和一些标签。WPF控件连接到互联网下载一些数据。我把代码分成两步首先设置控件属性第二次连接到网络第二个线程运行在线程内部,但是,在WPF控件完成这两个步骤之前,Winform控件不会在表单上运行。

我尝试了许多方法使它可线程化但殊途同归。

代码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
}

在WinForm中托管的WPF控件阻止WinForm控件加载,即使是线程

从你发布的代码来看,延迟是由于在UI线程上运行GetWeather。假设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线程上运行。