Daniel Lipman

Daniel Lipman

  • 1.4k
  • 371
  • 13.6k

Why my process is jumping to the Exited event automatic ?

Aug 4 2013 2:36 PM
At the top of Form1 i did:

private Process zipFileDirectoryProcess;

In the constructor i did:

zipFileDirectoryProcess = new Process();
zipFileDirectoryProcess.StartInfo.FileName = "explorer.exe";
zipFileDirectoryProcess.StartInfo.CreateNoWindow = true;
zipFileDirectoryProcess.EnableRaisingEvents = true;
zipFileDirectoryProcess.Exited += new EventHandler(zipFileDirectoryProcess_Exited);


Then i have a method that is called from a button click event:

private void Process()
{
zipFileDirectoryProcess.StartInfo.Arguments = zipFileDirectoryProcess.StartInfo.Arguments = "/select," + Path.GetFullPath(t);
zipFileDirectoryProcess.Start();
this.TopMost = true;
}


Then in the bottom i have the Exited event:

private void zipFileDirectoryProcess_Exited(object sender, EventArgs e)
        {
            this.TopMost = false;
        }


The first problem is once i click the button that call the method and start the process after few seconds automatic its jumping to the Exited event !
I want that it will go/jump to the Exited event only when i close the Process window .

The second problem once its getting to the event Exited and trying to do the line: this.TopMost = false;  im getting an exception:

Cross-thread operation not valid: Control 'Form1' accessed from a thread other than the thread it was created on

System.InvalidOperationException was unhandled
  HResult=-2146233079
  Message=Cross-thread operation not valid: Control 'Form1' accessed from a thread other than the thread it was created on.
  Source=System.Windows.Forms
  StackTrace:
       at System.Windows.Forms.Control.get_Handle()
       at System.Windows.Forms.Form.set_TopMost(Boolean value)
       at Diagnostic_Tool_Blue_Screen.Form1.zipFileDirectoryProcess_Exited(Object sender, EventArgs e) in d:\C-Sharp\Diagnostic Tool Blue Screen\Diagnostic Tool Blue Screen\Diagnostic Tool Blue Screen\Form1.cs:line 579
       at System.Diagnostics.Process.RaiseOnExited()
       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._ThreadPoolWaitOrTimerCallback.PerformWaitOrTimerCallback(Object state, Boolean timedOut)
  InnerException: 


How can i solve this two problems ?

Answers (1)