Closing more than one Autohotkeys applications - c#

I have 4 applications and I have to use 4 different autohotkey exe applications for always on Top future. However, I would like to close all of them or kill their process.
I tried using ESC::ExitApp, Process.Kill() function as well, but only one of them is closing.
Here is my code:
#SingleInstance Force
WinSet, AlwaysOnTop, Toggle, Bot
Winset, Style, -0xC00000, Bot
^!p::Pause
*ESC::ExitApp
return
Other applications are the same but different Windows Title name.
Process[] bottom = Process.GetProcessesByName("bottom");
//MessageBox.Show(workers.Length.ToString());
foreach (Process bot in bottom)
{
bot.Kill();
bot.WaitForExit();
bot.Dispose();
}
Process[] left = Process.GetProcessesByName("left");
//MessageBox.Show(workers.Length.ToString());
foreach (Process l in left)
{
l.Kill();
l.WaitForExit();
l.Dispose();
}
Process[] right = Process.GetProcessesByName("test1");
//MessageBox.Show(workers.Length.ToString());
foreach (Process r in right)
{
r.Kill();
r.WaitForExit();
r.Dispose();
}
Process[] top = Process.GetProcessesByName("top");
//MessageBox.Show(workers.Length.ToString());
foreach (Process t in top)
{
t.Kill();
t.WaitForExit();
t.Dispose();
}
SendKeys.Send("{ESC}");
According to above code, I tried to kill 4 apps but only one is closing. Plus I cannot see in TaskManager running autohotkeys.exe files.Is there any way
to turn off or close all autohotkeys.exe files? Thank you in advance.

You can use the WMI from windows to do that.
ESC::
Query := "Select ProcessId, CommandLine from Win32_Process where name = 'Autohotkey.exe'"
for process in ComObjGet("winmgmts:").ExecQuery(Query, "WQL")
process, close, % process.ProcessId
Return

Related

How to kill specific process when three or more same name process is running [duplicate]

This question already has answers here:
How do I determine the owner of a process in C#?
(10 answers)
Closed 13 days ago.
I have a C# program and I want only kill one specific process of it for example I have these three instance running at the same time in this way:
first application1 use the name.exe
2nd application2 use the name.exe
3rd application3 use the name.exe
when I am close the application1 I used the code for kill the process
Process[] pr = Process.GetProcessesByName("MAPPOINT");
foreach (Process prs in pr)
{
if (prs.ProcessName.ToUpper() == "NAME")
{
prs.Kill();
}
this code kill all name.exe process where I want to kill specific one.
how to kill only one process which is user by application1 not close the application2 and application3 process?
First to Start
If 'application1.exe' is always the first to start, then you could try killing the lowest PID. I believe PIDs are always assigned sequentially, so the lowest number would be the first that was opened.
So something like:
Process[] pr = Process.GetProcessesByName("MAPPOINT");
// check edgecase - no application open with this name...
if (pr.Length < 0) { return; }
//edgecase - give a default value
int firstPID= pr[0].Id;
//iterate through to find lowest number PID
foreach(Process prs in pr)
{
if (prs.Id < firstPID)
{
firstPID= prs.Id;
}
}
// close the first (aka lowest) PID
Process process = Process.GetProcessById(firstPID);
process.Kill();
If you need to be careful and only close if there are 3 or more application instances running, then put it in a function and check the length:
private static void CloseFirstInstance_IfMoreThanThree()
{
Process[] pr = Process.GetProcessesByName("MAPPOINT");
// check edgecase - no application open with this name...
if (pr.Length < 0) { return; }
//edgecase - not enough applicaitons open
if (pr.Length < 3) { return; }
//edgecase - give a default value
int firstPID = pr[0].Id;
...
}
Alternates
If the instance you want to kill is not the first to start, you may need to get fancy and:
Ask the user to select the window, then grab the pointer to the window, then grab the PID. (Getting more difficult - UI windows have been a pain for me.)
Actually might be simpler: you might try PID = Process.GetProcessesByName(MyProcessName).FirstOrDefault().Id; and kill that process. This could be all you need.

Closing a program running under another users profile using C#

I made a program that opens COM6. The program starts when the user logs on.
If another user logs on, while the first user is still logged in, the program crazes because the COM is already open.
I found this code, which I thought could solve the problem. The code was meant to close all other application with the same name, but apparently, it does not work, when the other app is running under another user. Have anybody got any solution for this ?
void CloseAllButMe()
{
Process[] processes;
Process self = Process.GetCurrentProcess();
processes = Process.GetProcessesByName(self.ProcessName);
foreach (Process p in processes)
{
if (self.Id != p.Id) p.CloseMainWindow();
}
}
You can use the methods Process.Kill to stop a process. Calling Kill will immediately stop the process and could cause a loss of work.
Here is a code sample for killing Calculator:
public static void KillPaint()
{
System.Diagnostics.Process[] procs = null;
try
{
procs = Process.GetProcessesByName("calc");
Process mspaintProc = procs[0];
if (!mspaintProc.HasExited)
{
mspaintProc.Kill();
}
}
finally
{
if (procs != null)
{
foreach (Process p in procs)
{
p.Dispose();
}
}
}
}

C# process.mainwindowtitle showing process name instead

I am writing a C# application and before I exit, I need to close another console application based on its window title... this second application (staticEngineWriter) is a C program that uses SetConsoleTitle() function to set its title.
The code I have in C# to do this is below. The problem is it is returning the process name instead of the window title of the C application. Seems simple enough but I wonder if it has to do with how the writer application is setting its title using the SetConsoleTitle function.
static void stopWriter()
{
Process[] proc = Process.GetProcessesByName("staticEngineWriter");
foreach (System.Diagnostics.Process CurrentProcess in proc)
{
Console.Writeline(CurrentProcess.MainWindowTitle); //debug
if (CurrentProcess.MainWindowTitle.Contains("My Writer"))
{
CurrentProcess.Kill();
}
}
}

Exit all instances of my app

I have the following function:
private void TakeOverAllScreens()
{
int i = 0;
foreach (Screen s in Screen.AllScreens)
{
if (s != Screen.PrimaryScreen)
{
i++;
Process.Start(Application.ExecutablePath, "Screen|" + s.Bounds.X + "|" + s.Bounds.Y + "|" + i);
}
}
}
As you can see it creates a separate instance of my application for each screen on the pc.
How can I create an exit function that closes all of them?
The function needs to work on any instance of the app, not just my "Main" instance.
You can terminate all processes with the same name:
var current = Process.GetCurrentProcess();
Process.GetProcessesByName(current.ProcessName)
.Where(t => t.Id != current.Id)
.ToList()
.ForEach(t => t.Kill());
current.Kill();
Just you have to keep track of opened processes:
List<Process> opened = new List<Process>();
private void TakeOverAllScreens()
{
int i = 0;
foreach (Screen s in Screen.AllScreens)
{
if (s != Screen.PrimaryScreen)
{
i++;
opened.Add(Process.Start(Application.ExecutablePath, "Screen|" + s.Bounds.X + "|" + s.Bounds.Y + "|" + i));
}
}
}
Then:
private void terminateAll()
{
foreach (var p in opened) p.Kill();
}
How can you close an application?
There are three general options:
Kill it outright
Assuming that it has a main window and that it behaves conventionally, send a WM_CLOSE message to that window by P/Invoking SendMessage
Assuming that it wants to cooperate, use a custom communication channel to tell it to terminate itself
How should you close an application?
Killing a process outright is heavy-handed and should never be attempted unless you know that the process won't be doing anything sensitive when it's terminated. Typically this is used as a last resort.
Telling it to close its main window requires that you first learn what that window's HWND is somehow; you can do that by walking the list of top-level windows or by having the spawned process somehow communicate it to you. It also assumes that the other process will decide to terminate itself when you ask that its main window be closed (in reality it could decide to do anything, including completely ignore the message). This approach is a reasonable first attempt and it usually works fine with processes which are not under your control -- although you have to find the window yourself.
Using a custom communication channel allows you to completely control what happens and how plus it can be implemented in lots of ways, but it's an approach that involves writing the most code and needs you to have source access to both applications.
Conclusion
It depends. If you are 1000% sure that killing the process won't disrupt anything then that would be a quick and dirty solution; otherwise you need to use one of the two "polite" approaches.
Put all opened process in list, so you can kill the process one by one. Look code bellow:
private void TakeOverAllScreens()
{
int i = 0;
List<Process> allProcesses = new List<Process>();
foreach (Screen s in Screen.AllScreens)
{
if (s != Screen.PrimaryScreen)
{
i++;
allProcesses.Add(Process.Start(Application.ExecutablePath, "Screen|" + s.Bounds.X + "|" + s.Bounds.Y + "|" + i));
}
}
foreach (Process proc in allProcesses)
{
proc.Kill();
}
}
I always execute command
tskill notepad // to kill all instances of notepad
From within App, it can be done like
Process.Start("cmd /k tskill " + Process.GetCurrentProcess().ProcessName)
Try this function:
void ExitAll(string processName)
{
foreach(Process p in Process.GetProcesses())
{
if (p.ProcessName.ToLower().Equals(processName.ToLower()))
p.Kill();
}
}

Kill some processes by .exe file name

How can I kill some active processes by searching for their .exe filenames in C# .NET or C++?
Quick Answer:
foreach (var process in Process.GetProcessesByName("whatever"))
{
process.Kill();
}
(leave off .exe from process name)
My solution is to use Process.GetProcess() for listing all the processes.
By filtering them to contain the processes I want, I can then run Process.Kill() method to stop them:
var chromeDriverProcesses = Process.GetProcesses().
Where(pr => pr.ProcessName == "chromedriver"); // without '.exe'
foreach (var process in chromeDriverProcesses)
{
process.Kill();
}
Update:
In case if you want to do the same in an asynchronous way (using the C# 8 Async Enumerables), check this out:
const string processName = "chromedriver"; // without '.exe'
await Process.GetProcesses()
.Where(pr => pr.ProcessName == processName)
.ToAsyncEnumerable()
.ForEachAsync(p => p.Kill());
Note: using async methods doesn't always mean code will run faster.
The main benefit is that the foreground thread will be released while operating.
You can use Process.GetProcesses() to get the currently running processes, then Process.Kill() to kill a process.
If you have the process ID (PID) you can kill this process as follow:
Process processToKill = Process.GetProcessById(pid);
processToKill.Kill();
You can Kill a specific instance of MS Word.
foreach (var process in Process.GetProcessesByName("WINWORD"))
{
// Temp is a document which you need to kill.
if (process.MainWindowTitle.Contains("Temp"))
process.Kill();
}
Depending on how many processes there are to kill (e.g. when its hundreds like in my case), foreaching over all of them might take quite a while. (interesting sidenote: while Kill() was usually quite quick in .NET FW 4.8 , somehow in NET 6.0 Windows its a lot slower - seeing multiple Win32Exceptions in the debug/trace until the target process is finally done)
Anyway back to topic:
In case of an app shutdown, where u need to make sure every process is is gone, consider using the TAP library - particulary the Parallel shortcuts, hundreds of processes killed within a glimpse.
Usage example:
var procs = Process.GetProcessByName("mydirtyprocesses");
if (procs.Length == 0) return;
procs.AsParallel().ForAll(process =>
{
try
{
process.Kill();
// No process linked to the process comp (mostly because the process died in
// the short timespan between invoking GetProcess() and the effective
// initialization of the props/fields of the component. -OR- Process has
// already exited (when the exit happened after the process component has
// beenpopulated (difference is, in case 1 you cannot even get the Process
// ID from // the component, in case 2 you see data like Id and get the true
// for HasExited // - so always be prepared for that.
// catch (InvalidOperationException)
{
// Process is gone, no further action required
return;
}
// Ensuring process is gone (otherwise try again or fail or whatever)
if (!process.HasExited)
{
// Handle it
}
}
In this particular scenario just wrap it properly in try/catch , as with such a number of processes the probability for an exception is quite increased
static void Main()
{
string processName = Process.GetCurrentProcess().ProcessName;
int processId = Process.GetCurrentProcess().Id;
Process[] oProcesses = Process.GetProcessesByName(processName);
if (oProcesses.Length > 1)
{
if ((MessageBox.Show("Application is opened!", "",MessageBoxButtons.YesNo) == DialogResult.Yes)) ;
{
foreach (var process in Process.GetProcessesByName(processName))
{
if (process.Id != processId)
{
process.Kill();
}
}
}
}
else
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new frmLogin());
}
}
public void EndTask(string taskname)
{
string processName = taskname.Replace(".exe", "");
foreach (Process process in Process.GetProcessesByName(processName))
{
process.Kill();
}
}
//EndTask("notepad");
Summary: no matter if the name contains .exe, the process will end. You don't need to "leave off .exe from process name", It works 100%.

Categories