wpf检测点击事件完成而不延迟主进程

发布于 2024-11-09 05:04:54 字数 559 浏览 0 评论 0原文

我需要检测点击事件中的过程是否已完成,而不延迟主 wpf 进程。

我不想要的

public void click_event(object sender,routedeventargs)
{
        <launch a bunch of threads>
        while(<threads.are alive>);


        <code for what i want to do after threads are done>
}

public void threadtask()
{}

我刚刚做了的

 public void click_event()
   {
         foreach(<thread>)
           <give thread task and start() each>
   }
   }

但这不会检测到何时线程已完成..这里需要帮助。谢谢。

I need to detect a procedure from a click event has finished without delaying the main wpf process..

What I don't want

public void click_event(object sender,routedeventargs)
{
        <launch a bunch of threads>
        while(<threads.are alive>);


        <code for what i want to do after threads are done>
}

public void threadtask()
{}

what i just did

 public void click_event()
   {
         foreach(<thread>)
           <give thread task and start() each>
   }
   }

but this will not detect when the threads are done.. need help here. Thanks.

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

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

发布评论

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

评论(2

熟人话多 2024-11-16 05:04:54

你要求的是两件不同的事情。您希望主线程不被阻塞,但您希望在其他线程完成时执行某些操作(您必须等待)。考虑从一个新线程启动线程,然后让另一个线程完成工作。像这样的事情:

public void click_event() 
{
    <start new thread>
         <foreach(thread in threads)>
            <do work>
         <join threads>
         <do your work here>
}

所以所有的工作都在不同的线程上,甚至是你之后想做的工作。鉴于此,您是否需要多个工作线程?

You are asking for two different things. You want the main thread to not be blocked, but you want to do something when the other threads are done (which you have to wait for). Consider starting the threads from a new thread, then let that other thread do the work. Something like this:

public void click_event() 
{
    <start new thread>
         <foreach(thread in threads)>
            <do work>
         <join threads>
         <do your work here>
}

So all of the work is on a different thread, even the work you want to do afterward. Given that, do you need more than one worker thread anyway?

挽手叙旧 2024-11-16 05:04:54

查看这篇文章

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        this.ClickMe.Click += new RoutedEventHandler(ClickMe_Click);
    }

    void ClickMe_Click(object sender, RoutedEventArgs e)
    {
        BackgroundWorker bw = new BackgroundWorker();
        bw.DoWork += (workSender, workE) =>
        {
            string argument = (string)workE.Argument;
            // argument == "Some data"
            System.Threading.Thread.Sleep(2000);
        };

        bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
        bw.RunWorkerAsync("Some data");
    }

    void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        this.ResultsTextBlock.Text = "I'm done";
    }

}

Check out this article

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        this.ClickMe.Click += new RoutedEventHandler(ClickMe_Click);
    }

    void ClickMe_Click(object sender, RoutedEventArgs e)
    {
        BackgroundWorker bw = new BackgroundWorker();
        bw.DoWork += (workSender, workE) =>
        {
            string argument = (string)workE.Argument;
            // argument == "Some data"
            System.Threading.Thread.Sleep(2000);
        };

        bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
        bw.RunWorkerAsync("Some data");
    }

    void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        this.ResultsTextBlock.Text = "I'm done";
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文