c# 运行进程而不冻结我的应用程序的 GUI

发布于 2024-08-31 18:23:35 字数 136 浏览 4 评论 0原文

我想启动一个进程(调用另一个程序),目前外部程序需要时间(这是正常的)!

但它冻结了我的 GUI 我看到了很多例子,我正在学习,很难弄清楚, 尝试阅读和学习线程,但这并不容易(至少对我来说) 以及好的简单教程或代码示例?

干杯

I want to start a process (calling another program), currently the external program takes time (it is normal)!

but it freezes my GUI
I saw allot of examples and I'm learning, it is hard to figure it out,
trying to read and learn threading, but it is not that easy (at least for me)
and good simple tutorial or code sample?

cheers

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

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

发布评论

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

评论(3

夜夜流光相皎洁 2024-09-07 18:23:35

这是显示如何使用异步方法的链接。
http://www.codeproject.com/KB/cs/AsyncMethodInitation.aspx

您可以使用异步方法来启动该进程,并且启动时不会冻结 gui。

void Your_Method()
{
   //Start process here
}


MethodInvoker myProcessStarter= new MethodInvoker(Your_Method);

myProcessStarter.BeginInvoke(null, null);

MethodInvoker 说明

Here is a link showing how to use an asynchronous method.
http://www.codeproject.com/KB/cs/AsyncMethodInvocation.aspx

You can use the asynchronous method to start the process, and it won't freeze the gui while it starts up.

void Your_Method()
{
   //Start process here
}


MethodInvoker myProcessStarter= new MethodInvoker(Your_Method);

myProcessStarter.BeginInvoke(null, null);

MethodInvoker Description

臻嫒无言 2024-09-07 18:23:35

BackgroundWorker 正是为这种场景而设计的。

请参阅 MSDN

它提供了有用的信号发送方法(双向),不需要您自己将回调编组回 UI 线程。

或者使用任务并行库...

BackgroundWorker was designed for exactly this kind of scenario.

See MSDN.

It provides useful methods for signalling (both ways) that don't require you to marshall calls back to the UI thread yourself.

Or use the Task Parallel Library...

浪推晚风 2024-09-07 18:23:35

使用这个:

在顶部声明:

BackgroundWorker backgroundWorker1 = new System.ComponentModel.BackgroundWorker();

然后在 form_load:

          backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
            backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
            backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);

之后声明:

backgroundWorker1.RunWorkerAsync(someArg); // this calls  backgroundWorker1_DoWork(....


    // This event handler is where the actual,
    // potentially time-consuming work is done.
    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
    }

Use this:

declare at top:

BackgroundWorker backgroundWorker1 = new System.ComponentModel.BackgroundWorker();

then in form_load:

          backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
            backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
            backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);

after which:

backgroundWorker1.RunWorkerAsync(someArg); // this calls  backgroundWorker1_DoWork(....


    // This event handler is where the actual,
    // potentially time-consuming work is done.
    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文