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 ?
VulpesPosted Aug 4, 2013, 4:18 PM
Check this SO thread:
http://stackoverflow.com/questions/6156327/open-explorer-window-and-wait-for-it-to-close
The second problem is caused by the Exit event being raised on a subsidiary thread. Any changes to the form therefore need to be marshaled back to the main UI thread on which it was created to avoid the error.
To do this, try replacing this line:
Incidentally, you can reply to your own threads on this site - there's no need to start a new one.