How to kill a Windows Process through a WPF ListView? - c#

I am a WPF newbee trying to build my own Windows Task Manager.
Right now i have a window with all the active processes running on my machine showing in a ListView.
Now here is my problem: I have a button called End Process. I want to select a process from the ListView and kill it by pressing the End Process button. I just cant figure it out how to do that.
I have this code (C#) assigned to the button:
private void End_Process_Click(object sender, RoutedEventArgs e)
{
Process[] procs=Process.GetProcessesByName(ProcessesListView.SelectedItem.ToString());
foreach (Process p in procs)
{
p.Kill();
}
}

I suspect the items in your list are the actual Process objects. If so, you can do something like this:
private void End_Process_Click(object sender, RoutedEventArgs e)
{
Process process = (Process)ProcessesListView.SelectedItem;
process.Kill();
}

Related

Can anyone help me to get handle of process which is called by func Process.Start();?

For example, I wanna get handle from browser.
private void button1_Click(object sender, EventArgs e)
{
Process.Start("https://google.com/");
//How to get handle of this process?
}
Process.Start() returns a Process object of the newly created process.
In the example below, myProcess.Handle is going to be the handle of said process.
var myProcess = Process.Start("notepad.exe");
Console.WriteLine(myProcess.Handle);

C# Start process then stop

I'm trying to start a process upon a button press and then stop it when another button is pressed in WPF. When I click the "Start" button, the process starts normally, but when I click the "Stop" button, the process keeps running (I don't get any errors). My code is below:
private Process _process;
private void StartProcess(object sender, RoutedEventArgs e) {
string command = "/C .\\Miners\CryptoMiner1\Miner.exe";
_process = Process.Start("cmd.exe", command);
}
private void StopProcess(object sender, RoutedEventArgs e) {
_process.Kill();
}
I found the solution. Instead of creating a cmd.exe process and then running the .exe from there, I directly ran the exe like so:
Before
string process = "/C Process.exe -args"
Process.Start("cmd.exe", process);
After
Process.Start("Process.exe", "-args");

On Screen Keyboard auto pop up

Is there a way to on screen keyboard auto pop up when user clicks TextBox field on WPF?
If it is possible, can it work across the app?
On TextBox focus you can use
XAML
<TextBox Name="TxtBxName" GotFocus="TxtBxName_GotFocus" />
C#
private void TxtBxName_GotFocus(object sender, RoutedEventArgs e)
{
Process[] pname = Process.GetProcessesByName("notepad");
if (pname.Length == 0)
System.Diagnostics.Process.Launch("osk.exe");
}
to invoke the on-screen keyboard application that comes with Windows
Same way on the lost focus you should terminate the process
I have put together all your answers and what works for what me is:
private void OpenOSK()
{
try
{
Process.Start("TabTip.exe");
}
catch
{
}
}
private void _textBox_GotFocus(object sender, RoutedEventArgs e)
{
OpenOSK();
}

Using Process to display an external program inside winforms

I am trying to embed part of an external program (part of its window) inside a existing WinForm that I am developing.
Following examples online, I came out with the following. I assume that the process name is unique , singular and that it is already open, in the example:
private void button1_Click(object sender, EventArgs e)
{
Process[] myProc = Process.GetProcessesByName("the_external_program_i_want");
Process p = myProc[0];
IntPtr appWin = p.MainWindowHandle;
SetParent(appWin, this.Handle);
SetWindowLong(appWin, GWL_STYLE, WS_VISIBLE);
MoveWindow(appWin, panel1.Location.X, panel1.Location.Y, panel1.Width, panel1.Height, true);
}
My issue is that after i close my WinForm, I realised that this external program will trigger a pop-up box saying that "program_name has stopped working".
My question is that is there additional functions that I will neeed to call, to close the external program correctly, within the WinForm?
Thanks!
First of all make the p variable accesible to the whole class.
Then add a event listener to the FormClosing event:
this.FormClosing += Window_FormClosing;
void Window_FormClosing(object sender, FormClosingEventArgs e)
{
p.Kill();
}

Closing Processes represented by CheckedItems in c#

I am wanting to close processes that a user selects in a ListView box in another form. the ListView box uses checkboxes for selecting which processes to close. Unfortunately, i dont know how to convert the checked processes into processes....any help?
public void Form4_Load(object sender, EventArgs e)
{
Process[] prs = Process.GetProcesses();
listView1.Items.Clear();
foreach (Process proces in prs)
{
if (!string.IsNullOrEmpty(proces.MainWindowTitle))
listView1.Items.Add(proces.MainWindowTitle);
}
foreach (Process PRC in listView1.CheckedItems)
{
\\Idk what to put here.
}
In order to kill a process you either need it's name or process id, so change your above code that adds process to ListView1 to some thing like this:
private void Form4_Load(object sender, EventArgs e)
{
Process[] prs = Process.GetProcesses();
listView1.Items.Clear();
foreach (Process proces in prs)
{
if (!string.IsNullOrEmpty(proces.MainWindowTitle))
{
ListViewItem item = new ListViewItem();
item.Tag = proces.Id; //it will be used to kill this process
item.Text = proces.MainWindowTitle;
listView1.Items.Add(item);
}
}
}
Note the tag field, which we will use later to kill the selected process.. Now loop through all selected ListViewItems, get the corresponding process id from tag field and get the reference to the process using System.Diagnostics.Process.GetProcessById. This method returns an object of Process, which exposes a Kill method using which one can stop the process this object refers to.
private void button1_Click(object sender, EventArgs e)
{
foreach (ListViewItem list in listView1.CheckedItems)
{
Process p = System.Diagnostics.Process.GetProcessById(Convert.ToInt32(list.Tag));
if (p != null)
p.Kill();
}
}

Categories