C#、WPF、在没有后台工作人员的情况下更新 gui [英] C#, WPF, Updating the gui without backgroundworkers

查看:14
本文介绍了C#、WPF、在没有后台工作人员的情况下更新 gui的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要 10-20 秒才能启动的程序.我需要在程序启动时显示一个带有进度条的窗口.我知道 BackgroundWorker 是执行此操作的正确方法,但不幸的是,我现在没有时间使用线程重新设计整个 gui.这是我正在尝试的一些代码,但它不起作用.谁能帮忙..?

使用系统;使用 System.Threading;使用 System.Windows;命名空间 WpfApplication1{公共部分类 Window1 : 窗口{公共窗口 1(){初始化组件();线程 t = 新线程(ShowLoadingWindow);t.SetApartmentState(ApartmentState.STA);t.Priority = ThreadPriority.Highest;t.开始();DoSomeLongTask();保持循环=假;}bool keepLo​​oping = true;私有无效 ShowLoadingWindow(){LoadingWindow lw = new LoadingWindow();lw.Show();而(保持循环){线程.睡眠(1);}lw.Close();}私有无效 DoSomeLongTask(){for (int i = 0; i <20000; i++){GC.Collect();}}}}

加载窗口只是一个带有进度条的裸窗口.这怎么行不通?

解决方案

您仍然在主线程中执行长期任务.您需要在主线程中显示加载窗口,并在后台完成长期任务.

使用 BackgroundWorker 确实是最简单的解决方案:

<前>BackgroundWorker bw;加载窗口 lw;公共窗口1(){bw = new BackgroundWorker();lw = new LoadingWindow();bw.DoWork += (sender, args) => {//在这里做你冗长的事情——这将在一个单独的线程中发生...}bw.RunWorkerCompleted += (sender, args) => {if (args.Error != null)//如果在 DoWork 期间发生异常,MessageBox.Show(args.Error.ToString());//在这里做你的错误处理//在这里关闭你的加载窗口...}//在此处打开并显示您的加载窗口...bw.RunWorkerAsync();//启动后台工作者}

I have a program that takes 10-20 seconds to start. I need to show a window with a progress bar when the program starts up. I know BackgroundWorker's are the correct way to do this but I unfortunately don't have the time to rework the whole gui using threads right now. Here is some code I'm trying but it is not working. Can anyone help..?

using System;
using System.Threading;
using System.Windows;

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();

            Thread t = new Thread(ShowLoadingWindow);
            t.SetApartmentState(ApartmentState.STA);
            t.Priority = ThreadPriority.Highest;
            t.Start();

            DoSomeLongTask();
            keepLooping = false;
        }

        bool keepLooping = true;
        private void ShowLoadingWindow()
        {
            LoadingWindow lw = new LoadingWindow();
            lw.Show();

            while (keepLooping)
            {
                Thread.Sleep(1);
            }

            lw.Close();
        }

        private void DoSomeLongTask()
        {
            for (int i = 0; i < 20000; i++)
            {
                GC.Collect();
            }
        }
    }
}

The loading window is just a bare window with a progress bar. Howcome this doesn't work?

解决方案

You're still doing the long task in the main thread. You need to show the loading window in the main thread and do the long task in the background.

Using a BackgroundWorker is really the simplest solution for this:

BackgroundWorker bw;
LoadingWindow lw;

public Window1() {
    bw = new BackgroundWorker();
    lw = new LoadingWindow();

    bw.DoWork += (sender, args) => {
        // do your lengthy stuff here -- this will happen in a separate thread
        ...
    }

    bw.RunWorkerCompleted += (sender, args) => {
        if (args.Error != null)  // if an exception occurred during DoWork,
            MessageBox.Show(args.Error.ToString());  // do your error handling here

        // close your loading window here
        ...
    }

    // open and show your loading window here
    ...

    bw.RunWorkerAsync(); // starts the background worker
}

这篇关于C#、WPF、在没有后台工作人员的情况下更新 gui的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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