How to open another program from my first in C#? - c#

I want to open a second program from my first program and still be able to work on my first program. And another thing is to close the second program from my first. Is there a way to do this?

For running the program:
You need using System.Diagnostics;
Process open_exe(string path)
{
Process to_open;
to_open.StartInfo.FileName = path;
to_open.Start();
return (to_open);
}
For closing the program:
void close_exe(Process p, bool force = false)
{
if(force)
{
p.Kill();
}
else
{
p.CloseMainWindow();
}
}
When calling open_exe, it returns a Process, which you can use in the close_exe function as the argument.
Addition: on the close_exe function, you can either call it by:
close_exe(process);
This will use the default value for force as false, and will not force it to close
close_exe(process, true);
This will not use the default value for force, and use true, and will thus force it to close

You can do it just for one line (with "using System.Diagnostics")
Process.Start(pathToAnotherProgramm);

As to launching another exe-file see the following:
codeproject, stackoverflow
As to closing the application I would say that the way to close the app depends on your needs.
Two ways come to my mind immediately.
The first is to ungracefully kill the exe as a process. You can kill
any exe either by id or it's exe-name.
The second one is to set up a
communication between two processes with the help of WCF for
interprocess communication.
You can easily google about killing processes in C# as well as about setting up a communication between two processes.

Related

Making application run only through launcher [duplicate]

I need to make the primary .exe unrunnable from it (When you try to start it directly ,you get a message : Cannot start directly,if it runs from the secondary exe (only it,must have a crc verification i think) then start .
Hope i make myself clear
First .exe can't start directly
Second .exe can start the first exe (only)
Make the first exe a DLL. Then the second program can use it but a user won't be able to run it directly.
Set up the EXE that can't be started directly to accept a parameter, such as a SHA-256 hash of some unique data from the one that's supposed to start it. If that parameter doesn't exist or is not what's expected, display an error and exit.
EDIT:
static class Program
{
static void Main(params string[] args) //<- first needed change
{
if(args.Length == 0 || args[0] != "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855")
{
Console.WriteLine("Cannot execute this program directly.")
return;
}
... //rest of main function
}
}
The simplest way to do that is to have command line parameters, or beter still, set an environment variable and run it, so theres little way to trace the requirements from a "can I get round the fact you want me to use your app to run it". However, I would say a DLL would be the way to go really.
I am not sure if the process name (Process.GetCurrentProcess().ProcessName) would give you the 1st or the second EXE name but you can make the 1st EXE as a DLL and the second as an EXE.

one process waiting for another process's output via the file system

I have a process A that reads in some data produced by some other process B. The data is 'exchanged' via the file system. To ensure that the file exists, process A currently checks for the file's existence like this:
while (!File.Exists(FileLocation))
{
Thread.Sleep(100);
}
This only seems to work 99 percent of the time. The other 1 percent of the time, process A establishes that the file exists but process B has not written everything yet (i.e. some data is missing).
Is there another simpler way to make the above situation more bullet proofed? Thanks.
Is there another simpler way to make the above situation more bullet proofed?
You could use a Mutex for reliable inter-process synchronization. Another possibility is to use a FileSystemWatcher.
After determining that the file exists, you can try opening the file for exclusive access, which will fail if another process still has the file open:
try
{
File.Open("foo",FileMode.Open,FileAccess.Read,FileShare.None);
}
catch(IOException ex)
{
// go back to
}
Given that you say that you can change both processes' code, you can use an EventWaitHandle to communicate between the processes.
In your program that creates the file, in the Main() method you can create an EventWaitHandle and keep it around until the end of the program. You'll need to pass the EventWaitHandle object around in your program so that it is available to the bit of code that creates the file (or provide some method that the file-creating code can call to set the event).
using (EventWaitHandle readySignaller = new EventWaitHandle(false, EventResetMode.ManualReset, "MySignalName"))
{
// Rest of program goes here...
// When your program creates the file, do this:
readySignaller.Set();
}
Then have some code like this in the program that's waiting for the file:
// Returns true if the wait was successful.
// Once this has returned true, it will return false until the file is created again.
public static bool WaitForFileToBeCreated(int timeoutMilliseconds) // Pass Timeout.Infinite to wait infinitely.
{
using (EventWaitHandle readySignaller = new EventWaitHandle(false, EventResetMode.ManualReset, "MySignalName"))
{
bool result = readySignaller.WaitOne(timeoutMilliseconds);
if (result)
{
readySignaller.Reset();
}
return result;
}
}
NOTE: If we successfully wait note that I am resetting the signal and it will remain reset until the other process sets it again. You can handle the logic differently if you need to; this is just an example.
Essentially what we are (logically) doing here is sharing a bool between two processes. You have to be careful about the order in which you set and reset that shared bool.
Try the FileSystemWatcher.
Listens to the file system change notifications and raises events when
a directory, or file in a directory, changes.

C# : Making an exe to not run directly

I need to make the primary .exe unrunnable from it (When you try to start it directly ,you get a message : Cannot start directly,if it runs from the secondary exe (only it,must have a crc verification i think) then start .
Hope i make myself clear
First .exe can't start directly
Second .exe can start the first exe (only)
Make the first exe a DLL. Then the second program can use it but a user won't be able to run it directly.
Set up the EXE that can't be started directly to accept a parameter, such as a SHA-256 hash of some unique data from the one that's supposed to start it. If that parameter doesn't exist or is not what's expected, display an error and exit.
EDIT:
static class Program
{
static void Main(params string[] args) //<- first needed change
{
if(args.Length == 0 || args[0] != "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855")
{
Console.WriteLine("Cannot execute this program directly.")
return;
}
... //rest of main function
}
}
The simplest way to do that is to have command line parameters, or beter still, set an environment variable and run it, so theres little way to trace the requirements from a "can I get round the fact you want me to use your app to run it". However, I would say a DLL would be the way to go really.
I am not sure if the process name (Process.GetCurrentProcess().ProcessName) would give you the 1st or the second EXE name but you can make the 1st EXE as a DLL and the second as an EXE.

How to make script/program to make it so an application is always running?

I have a simple .exe that needs to be running continuously.
Unfortunately, sometimes it crashes unexpectedly, and there's nothing that can be done for this.
I'm thinking of like a C# program that scans the running application tree on a timer and if the process stops running it re-launches it... ? Not sure how to do that though....
Any other ideas?
It's fairly easy to do that, but the "crashes unexpectedly, and there's nothing that can be done for this" sounds highly suspect to me. Perhaps you mean the program in question is from a third party, and you need to work around problems they can't/won't fix?
In any case, there's quite a bit of sample code to do exactly what you're talking about.
The first solution would be to fix your EXE, so it does not crash. If you can not fix it now, you probably need to add exception handling, so you can catch the exception, and not close the EXE.
Second solution is to write simple guard programm that will start your simple .exe and will monitor specific process handle. It will restart your program when it closes.
easiest way is to have you program see if an instance of itself is running and exit if it is. Set up a scheduled task to run it every couple of minutes.
class Program
{
static void Main(string[] args)
{
if (IsRunning())
{
return;
}
else
{
for (int x = 0; x < 10; x++)
{
//Do Stuff
System.Threading.Thread.Sleep(1000);
}
}
}
private static bool IsRunning()
{
Process[] P = Process.GetProcessesByName( Process.GetCurrentProcess().ProcessName ) ;
return P.Count() > 1;
}
}
One trick occasionally employed by malware in days past was to have two processes that each monitor the currently running processes and restart the other process if it is terminated.
The System.Diagnostics namespace has classes which can help, particularly "Process".
For example
static Process[] Process.GetProcesses()
returns a list of all the currently running processes.
If your other process is not in this list, you just restart it with, for example
Process.Start()
Your program needs to initially start your target process itself (with Process.Start), then simply wait for it to terminate (with WaitForExit on object that is returned by Process.Start()). After that whole procedure is repeated.
This way you'd be sure that you are watching the process you are interested in, and you don't need to poll process list at all.
Process.Start() and WaitForExit() usage example.

How do I find out if a process is already running using c#?

I have C# winforms application that needs to start an external exe from time to time, but I do not wish to start another process if one is already running, but rather switch to it.
So how in C# would I so this in the example below?
using System.Diagnostics;
...
Process foo = new Process();
foo.StartInfo.FileName = #"C:\bar\foo.exe";
foo.StartInfo.Arguments = "Username Password";
bool isRunning = //TODO: Check to see if process foo.exe is already running
if (isRunning)
{
//TODO: Switch to foo.exe process
}
else
{
foo.Start();
}
This should do it for ya.
Check Processes
//Namespaces we need to use
using System.Diagnostics;
public bool IsProcessOpen(string name)
{
//here we're going to get a list of all running processes on
//the computer
foreach (Process clsProcess in Process.GetProcesses()) {
//now we're going to see if any of the running processes
//match the currently running processes. Be sure to not
//add the .exe to the name you provide, i.e: NOTEPAD,
//not NOTEPAD.EXE or false is always returned even if
//notepad is running.
//Remember, if you have the process running more than once,
//say IE open 4 times the loop thr way it is now will close all 4,
//if you want it to just close the first one it finds
//then add a return; after the Kill
if (clsProcess.ProcessName.Contains(name))
{
//if the process is found to be running then we
//return a true
return true;
}
}
//otherwise we return a false
return false;
}
You can use LINQ as well,
var processExists = Process.GetProcesses().Any(p => p.ProcessName.Contains("<your process name>"));
I have used the AppActivate function in VB runtime to activate an existing process.
You will have to import Microsoft.VisualBasic dll into the C# project.
using System;
using System.Diagnostics;
using Microsoft.VisualBasic;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
Process[] proc = Process.GetProcessesByName("notepad");
Interaction.AppActivate(proc[0].MainWindowTitle);
}
}
}
You can simply enumerate processes using Process.GetProcesses method.
I found out that Mutex is not working like in the Console application. So using WMI to query processes that can be seen using Task Manager window will solved your problem.
Use something like this:
static bool isStillRunning() {
string processName = Process.GetCurrentProcess().MainModule.ModuleName;
ManagementObjectSearcher mos = new ManagementObjectSearcher();
mos.Query.QueryString = #"SELECT * FROM Win32_Process WHERE Name = '" + processName + #"'";
if (mos.Get().Count > 1)
{
return true;
}
else
return false;
}
NOTE: Add assembly reference "System.Management" to enable the type intellisense.
I think the complete answer to your problem requires understanding of what happens when your application determines that an instance of foo.exe is already running i.e what does '//TODO: Switch to foo.exe process' actually mean?
In a past project I needed to prevent multiple execution of a process, so I added a some code in the init section of that process which creates a named mutex. This mutext was created and acquired before continuing the rest of the process. If the process can create the mutex and acquire it, then it is the first one running. If another process already controls the mutex, then the one which fails is not the first so it exits immediately.
I was just trying to prevent a second instance from running, due to dependencies on specific hardware interfaces. Depending on what you need with that "switch to" line, you might need a more specific solution such as a process id or handle.
Also, I had source code access to the process I was trying to start. If you can not modify the code, adding the mutex is obviously not an option.
Two concerns to keep in mind:
Your example involved placing a
password on a command line. That
cleartext representation of a secret
could be a security vulnerability.
When enumerating processes, ask
yourself which processes you really
want to enumerate. All users, or
just the current user? What if the
current user is logged in twice (two
desktops)?
Mnebuerquo wrote:
Also, I had source code access to the
process I was trying to start. If you
can not modify the code, adding the
mutex is obviously not an option.
I don't have source code access to the process I want to run.
I have ended up using the proccess MainWindowHandle to switch to the process once I have found it is alread running:
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool SetForegroundWindow(IntPtr hWnd);

Categories

Resources