C# WPF 调度程序 - 无法正确执行

发布于 2024-10-20 11:34:24 字数 961 浏览 2 评论 0原文

我正在尝试更改 UI,然后让我的函数运行 这是我的代码:

private void btnAddChange_Document(object sender, RoutedEventArgs e)
{
   System.Threading.ThreadStart start = delegate()
   {
      // ...

      // This will throw an exception 
      // (it's on the wrong thread)
      Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(changest));

      //this.BusyIndicator_CompSelect.IsBusy = true;
      
      //this.MainWindow_parent.BusyIndicator_MainWindow.IsBusy = true;
   };
   // Create the thread and kick it started!
   new System.Threading.Thread(start).Start();
}

public void changest()
{
    this.BusyIndicator_CompSelect.IsBusy = true;
    this.MainWindow_parent.BusyIndicator_MainWindow.IsBusy = true;
    t = "Creating document 1/2..";
}

我想在 ui 更新后/ ThreadStart“启动”结束后运行的函数:

string x = "";
for(int i =0;i<=1000;i++)
{
   x+= i.ToString();
}
MessageBox.Show(x);

那么我应该做什么?

I am trying to make a change in the UI and then make my function run
here's my code:

private void btnAddChange_Document(object sender, RoutedEventArgs e)
{
   System.Threading.ThreadStart start = delegate()
   {
      // ...

      // This will throw an exception 
      // (it's on the wrong thread)
      Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(changest));

      //this.BusyIndicator_CompSelect.IsBusy = true;
      
      //this.MainWindow_parent.BusyIndicator_MainWindow.IsBusy = true;
   };
   // Create the thread and kick it started!
   new System.Threading.Thread(start).Start();
}

public void changest()
{
    this.BusyIndicator_CompSelect.IsBusy = true;
    this.MainWindow_parent.BusyIndicator_MainWindow.IsBusy = true;
    t = "Creating document 1/2..";
}

the function I want to run after the ui updates / after the ThreadStart 'start' has ended:

string x = "";
for(int i =0;i<=1000;i++)
{
   x+= i.ToString();
}
MessageBox.Show(x);

So what am I supposed to do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

∞梦里开花 2024-10-27 11:34:24

我假设您想异步执行一些操作。正确的?为此,我建议在 WPF 中使用 BackgroundWorker-班级:

BackgroundWorker bgWorker = new BackgroundWorker() { WorkerReportsProgress=true};  
bgWorker.DoWork += (s, e) => {      
    // Do here your work
    // Use bgWorker.ReportProgress(); to report the current progress  
};  
bgWorker.ProgressChanged+=(s,e)=>{      
    // Here you will be informed about progress and here it is save to change/show progress. 
    // You can access from here savely a ProgressBars or another control.  
};  
bgWorker.RunWorkerCompleted += (s, e) => {      
   // Here you will be informed if the job is done. 
   // Use this event to unlock your gui 
};  
// Lock here your GUI
bgWorker.RunWorkerAsync();  

我希望这就是你的问题。

I assume you want to execute some action asynchronous. Right? For this, I recommend using in WPF the BackgroundWorker-class:

BackgroundWorker bgWorker = new BackgroundWorker() { WorkerReportsProgress=true};  
bgWorker.DoWork += (s, e) => {      
    // Do here your work
    // Use bgWorker.ReportProgress(); to report the current progress  
};  
bgWorker.ProgressChanged+=(s,e)=>{      
    // Here you will be informed about progress and here it is save to change/show progress. 
    // You can access from here savely a ProgressBars or another control.  
};  
bgWorker.RunWorkerCompleted += (s, e) => {      
   // Here you will be informed if the job is done. 
   // Use this event to unlock your gui 
};  
// Lock here your GUI
bgWorker.RunWorkerAsync();  

I hope, this is what your question is about.

小帐篷 2024-10-27 11:34:24

对你想要实现的目标有点困惑,但我相信这就是你想要的......

    private void btnAddChange_Document(object sender, RoutedEventArgs e)
    {
        System.Threading.ThreadStart start = delegate()
        {
            //do intensive work; on background thread
            string x = "";
            for (int i = 0; i <= 1000; i++)
            {
                x += i.ToString();
            }

            //done doing work, send result to the UI thread
            Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, 
                new Action<int>(changest));

        };

        //perform UI work before we start the new thread
        this.BusyIndicator_CompSelect.IsBusy = true;
        this.MainWindow_parent.BusyIndicator_MainWindow.IsBusy = true;
        t = "Creating document 1/2..";

        //create new thread, start it
        new System.Threading.Thread(start).Start();
    }

    public void changest(int x)
    {
        //show the result on the UI thread
        MessageBox.Show(x.ToString());
    }

Slightly confused on what you are trying to accomplish but I believe this is what you are after...

    private void btnAddChange_Document(object sender, RoutedEventArgs e)
    {
        System.Threading.ThreadStart start = delegate()
        {
            //do intensive work; on background thread
            string x = "";
            for (int i = 0; i <= 1000; i++)
            {
                x += i.ToString();
            }

            //done doing work, send result to the UI thread
            Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, 
                new Action<int>(changest));

        };

        //perform UI work before we start the new thread
        this.BusyIndicator_CompSelect.IsBusy = true;
        this.MainWindow_parent.BusyIndicator_MainWindow.IsBusy = true;
        t = "Creating document 1/2..";

        //create new thread, start it
        new System.Threading.Thread(start).Start();
    }

    public void changest(int x)
    {
        //show the result on the UI thread
        MessageBox.Show(x.ToString());
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文