0

I have this application to upload files to server via API, is a WinForm application in c#, this application starts a process

Process

This process is an .exe file from another WinForm Application. I tried to get the process by id, by name even get all the process and I can't find the process started.

The problem is I run the process to upload files, the form has one cancel button, this button should pause the process and ask the user if he wants to cancel the operation or not, if he clicks yes I have to call another piece of code but I can't find the process and I can't interrupt or pause.

Any thoughts?

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
Elmer A. Chacon
  • 75
  • 3
  • 11
  • Why do you need the complexity of two separate applications? It seems like you're just making your life so much harder. – ProgrammingLlama May 19 '21 at 06:41
  • Hey @Llama, I know that but is a legacy application from a client, maybe in a future we can merge the apps but at this moment is not an option :( – Elmer A. Chacon May 19 '21 at 06:44
  • I'm not sure if [suspending the process](https://stackoverflow.com/questions/71257/suspend-process-in-c-sharp) would help you. Honestly though, your best bet is probably to implement some form of inter-process communication (IPC) and cooperatively pause/resume it (this would require making changes to the other application too). – ProgrammingLlama May 19 '21 at 06:47
  • _"I can't find the process and I can't interrupt or pause"_ -- it's not clear, really, what you are even asking here. I agree with the previous comment, suspending a process unilaterally is not generally a good idea. But taking your question literally, that seems to be what you want. See duplicate. If you have some different need in mind, please improve the question by providing a proper [mcve], along with a detailed explanation of what the code does, how that's different from what you want, and what _specifically_ you need help with. – Peter Duniho May 19 '21 at 07:32

2 Answers2

1

You can't stop it, you just find the exe name and kill it.

DialogResult dialogResult = MessageBox.Show("Sure", "Some Title", MessageBoxButtons.YesNo);
if(dialogResult == DialogResult.Yes)
{
    try
    {
        foreach (Process proc in Process.GetProcessesByName("_procExe"))
                {
                    proc.Kill();
                }
    }
    catch(Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}
else if (dialogResult == DialogResult.No)
{
    //do something else
}
 
H.S
  • 190
  • 13
1

If you can edit the uploading application, you can try this:

  • Create a registry key in Current User which would have a pause entry (0 to continue, 1 to pause) which the uploading application would frequently check at regular intervals when uploading. It would pause its operations if it finds a 1 in the registry key.
  • If you want to pause the application, set this flag to 1 and show the confirmation box.
  • If the user chooses to cancel upload, then you can use _proc.Kill() otherwise make the registry pause entry back to 0 which the uploading application again picks up and resumes.
Dharman
  • 30,962
  • 25
  • 85
  • 135