wpf c#后台工作程序在执行完成后工作

时间:2017-04-28 13:45:11

标签: c# wpf backgroundworker

我有一个wpf应用程序,我想在任务之前启动一个加载指示器,在任务完成后结束。但是指示在任务执行后开始。 我想要的是如下。

      private void RunAllScriptsChildwdwBtnOK_Click(object sender, RoutedEventArgs e)
    {
        worker.RunWorkerAsync(); // this supposed to start progress bar
        _RunAllScripts_Click();

    }

   private void worker_DoWork(object sender, DoWorkEventArgs e)
    {

        this.Dispatcher.Invoke(() =>
        {
            ... Start loading indicator
        });
    }

    private void worker_RunWorkerCompleted(object sender,
                                           RunWorkerCompletedEventArgs e)
    {
        ... End loading indicator
    }

但是加载指示器仅在之后开始和结束(如在工作人员事件中所假设的) _RunAllScripts_Click();方法执行完成。 (我发现在取消订阅worker_RunWorkerCompleted事件后,进度条会启动并保持不变,因为没有代码可以结束它。)

另外我想补充一点,断点在执行之前命中了worker_DoWork方法,但是如上所述,UI执行后会更新。

感谢您提供的所有帮助。

3 个答案:

答案 0 :(得分:2)

如果我是你,我将使用async + await关键字

    private async void ButtonBase_OnClick(object sender, RoutedEventArgs e)
    {
        // this is where you would enable your indicator
        Button.IsEnabled = false;

        await Task.Run(
            () =>
            {
                // this is where you put your work, which should be executed in the background thread.
                Thread.Sleep(2000);
            });

        // this is where you would disable it
        Button.IsEnabled = true;
    }

答案 1 :(得分:0)

使用0 bits: echo -ne "" | openssl sha1 (stdin)= da39a3ee5e6b4b0d3255bfef95601890afd80709 8 bits: echo -ne "\x5d" | openssl sha1 (stdin)= 4ff447b8ef42ca51fa6fb287bed8d40f49be58f1 16 bits: echo -ne "\x53\xa1" | openssl sha1 (stdin)= c9066463926e470db1ba15cbd06e614dbf0bc9a7 etc... 将有效。 async/await关键字将允许您在不影响/阻止UI线程的情况下运行工作(允许仍然发生消息传递)。工作完成后,await关键字后的任何代码都将执行。

请注意,我还在await调用中包含了await工作,因为您正在执行的其他工作需要UI线程访问。

InvokeAsync

答案 2 :(得分:0)

亲爱的人们帮我解决这个问题,谢谢大家。 这对我有用,希望它适用于所有人。

BackgroundWorker bwTestAll = new BackgroundWorker() { WorkerReportsProgress = true };

 bwTestAll.DoWork += new DoWorkEventHandler(TestAll);
        bwTestAll.RunWorkerCompleted += TestAll_RunWorkerCompleted;

//this is where I initialize my loading ring and other stuff and marshall background
//worker to do the main work
 Dispatcher.Invoke(new Action(() =>
                {
                    EnableLoading = true;
                    RunAllScriptsTest.IsEnabled = false;

                }), DispatcherPriority.ContextIdle);

                bwTestAll.RunWorkerAsync();


//this is my main work
  void TestAll(object sender, DoWorkEventArgs e)
    {
        presenter.RunAllScripts(true);
    }

//this is where I do my post-work stuff
 private void TestAll_RunWorkerCompleted(object sender,
                                   RunWorkerCompletedEventArgs e)
    {
        Dispatcher.Invoke(new Action(() =>
        {
            /
            EnableLoading = false;
            RunAllScriptsTest.IsEnabled = true;
            DbExecGrid = this.ExecutionResults;
            ShowOrHideExecGrid(this.EnableOrDisableGrid);

        }), DispatcherPriority.ContextIdle);
    } 

*请注意,带有“DispatcherPriority.ContextIdle”的Dispatcher适合我。