I'm rather new to C# so I'm not sure if this is even possible...
What I'm trying to do is use an application form to launch other programs via Process, such as Notepad, and then close my original application while Notepad is still running.
Process notePad = new Process();
notePad.StartInfo.FileName = "notepad.exe";
notePad.Start();
Application.Exit();
From what I've found, I have to close/kill the Notepad process in order to close the launching application. I'd like Notepad to remain open, however.
Is there any way to achieve this via C#?
Try this?
using(Process notePad = Process.Start("notepad.exe","")) { }
Application.Exit();
if you want notepad still open after close the form use this code
private void button1_Click(object sender, EventArgs e)
{
Thread notepad_opener = new Thread(opener);
notepad_opener.Start();
}
public void opener()
{
Process.Start("notepad.exe");
Application.Exit();
}
i try this and notepad still open :D
You need to set the UseShellExecute property to True; see this page
You can save the Id of Process you have started:
int id = notePad.Id;
Then Kill it when you want:
Process.GetProcessById(id).Kill();
Related
I am trying to keep track of a webpage; whether it was closed, specifically. I use this code to open processes and keep track of close:
this.Process = new Process();
this.Process.EnableRaisingEvents = true;
this.Process.Exited += new EventHandler(Process_Exited);
this.Process.StartInfo.FileName = this.ProcessPath;
this.Process.StartInfo.Arguments = this.Arguments;
this.Process.Start();
private void Process_Exited(object sender, EventArgs e)
{
Console.WriteLine("Exiting...");
}
This works fine for other types of processes, like batch files (it'll print "Exiting..." when I close them). However, if I use it to open a webpage and then close that webpage, it'll never print "Exiting". I assume this is because the browser can have multiple tabs and it's waiting for the whole browser to close. How can I keep track of the specific tab being exited?
I am Working in Visual Studio 2008 Winforms Application project in Windows 7 (32 bit).I am doing the project in C#.
I have placed some buttons in a tab and added actions for that once it is clicked. While clicking the button am just running a .exe file in its action part.
My problem is that, i opened a window by clicking one button(so the .exe file is running), now while am clicking the button again it is opening same window again irrespective of checking that it is open or not. I want to solve this issue,as when a window is opened it must not open again on another click on same button. How to solve this issue. ?
Please help....
Thanks in advance..
You could check if the process is already running, when re-clicking the button:
private void btnStartExecutable_Click(object sender, EventArgs e)
{
Process[] processName = Process.GetProcessesByName("InsertProcessNameHere");
if (pname.Length == 0)
{
MessageBox.Show("Application isn't running yet.");
//Start application here
Process.Start("InsertProcessNameHere");
}
else
{
MessageBox.Show("Application is already running.");
//Don't start application, since it has been started already
}
}
You can try this:
bool processExited = true;
private void button1_Click(object sender, EventArgs e)
{
if (processExited)
{
Process process = new Process();
process.EnableRaisingEvents = true;
process.Exited += MyProcessExited;
process.StartInfo = new ProcessStartInfo();
process.StartInfo.FileName = "notepad.exe";
process.Start();
processExited = false;
}
else
{
MessageBox.Show("Still running");
}
}
void MyProcessExited(object sender, EventArgs e)
{
processExited = true;
}
The right answer here IMHO is that unless the two application shares a common resource or can talk to each other through some channel, there is no safe and efficient way to achieve what you want. Since the process is external, it could already be running before your calling app starts, or even while it's already running. You won't be able to tell if the process has been started from your app or not.
By the time I'm writing this your question does not yet state if you are in liberty to modify the external app you are calling. If you are however, using a Mutex would be a quick and easy way to solve your problem.
In your external app, whenever you want to make the other app aware of whatever condition you want (be it that the process is running or that a specific window is opened), have a Mutex instance created like this:
var mutex = new Threading.Mutex(true, "mutex unique identifier");
And in your calling app, try to create a Mutex instance with the same identifier:
bool alreadyExists;
var mutex = new Threading.Mutex(false, "mutex unique identifier", out alreadyExists);
Here the alreadyExists variable will tell you whether or not the external process is running or not. This is much safer than trying to identify it via its name, as other processes could have the same or a new version could be of a different name. Of course, the mutex identifier must be as unique as possible (like a Guid), otherwise you may encounter the same problem. ;)
Whenever you feel like the mutex must be released (at external app level), release it:
mutex.ReleaseMutex();
Note that if the process ends the mutex will be automatically released by the OS.
If the external app isn't a .NET based app, you can still create a mutex with Win32 API functions.
Thanks for the support.. I got the answer like this..
1) Creating an event'Exit' for the process in function button click
2) Define a function for the exit event where you set a flag
3) Check the flag is set or not everytime while opening the process in the function button click
Event for Exit: 'P' is the name of process:
p.Exited += new EventHandler(p_Exited);
p_Exited will be the function name where we will set the flag.
Thanks all...
If you know the name of the process that gets started or the path the .exe is run from you can use the Process class to check to see if it is currently running.
http://msdn.microsoft.com/en-us/library/system.diagnostics.process(v=vs.110).aspx
I am creating an C# desktop application.
I want to open only one document at a time.
how can i know that already opened word document is closed by the user before i opens next.
The user has to close opened word document.
Any idea or suggestion would be appreciated.
Can you explain how you application is interacting with Word documents? If your application is the only way for a user to open a word document, then once a document is open set a variable:
bDocOpen = true;
Once the document has been closed set it to false. Don't allow opening documents if bDocOpen equals true.
EDIT
You could launch the document and wait for the process to finish. This is not threaded, so your app will be unresponsive until the document is closed. I guess this behavior could be a mechanism to keep multiple documents from being opened. However leaving your app unresponsive is not a good practice, the user will think something is wrong.
bool bDocOpen = false;
private void btnOpenDoc(object sender, EventArgs e)
{
if (!bDocOpen)
{
ProcessStartInfo pInfo = new ProcessStartInfo();
pInfo.FileName = #"C:\temp\word.doc";
Process p = Process.Start(pInfo);
p.WaitForInputIdle();
p.WaitForExit();
//Will not get here till process exits
MessageBox.Show("Document closed");
bDocOpen = false;
}
}
Every time I click the No button (here button2_click), the program opens itself again, and I cannot figure out why.
private void button2_Click(object sender, EventArgs e)
{
Process game = new Process();
game.StartInfo.FileName = "ShovelShovel.exe";
game.Start();
}
I figured it would launch the game and close, but if I click the button, it just launches my program again. If I click No on that form, then I get a huge list of unhandled exceptions.
Whether I do this.Close(); or Application.Exit(); the program still opens itself.
Can anyone tell me why it would open again? I haven't altered Program.cs in anyway.
Thank you very much.
Try this: (assuming that the program you are working from is "ShovelShovel Settings.exe" and is the launcher for "ShovelShovel.exe")
//launch game
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "ShovelShovel.exe";
Process p = Process.Start(startInfo);
//close
this.Close();
Or even just
Process.Start("ShovelShovel.exe");
If this is still starting the same application, you either have the wrong path, or the exe's are the same.
To double check, why dont you go into the directory and execute "ShovelShovel.exe" manually from WindowsExplorer.
In order to start another instance of my program I did something like:
private void button1_Click(object sender, EventArgs e)
{
Process p = new Process();
p.StartInfo.FileName = Application.ExecutablePath;
p.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
p.Start();
}
And found that stopping the debugger didn't stop the new window, only the first (-launching) window.
How do I programmatically get the new process to be "under" VS?
That Process that you get back has a handle to the running process. You could keep a hold of that in a member variable, rather than a local variable, and on form closing, kill the process.
http://msdn.microsoft.com/en-us/library/e8zac0ca.aspx
You can Change the Start Action for Application Debugging
Right click on your project
Properties
Debug
Start external program
And set the program you want to launch.
If you want to attach to an another instance programmatically, a duplicate question can be found here:
How do I attach a process to the debugger in Visual Studio?
Wich refer this article:
Attaching to a Process using VS.NET Automation Model
Since you're starting your own program a second time, you know it is a GUI. You can keep the Process reference around and call CloseMainWindow (or Kill) on each of them in your FormClosing event handler:
private List<Process> children = new List<Process>();
private void button1_Click(object sender, EventArgs e)
{
Process p = new Process();
p.StartInfo.FileName = Application.ExecutablePath;
p.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
p.Start();
children.Add(p);
}
private Form1_FormClosing(object sender, FormClosingEventArgs e)
{
foreach (Process p in this.children)
{
// posts WM_CLOSE to the main handle of the process
// which allows a graceful exit, as if the user clicked [X]
p.CloseMainWindow();
// p.Kill(); // less graceful, just kill
}
}
Debug -> Attach to Process and select your process from the list.
If by "under VS" you mean having Visual Studio be able to debug the external process you may want to consider the "Attach to Process" strategy.
http://msdn.microsoft.com/en-us/library/c6wf8e4z.aspx