Task.Factory.StartNew 和从外部组件打开表单的问题

发布于 2024-10-09 06:31:31 字数 1105 浏览 1 评论 0原文

我有一个主 Windows 应用程序项目和另一个类库应用程序。在类库应用程序中,我有一个表单(以编程方式创建),在长时间运行工作后显示在主 Windows 应用程序项目中。

我想异步显示该表单(在类库中)。

我为主应用程序项目编写了这段代码:

Task t = Task.Factory.StartNew(delegate
            {

                if (this.InvokeRequired)
                {
                    this.Invoke((Action)(() => textBox1.Text += "Enter Task"));
                    this.Invoke((Action)(() =>
                        {
                            ExternalAssembly.OpenForm of = new ExternalAssembly.OpenForm();
                            of.ShowWindow();
                        })); 
                }

                this.Invoke((Action)(() => textBox1.Text = textBox1.Text + Environment.NewLine + "Exit Task"));
            }, CancellationToken.None, TaskCreationOptions.None); 

并在类库中:

public class OpenForm
    {
        public void ShowWindow()
        {
            Thread.Sleep(10000);
            Form1 frm = new Form1();
            frm.ShowDialog();
        }
    }

但是当类库中的表单显示时,我的主表单冻结了。如何更改代码以便在另一个程序集中异步显示表单?

多谢。

I have a main Windows Application Project and another Class library application. In the class library app, I have a form (created programatically) that shows in main windows application project after long running work.

I want to show that form (in class library) asynchronously.

I wrote this code for Main Application Project:

Task t = Task.Factory.StartNew(delegate
            {

                if (this.InvokeRequired)
                {
                    this.Invoke((Action)(() => textBox1.Text += "Enter Task"));
                    this.Invoke((Action)(() =>
                        {
                            ExternalAssembly.OpenForm of = new ExternalAssembly.OpenForm();
                            of.ShowWindow();
                        })); 
                }

                this.Invoke((Action)(() => textBox1.Text = textBox1.Text + Environment.NewLine + "Exit Task"));
            }, CancellationToken.None, TaskCreationOptions.None); 

and in class library:

public class OpenForm
    {
        public void ShowWindow()
        {
            Thread.Sleep(10000);
            Form1 frm = new Form1();
            frm.ShowDialog();
        }
    }

But when the form in the class library shows, my main form freezes. How I can change the code so that showing a form in another assembly is asynchronous?

Thanks a lot.

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

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

发布评论

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

评论(2

旧城空念 2024-10-16 06:31:31

您已使用 Task.Factory.StartNew 启动了后台任务。然后,您在该后台任务中使用了 Invoke。这会在前台线程上运行该操作,并阻塞后台线程直到完成。

由于您调用的代码 (ShowWindow) 需要很长时间,因此您已成功阻止前台和后台线程。

You've started a background task by using Task.Factory.StartNew. You've then, in that background task, used Invoke. This runs the action on the foreground thread, blocking the background thread until it's completed.

Since the code you're invoking (ShowWindow) takes a long time, you've successfully blocked both foreground and background threads.

笨死的猪 2024-10-16 06:31:31

你可以尝试这样的事情

this.textBox1.Text += "Enter Task";
Task t = Task.Factory.StartNew(
            () =>
                {
                    var of = new ExternalAssembly.OpenForm();
                    of.ShowWindow();
                }).ContinueWith(o => Invoke(new Action(() => this.textBox1.Text += Environment.NewLine + "Exit Task"))
            );

You could try something like this

this.textBox1.Text += "Enter Task";
Task t = Task.Factory.StartNew(
            () =>
                {
                    var of = new ExternalAssembly.OpenForm();
                    of.ShowWindow();
                }).ContinueWith(o => Invoke(new Action(() => this.textBox1.Text += Environment.NewLine + "Exit Task"))
            );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文