How to create a new app instance with Process

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am using the Process class to start the associated program for a specific
file. The Process.Start method reuses an existing running instance of the
Process, and that's not want I want. I always want a new unique instance of
the application to start.
 
Joe:

There are 3 static overloads of the Start method in the Process class. Try
those.

Thanks,
Mujtaba.
 
Thanks, but all of the overloads do the same thing - they reuse a process if
it is already running, and I alwasy want to create an new instance of the
process. I'm trying to display an XML or HTML file in IE (or whatever program
is associated with XML on the user's system). I don't want an existing
instance of IE to load the XML because I want the user to close the
application before returning to my app. Closing an open instance would be a
major annoyance if they want their browse left open.
 
Joe:

Try this class:

Imports System.Diagnostics
Imports System.IO

Public Class ShellDocument

Private Declare Function FindExecutable Lib "shell32.dll" Alias
"FindExecutableA" _
(ByVal lpFile As String, ByVal lpDirectory As String, ByVal lpResult
As System.Text.StringBuilder) As Integer

Public Shared Function Start(ByVal fileName As String) As Process
Dim sExeName As String = ""
Dim sTempFile As String = Path.GetTempPath() +
Guid.NewGuid.ToString("B") + ".htm"
Dim oFile As New FileStream(sTempFile, IO.FileMode.Create,
IO.FileAccess.Write, IO.FileShare.Write)
Try
Dim oStr As New System.Text.StringBuilder
oStr.Append(Chr(32), 254)
Dim iResult As Integer = FindExecutable(sTempFile, "", oStr)
sExeName = oStr.ToString()
Finally
oFile.Close()
IO.File.Delete(sTempFile)
End Try

Dim info As New System.Diagnostics.ProcessStartInfo(sExeName)
info.Arguments = fileName

Return System.Diagnostics.Process.Start(info)
End Function
End Class


Shariq Khan
(e-mail address removed)
 
This doesn't do it either. I don't want to close the currently running
instance, I want to create my own instance of the app (whatever the
associated program is) and then wait for the user to close it manually.
Thanks though.
 
Yes, sorry, this does do what I want, thank you.

Shariq Khan said:
Joe:

Try this class:

Imports System.Diagnostics
Imports System.IO

Public Class ShellDocument

Private Declare Function FindExecutable Lib "shell32.dll" Alias
"FindExecutableA" _
(ByVal lpFile As String, ByVal lpDirectory As String, ByVal lpResult
As System.Text.StringBuilder) As Integer

Public Shared Function Start(ByVal fileName As String) As Process
Dim sExeName As String = ""
Dim sTempFile As String = Path.GetTempPath() +
Guid.NewGuid.ToString("B") + ".htm"
Dim oFile As New FileStream(sTempFile, IO.FileMode.Create,
IO.FileAccess.Write, IO.FileShare.Write)
Try
Dim oStr As New System.Text.StringBuilder
oStr.Append(Chr(32), 254)
Dim iResult As Integer = FindExecutable(sTempFile, "", oStr)
sExeName = oStr.ToString()
Finally
oFile.Close()
IO.File.Delete(sTempFile)
End Try

Dim info As New System.Diagnostics.ProcessStartInfo(sExeName)
info.Arguments = fileName

Return System.Diagnostics.Process.Start(info)
End Function
End Class


Shariq Khan
(e-mail address removed)
 
If you look closely, the function returns a System.Diagnostics.Process
object. You can call the WaitForExit method on that object to wait till that
process closes. See
http://msdn.microsoft.com/library/d...emdiagnosticsprocessclasswaitforexittopic.asp

You can also setup a handler for the OnExited event on this process object
which will be fired when the process exits.
http://msdn.microsoft.com/library/d...emdiagnosticsprocessclasswaitforexittopic.asp

Hope this helps.
Cheers,

Shariq Khan
(e-mail address removed)
 
Back
Top