I'm making a program which opens a configured application after with the passed paramters through an url with shell execute. I achieved this with the following:
ProcessStartInfo procinfo = new ProcessStartInfo(URI);
procinfo.UseShellExecute = true;
Process App = Process.Start(procinfo);
I want to kill this process later after some minutes through this project that I could do by App.Kill() but the problem is that the Process.Start() always returns null if I pass the URI. How could I reach that process?
If the address of the executable file to start is a URL, the process
is not started and null is returned.
http://msdn.microsoft.com/en-us/library/53ezey2s.aspx
Even if you specify a browser, the process could be a simple handler that sends a message to an existing process (or opens another process) and closes itself immediately.
But you may explicitly launch the browser you want if you know how its process works.
Process p = Process.Start(browserExePath, url);
Related
Was tough to make a title that made sense.
So I am working with SalesLogix CRM and trying to navigate to a contact in said CRM. Saleslogix allows you to use almost like a url that looks like slx:CONTACT//C6UJ9A006S96
That line will direct the CRM to the contact with that ID.
My problem is when I try to navigate to that url I open a new SalesLogix instance instead of using the current one.
Saleslogix is a desktop based software just for your information.
The strange part for me is that if I use Windows' 'Run...' from the start menu and put in slx:CONTACT//C6UJ9A006S96 then it will use the already open application, same with a normal command prompt, however if I use an Admin command prompt it will open a new instance of SalesLogix. I checked the task manager and it doesn't matter if I open it using the admin command prompt or just the desktop window, it will say it is running under my current user and not system or anything weird so it seems the user isn't the problem.
My code is:
Process[] processes = Process.GetProcesses();
foreach (Process p in processes)
{
if (p.ProcessName == "SalesLogix")
{
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.FileName = #"C:\Program Files (x86)\SalesLogix\SalesLogix.exe";
p.StartInfo.Arguments = "slx:CONTACT//C6UJ9A006S96";
p.Start();
}
}
Any help would be appreciated and if I didn't explain my problem clearly enough I would be happy to clear up any confusion.
Try running the URL directly. As in, don't execute the SalesLogix exe, just pass the URI as the filename and let the Windows protocol handler take care of it.
What i think you are doing here is taking the process that is already running, rewriting its parameters and starting it all new again. Did you try simply calling the new process with those parameters to see if it would attach to the existing system process ?
I have an old web form application that spawns a process when an ImageButton is pressed:
System.Diagnostics.Process someProcess = new System.Diagnostics.Process();
someProcess.StartInfo.WorkingDirectory = "path goes here";
someProcess.StartInfo.UseShellExecute = true;
someProcess.StartInfo.FileName = "SomeProcess.exe";
someProcess.StartInfo.Arguments = "arguments goes there";
someProcess.Start();
The process has a variable duration but it is somewhat long running.
Is it possible that IIS shuts down the application before the process is completed?
The kill command will be given, it depends on the process and its settings on whether or not the kill command will actually be executed prior to work being finished.
In other words; this is something you should and can easily test yourself.
I have a WCF service in a self hosted application using TCP bindings. If I start an external process "commandLineApp" from the application, that continues even after my application has closed, I run into problems next time the WCF service is started by my application.
WCF says that the address/port is already in use. If I close the external application (that isn't using WCF or any sockets at all) before restarting my application, the WCF service starts just fine.
It looks like that the socket handles from my application is somehow inherited by the new process "commandLineApp", and not released until that process has exited.
How do I prevent the other process from inheriting the handles (or becoming a child process?) from my main application? Currently I'm using Process.Start to launch the other process, using UseShellExecute set to False because I need to set both EnvironmentVarables and RedirectStandardOutput/Error.
I think the child process setup is prevented if I set UseShellExecute = true, but then I don't get all features I need.
Are there any way around this problem? See example code below.
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "commandLineApp.exe";
psi.Arguments = "/someParameter";
psi.EnvironmentVariables.Add("...", "...");
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
psi.UseShellExecute = false;
Process process = new Process();
process.StartInfo = psi;
process.Start();
// Monitor if process with PID = process.Id is running
// ...
Edit - Additional information:
Doing "netstat -noa" indicates that the port is used with state LISTEN by the previous PID of the main application, but there is no process with that PID anymore. As soon as I close "commandLineApp", the port isn't listed by the netstat command anymore.
WCF services are closed like this before the main application exit:
try
{
serviceHost.Close(TimeSpan.FromSeconds(4));
}
catch (Exception)
{
serviceHost.Abort();
}
I was wrong in the above comment: SO_REUSEADDR would only apply if the handle had been closed, but it seems like socket handles truly are inherited by child processes and there is no easy way to prevent this. This seems like a very stupid design decision, particularly since some places noted that the handle can't be used in the child if any LSPs are installed.
If you had more control over the call to WSASocket, you might be able to pass the WSA_FLAG_NO_HANDLE_INHERIT flag, but this is going to be hard to accomplish inside WCF. Here's a couple other options:
Option #1: Call CreateProcess yourself and pass FALSE for bInheritHandles.
Option #2: Create a helper process before you set up WCF (or any other sockets). Communicate with it via named pipes. Start the child process from this helper instead of from the main process.
I am working on windows application. i have to run some window exe from my app, i am able to do the same but when i close my application these exe remains on running condition, i am not getting how can i close those exe. Please suggest some tips.
To run the Process
private void StartChildProcess(string fileName)
{
Process newProcess = new Process();
newProcess.StartInfo = new ProcessStartInfo(fileName); ;
newProcess.Start();
localProcess.Push(newProcess);
}
To close the process
private void CloseStartedProcesses()
{
while (localProcess.Count > 0)
{
Process process = localProcess.Pop();
if (process != null && !process.HasExited)
{
process.CloseMainWindow();
process.Close();
}
}
}
Some options:
Setup some communication system so the Main application can alert the other application to shutdown (read up on some WCF information or remoting)
Create a do.shutdown file and let the second application check if that file exists, simple but efficient.
Use the process.Kill options
Use Sendkey or equivalent to send a 'quit' key combination
Use Windows API - P/Invoke. FindWindow() or EnumWindows() to get the window handle. Then you can send WM_CLOSE or WM_QUIT to end the application via the SendMessage() function.
Note that if the application checks for user input on exiting (like a MessageBox asking weather the user really wants to quit) the only option might be to send WM_DESTROY which would be equivalent to Process.Kill (at least in respects to causing data loss - I am not certain it is the absolute equivalent).
Try this:
Process[] p = Process.GetProcessesByName("osk");
foreach (var item in p)
{
item.Kill();
}
The reason that the EXE you've ran from your application doesn't terminate once you close your application is probably because the 2nd application runs as a DIFFERENT, SEPARATE process.
If you run another process with System.Diagnostics.Process, it will remain in background until terminated manually or until it finishes it's job.
try this Process proc = Process.GetProcessesByName("processname");
proc.Kill();
I wanted to build a front end web page so that I can start and stop various programs on my server remotely. I know that Shell will open something locally, but how can I make an ASP.Net page which activates programs server-side? Googling got me the "Shell" method, but I don't think that works server-side. Any ideas?
Take a look at the System.Diagnostics.Process class. It can allow you to start and stop an executable on the server.
You would have to impersonate an account that has sufficient privileged to run the application, though. You can use the UserName and Password properties of the System.Diagnostics.ProcessStartInfo object that you pass to Process.Start.
Edit
For an example, you could do the following:
var startInfo = new ProcessStartInfo("C:\MyServerApp.exe", "/A /B /C");
startInfo.UserName = "LocalUser";
// It's a bad idea to hard code the password in the app, this is just an example
startInfo.Password = "SomePass";
var process = Process.Start(startInfo);
// Not a good idea to wait in a web app, but you can until the process completes
process.WaitForExit();