Process对象的Start方法可能是程序员最不受重视的工具之一。作为一个.NET方法,Start有一系列重载,这些重载是不同的参数集,它们精确地确定了该方法的功能。重载允许您指定在另一个进程启动时可能要传递给另一个进程的任何参数集。
您可以使用Process.Start做什么实际上只受您可以使用的流程的限制。如果您想在记事本中显示基于文本的自述文件,只需执行以下操作:
Process.Start("ReadMe.txt") Process.Start("notepad", "ReadMe.txt")本例假设自述文件与程序位于同一文件夹中,记事本是.txt文件类型的默认应用程序,并且它位于系统环境路径中。
对于熟悉VisualBasic6的程序员,Process.Start有点像VB6shell命令。在VB 6中,您将使用以下内容:
lngPID = Shell("MyTextFile.txt", vbNormalFocus)您可以使用此代码启动记事本最大化,并创建ProcessStartInfo对象,以便进行更精确的控制:
Dim ProcessProperties As New ProcessStartInfoProcessProperties.FileName = "notepad"ProcessProperties.Arguments = "myTextFile.txt"ProcessProperties.WindowStyle = ProcessWindowStyle.MaximizedDim myProcess As Process = Process.Start(ProcessProperties)你甚至可以开始一个隐藏的过程。
ProcessProperties.WindowStyle = ProcessWindowStyle.Hidden将Process.Start作为.NET对象使用可以提供很多功能。例如,您可以检索已启动的进程的名称。此代码将在输出窗口中显示“记事本”:
Dim myProcess As Process = Process.Start("MyTextFile.txt") Console.WriteLine(myProcess.ProcessName)This was something you could not do with the VB6 Shell command because it launched the new application asynchronously. Using WaitForExit can cause the reverse problem in .NET because you have to launch a process in a new thread if you need it to execute asynchronously. For example, if you need the components to remain active in a form where a process was launched and WaitForExitOne way to force the process to halt is to use the Kill method.
myProcess.Kill()This code waits for ten seconds and then ends the process.
However, a forced delay is sometimes necessary to allow the process to complete exiting to avoid an error.
myProcess.WaitForExit(10000)' if the process doesn't complete within' 10 seconds, kill itIf Not myProcess.HasExited ThenmyProcess.Kill()End IfThreading.Thread.Sleep(1)Console.WriteLine("Notepad ended: " _& myProcess.ExitTime & _Environment.NewLine & _"Exit Code: " & _myProcess.ExitCode)In most cases, it's probably a good idea to put your processing in a Using block to ensure that the resources used by the process are released.
Using myProcess As Process = New Process' Your code goes hereEnd UsingTo make all this even easier to work with, there is even a Process component that you can add to your project so you can do a lot of the things shown above at design time instead of run time.
One of the things that this makes a lot easier is coding events raised by the process, such as the event when the process has exited. You can also add a handler using code like this:
' allow the process to raise eventsmyProcess.EnableRaisingEvents = True' add an Exited event handlerAddHandler myProcess.Exited, _AddressOf Me.ProcessExitedPrivate Sub ProcessExited(ByVal sender As Object, _ByVal e As System.EventArgs)' Your code goes hereEnd SubBut simply selecting the event for the component is a lot easier.
Visual Basic与Visual C++的主要区别在于Visual Basic是面向对象的编程语言,Visual C++是集成开发环境(IDE)。 visualbasic是微软开发的一种用户友好的编程语言。在visualbasic.NET发布之前,它的最终版本是visualbasic6.0。另一方面,Visual C++是...
...代码返回值的过程,而过程是程序中的可执行语句块。 visualbasic(VB.NET)是在微软开发的.NET框架上实现的一种编程语言。它是一种现代的通用编程语言。它有助于开发高效的程序,而且更容易学习语言。此外,VB还提供了各种功...
visualstudio中build和rebuild的主要区别在于,build有助于完成更改的代码文件,而rebuild删除所有以前编译过的文件并从头开始编译解决方案,而忽略以前所做的任何操作。 visualstudio是由微软开发的集成开发环境(IDE)。它提供了开...
...用机制。两者之间的区别取决于编程语言的上下文。 在Visual Basic中,过程声明为- [AccessSpecifier]子过程重命名([ParameterList]) [声明] 末端接头 在Visual Basic中,函数声明为- [AccessSpecifier]函数名([ParameterList])_ 作为数据类型 [声...
关键区别:VB代表visualbasic。它是微软的一种高级编程语言,用于快速开发基于Windows的程序。VB.NET代表支持Visual Basic网络的技术。它是Microsoft的.NET framework语言之一。这种语言是专门为VB开发人员创建的。它属于在.NET框架上实现...
...学生开发人员的轻量级、易于使用且易于学习的工具”,Visual Studio Express版本包括:VisualWebDeveloper2005Express版SQLServer2005Express版Visual Basic 2005快速版Visual C#2005快速版Visual C++ 2005快速版Visual J#2005速成版看起来微软打算招聘更多的程...
...。Unix和Linux管理员也大量使用Perl来自动化维护工作。 process是指当前正在计算机上执行或“运行”的程序。 多态性是在面向对象编程解释中看到的一个词。这就是拥有两个不同类型的不同对象的能力,这两个对象都实现了相...