Sunday, March 1, 2020

Start Something Using Process.Start in VB.NET

Start Something Using Process.Start in VB.NET The Start method of the Process object is possibly one of the most underappreciated tools available to a programmer. As a .NET method, Start has a series of overloads, which are different sets of parameters that determine exactly what the method does. The overloads let you specify just about any set of parameters that you might want to pass to another process when it starts. What you can do with Process.Start is really only limited by the processes you can use with it. If you want to display your text-based ReadMe file in Notepad, its as easy as: Process.Start(ReadMe.txt)or Process.Start(notepad, ReadMe.txt) This example assumes the ReadMe file is in the same folder as the program and that Notepad is the default application for .txt file types, and its in the system environment path. Process.Start Similar to Shell Command in VB6 For programmers familiar with Visual Basic 6, Process.Start is somewhat like the VB 6 Shell command. In VB 6, you would use something like: lngPID Shell(MyTextFile.txt, vbNormalFocus) Using Process.Start You can use this code to start Notepad maximized and create a ProcessStartInfo object that you can use for more precise control: Dim ProcessProperties As New ProcessStartInfoProcessProperties.FileName notepadProcessProperties.Arguments myTextFile.txtProcessProperties.WindowStyle ProcessWindowStyle.MaximizedDim myProcess As Process   Process.Start(ProcessProperties) Starting a Hidden Process You can even start a hidden process. ProcessProperties.WindowStyle ProcessWindowStyle.HiddenBut be careful. Unless you add more code to end the process, youll probably have to end it in Task Manager. Hidden processes are normally only used with processes that dont have any kind of a user interface. Retrieving the Name of a Process Working with Process.Start as a .NET object gives you a lot of capability. For example, you can retrieve the name of the process that was started. This code will display notepad in the output window: 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  WaitForExit  was executed. Ordinarily, those components wont be active. Code it up and see for yourself. One 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 doesnt 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, its 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 Using To 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 Sub But simply selecting the event for the component is a lot easier.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.