如何在调用线程时阻止主应用程序表单 [英] How Do I Block Main Application Form When Called A Thread

查看:67
本文介绍了如何在调用线程时阻止主应用程序表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中有一个名为form1的表单。我从我的表单调用diskcleanup和系统管理。但问题是那些是同时调用的。我想这样做-----



我想要第一次diskcleanup将打开,我将选择驱动器并做工作。那时我的主窗体将被阻止,它不会调用第二个功能进行系统管理。

完成diskcleanup后我的应用程序将调用第二个进程。



diskcleanup = Process.Start(cleanmgr.exe);

系统管理= Process.Start(msdt.exe ,-id MaintenanceDiagnostic -ep ControlPanelSearch);

解决方案

你不想阻塞主线程:这消除了移动长时间运行操作的优点到一个单独的线程 - 用户界面不再响应。



相反,请考虑使用BackgroundWorker [ ^ ]运行清理(并报告进度)因此,您可以让用户知道它仍在继续)并处理 RunWorkerCompleted [ ^ ]事件。

在事件处理程序中,启动第二个BackgroundWorker来进行系统管理。



[edit]

BackgroundWorkers使用 Process.WaitForExit [ ^ ]方法停止,直到任务完成。

[/ edit]

In my application there is one form named form1. I am calling diskcleanup and system management from my form.But problem is those are called simultaneously.I want to do this-----

I want first diskcleanup will be opened and I'll select drive and do work.In that time my main form will be blocked that is it will not call 2nd function for system management.
after finishing diskcleanup my application will call 2nd process.

diskcleanup=Process.Start("cleanmgr.exe");
system management=Process.Start("msdt.exe"," -id MaintenanceDiagnostic -ep ControlPanelSearch");

解决方案

You don't want to "block" the main thread: that removes the advantages of moving long running operations to a separate thread - the UI no longer responds.

Instead, consider using a BackgroundWorker[^] to run your cleanup (and it reports progress so you can give the user an idea that it's still going) and handle the RunWorkerCompleted[^] event.
In the event handler, start a second BackgroundWorker to do your system management.

[edit]
The BackgroundWorkers use the Process.WaitForExit[^] method to halt until the task is complete.
[/edit]


这篇关于如何在调用线程时阻止主应用程序表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆