Cant kill the process with Process.Kill();

  • (2 Pages)
  • +
  • 1
  • 2

26 Replies - 3168 Views - Last Post: 15 June 2016 - 08:27 PM Rate Topic: -----

#1 DOT1   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 153
  • Joined: 02-November 14

Cant kill the process with Process.Kill();

Posted 15 June 2016 - 11:14 AM

So I am going to make this as simple as possible.

I need to understand why its throwing the exception and not just killing the open applications.

I got a application that I am developing.



What does the application do?
It opens UP TO 5 softwares that I SELECT BY BROWSING MY COMPUTER BY USING THE OPENFILEDIALOG


Whats the issue?
When it opens a software that I select and runs it it doesnt kill it after 5 seconds.. It throws me this error.


An unhandled exception of type 'System.InvalidOperationException' occurred in System.dll

Additional information: Cannot process request because the process has exited.



System.InvalidOperationException was unhandled
HResult=-2146233079
Message=Cannot process request because the process has exited.
Source=System
StackTrace:
at System.Diagnostics.Process.GetProcessHandle(Int32 access, Boolean throwIfExited)
at System.Diagnostics.Process.Kill()
at Software_Restarter.Form1.startlbl_Click(Object sender, EventArgs e)
at System.Windows.Forms.Control.onclick(EventArgs e)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.Label.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativewindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativewindow.WndProc(Message& m)
at System.Windows.Forms.Nativewindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at Software_Restarter.Program.Main()
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException: 





Here is my source code



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using System.Threading;

namespace Software_Restarter
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        

        public const int WM_NCLBUTTONDOWN = 0xA1;
        public const int HT_CAPTION = 0x2;

        [DllImportAttribute("user32.dll")]
        public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
        [DllImportAttribute("user32.dll")]
        public static extern bool ReleaseCapture();

        private void label1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                ReleaseCapture();
                SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
            }
        }

        private void BrowseFirst_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            if(ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string FilePath = ofd.FileName;
                textBoxFirst.Text = FilePath;
            }
        }

        private void BrowseSecond_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd1 = new OpenFileDialog();
            if (ofd1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string FilePath = ofd1.FileName;
                textBoxSecond.Text = FilePath;
            }
        }

        private void BrowseThird_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd2 = new OpenFileDialog();
            if (ofd2.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string FilePath = ofd2.FileName;
                textBoxThird.Text = FilePath;
            }
        }

        private void startlbl_Click(object sender, EventArgs e)
        {
            var firstProcess = Process.Start(@"C:\Program Files (x86)\ManyCam\ManyCam.exe");
            int timeOne = 5000;
            Thread.Sleep(timeOne);
            firstProcess.Kill(); //This is where it throws me the exception
        }

         }
    }
}

This post has been edited by DOT1: 15 June 2016 - 11:15 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Cant kill the process with Process.Kill();

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15275
  • View blog
  • Posts: 61,238
  • Joined: 12-June 08

Re: Cant kill the process with Process.Kill();

Posted 15 June 2016 - 11:22 AM

Why is the app being launched, put on hold, then attempted to be closed?
Was This Post Helpful? 0
  • +
  • -

#3 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6818
  • View blog
  • Posts: 28,231
  • Joined: 12-December 12

Re: Cant kill the process with Process.Kill();

Posted 15 June 2016 - 11:24 AM

Quote

What does the application do?
It opens UP TO 5 softwares that I SELECT BY BROWSING MY COMPUTER BY USING THE OPENFILEDIALOG

It isn't starting these processes, you are just obtaining their file names.

Quote

Whats the issue?
When it opens a software that I select and runs it it doesnt kill it after 5 seconds.. It throws me this error.

The only process you are killing is ManyCam, are you doing anything with this application, or anything else during the 5 seconds?



Your browse methods are identical, you should be using arrays or another collection type.
Was This Post Helpful? 0
  • +
  • -

#4 DOT1   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 153
  • Joined: 02-November 14

Re: Cant kill the process with Process.Kill();

Posted 15 June 2016 - 11:24 AM

View Postmodi123_1, on 15 June 2016 - 11:22 AM, said:

Why is the app being launched, put on hold, then attempted to be closed?


Thats the point of the app.. it opens a few softwares.. Waits until they have been open for the ammount of time that the user input and then closes them
Was This Post Helpful? 0
  • +
  • -

#5 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15275
  • View blog
  • Posts: 61,238
  • Joined: 12-June 08

Re: Cant kill the process with Process.Kill();

Posted 15 June 2016 - 11:26 AM

Is this some sort of bot building or what?
Was This Post Helpful? 0
  • +
  • -

#6 DOT1   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 153
  • Joined: 02-November 14

Re: Cant kill the process with Process.Kill();

Posted 15 June 2016 - 11:28 AM

I know I should use an array its just that I didnt get to that point yet, and no I am not doing anything with manycam during that process

I guess you could see this as a bot.. I wouldnt know because im not to good with bots, all it does is that it Opens a few softwares for a while and then it closes them after the ammount of time that the user requested as shown in this picture.

Posted Image
Was This Post Helpful? 0
  • +
  • -

#7 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15275
  • View blog
  • Posts: 61,238
  • Joined: 12-June 08

Re: Cant kill the process with Process.Kill();

Posted 15 June 2016 - 11:29 AM

Okay.. then I am curious on 'why'. Why open up five apps to close them? What's the app design case, purpose, etc?
Was This Post Helpful? 0
  • +
  • -

#8 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6818
  • View blog
  • Posts: 28,231
  • Joined: 12-December 12

Re: Cant kill the process with Process.Kill();

Posted 15 June 2016 - 11:29 AM

View PostDOT1, on 15 June 2016 - 06:28 PM, said:

I know I should use an array its just that I didnt get to that point yet, and no I am not doing anything with manycam during that process

In my opinion (and many others I suspect) you should cover arrays long before you get to messing around with processes, it is a fundamental topic.
Was This Post Helpful? 1
  • +
  • -

#9 DOT1   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 153
  • Joined: 02-November 14

Re: Cant kill the process with Process.Kill();

Posted 15 June 2016 - 11:33 AM

View Postandrewsw, on 15 June 2016 - 11:29 AM, said:

View PostDOT1, on 15 June 2016 - 06:28 PM, said:

I know I should use an array its just that I didnt get to that point yet, and no I am not doing anything with manycam during that process

In my opinion (and many others I suspect) you should cover arrays long before you get to messing around with processes, it is a fundamental topic.

Haha I know how to use arrays its just that I didnt use them in the code, I didnt get to that part yet I wanted to finish the Process part first :P

And "Why" you ask.. To be honest I dont really know.. I was working on some software to speed up my PC a while ago and I remember being stuck on the same part and since my desktops PSU broke im on this laptop and instead of recreating the same application im doing this I guess.
Was This Post Helpful? 0
  • +
  • -

#10 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15275
  • View blog
  • Posts: 61,238
  • Joined: 12-June 08

Re: Cant kill the process with Process.Kill();

Posted 15 June 2016 - 11:35 AM

What? You don't know why you are doing what you are doing? That.. that doesn't make much sense.

Quote

I remember being stuck on the same part

What part?

Quote

desktops PSU broke im on this laptop and instead of recreating the same application im doing this I guess.

What are you guessing at? I mean you are typing some app out and you don't know why you are doing it? :huh2:
Was This Post Helpful? 0
  • +
  • -

#11 DOT1   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 153
  • Joined: 02-November 14

Re: Cant kill the process with Process.Kill();

Posted 15 June 2016 - 11:39 AM

When you guys put it like that you make me sound like an idiot :sad3: No but seriously I remember being stuck at a point where I had to kill processes to speed up my PC, as I said this was a while ago since my PSU is broken and I am creating this project because I wanted to start working on that project again but since I cannot access it I am recreating another application with the same concept.. Atleast for the part where I have to kill processes because I found that part being quite tricky.. since I got the same error last time in my other project.
Was This Post Helpful? 0
  • +
  • -

#12 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6818
  • View blog
  • Posts: 28,231
  • Joined: 12-December 12

Re: Cant kill the process with Process.Kill();

Posted 15 June 2016 - 11:45 AM

If the Process has already exited then you could just catch that Exception and ignore it. Or are you saying that it hasn't exited? Keep an eye on your Task Manager.
Was This Post Helpful? 0
  • +
  • -

#13 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15275
  • View blog
  • Posts: 61,238
  • Joined: 12-June 08

Re: Cant kill the process with Process.Kill();

Posted 15 June 2016 - 11:47 AM

Okay.. that is making more sense.

FYI - you can always pull out your hard drive and get a SATA/PATA to USB connector to have it hook up to your laptop... or an enclosure to make it an external drive.
Was This Post Helpful? 0
  • +
  • -

#14 DOT1   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 153
  • Joined: 02-November 14

Re: Cant kill the process with Process.Kill();

Posted 15 June 2016 - 11:48 AM

View Postandrewsw, on 15 June 2016 - 11:45 AM, said:

If the Process has already exited then you could just catch that Exception and ignore it. Or are you saying that it hasn't exited? Keep an eye on your Task Manager.

The thing is.. The software doesnt exit. If it kills the process in Task Manager.. it should make the software exit right ?

View Postmodi123_1, on 15 June 2016 - 11:47 AM, said:

Okay.. that is making more sense.

FYI - you can always pull out your hard drive and get a SATA/PATA to USB connector to have it hook up to your laptop... or an enclosure to make it an external drive.

I know but I dont want to mess with the computer at this point, since its like a week left until I get my 1070
Was This Post Helpful? 0
  • +
  • -

#15 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15275
  • View blog
  • Posts: 61,238
  • Joined: 12-June 08

Re: Cant kill the process with Process.Kill();

Posted 15 June 2016 - 11:50 AM

1070? As in the video card? Ah.. wouldn't being able to boot up with a functioning powersupply seem to take a little more precedence?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2