WPF如何停止运行过程 [英] WPF how stop running process

查看:345
本文介绍了WPF如何停止运行过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

读取数据库需要更多时间。那个时候我想停止运行进程,如何

To read data base take more time. That time I want to stop running process, how

推荐答案

它与WPF无关。



启动外部流程来完成与当前流程相关的一些工作并不是很好,特别是如果外部流程不是为了进行这种协作而设计的,但停止(查杀)外部流程要差得多。根据它的不同,它可能会损坏一些数据并造成其他伤害。



尽管如此,这是你可以做到的。首先,注意所有静态 System.Diagnostic.Process.Start 方法返回类的实例 System.Diagnostic.Process 表示已启动的流程:

https://msdn.microsoft.com/en-us/library/system.diagnostics.process.start%28v=vs.110%29.aspx [ ^ ]。



您可以使用此实例来终止进程: https://msdn.microsoft.com/en-us/library/system.diagnostics.process.kill(v = vs.110)的.aspx [ ^ ]。



正如我所说,这是r最好避免的危险物品。可能的合法设计包括:


  • 您在当前流程中实现外部流程的所有功能;它可以是一个单独的组件,但是相同的过程。您需要在单独的线程中执行它。此线程可以使用 Dispatcher.Invoke Dispatcher.BeginInvoke 通知您的UI。
  • 您仍然可以有一个单独的进程,但是这个进程应该完美地设计用于使用一个或另一个IPC工具(例如套接字,命名管道,WCF)进行协作。粗略地说,你向这个进程发送一条消息,告诉它你必须尽快终止,并且在所有的postalalal动作完成属性之后,外部进程应该优雅地终止(关闭文件句柄,提交/滚动)回交易等)。此外部进程可能需要一个单独的线程来侦听消息并处理它们(至少)。您的WPF进程还需要一个单独的线程来等待外部进程(及其消息,也是最低限度),处于等待状态(在等待期间不浪费任何CPU时间)大部分时间。它还需要使用 Dispatcher 来通知UI线程。
It has nothing to do with WPF.

Starting external process to do some work related to your current process is not very good, especially if that external process is not designed to do such collaboration, but stopping (killing) en external process is much worse. Depending on what it is, it could damage some data and do other harm.

Nevertheless, this is how you can do it. First, pay attention that all the static System.Diagnostic.Process.Start methods return the instance of the class System.Diagnostic.Process representing the started process:
https://msdn.microsoft.com/en-us/library/system.diagnostics.process.start%28v=vs.110%29.aspx[^].

You can use this instance to kill the process: https://msdn.microsoft.com/en-us/library/system.diagnostics.process.kill(v=vs.110).aspx[^].

As I say, this is really dangerous stuff which is best avoided. Possible legitimate designs include:

  • You implement all the functionality of the external process in your current process; it could be a separate assembly, but the same process. You would need to execute it in a separate thread. This thread can notify your UI using Dispatcher.Invoke or Dispatcher.BeginInvoke.
  • You still could have a separate process, but this process should be perfectly designed for collaboration using one or another IPC facility (such as sockets, named pipes, WCF). Roughly speaking, you send a message to this process telling it "you have to terminate as soon as possible", and the external process should terminate "gracefully", after all post-mortal actions are property done (closed file handles, committed/rolled back transactions, and the like). This external process may need a separate thread to listen to messages and handle them (as a minimum). Your WPF process also would need a separate thread to wait for the external processes (and its messages, also minimally) effectively, being in the wait state (not wasting any CPU time during wait) most of the time. It also would need to use the Dispatcher to notify the UI thread.


这篇关于WPF如何停止运行过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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