I'm coding an editor for a game with C#. My programm openes as .txt File by starting a notepad.exe process. If that process exits, I want to call a function within the main form (to update the textbox). Here's what I'm doing so far:
void OpenTextEditor(TreeNode node)
{
Process editor = new Process();
editor.StartInfo.WorkingDirectory = "%WINDIR%";
editor.StartInfo.FileName = "notepad.exe";
var txtfilelocation = GetRealPathByNode(node);
var txtfile = File.ReadAllText(txtfilelocation,Encoding.Default);
txtfile = txtfile.Replace("\n", "\r\n");
File.WriteAllText(txtfilelocation,txtfile,Encoding.Default);
editor.StartInfo.Arguments = txtfilelocation;
editor.EnableRaisingEvents = true;
editor.Exited += delegate {
NotePadHasEnded(node);
};
editor.Start(); //starten
}
public Delegate NotePadHasEnded(TreeNode node)
{
var txtfilelocation = GetRealPathByNode(node);
var newfileloc = txtfilelocation;
var newfile = File.ReadAllText(newfileloc, Encoding.Default);
newfile = newfile.Replace("\r\n", "\n");
File.WriteAllText(txtfilelocation, newfile, Encoding.Default);
if (treeView1.SelectedNode == node) DisplayText(node);
return null;
}
The GetRealPathByNode() function returns a string of the full path of the File which the TreeView node points at. DisplayText() reads the text from the file the node points at and displays that in a richtextbox.
Upon executing, my main form is still usable as I wanted it, but when the process is terminated (notepad closed), it throws an error stating that the function NotePadHasEnded has no access to the treeView1 object because it is being executed in another process.
How can I create a process that calls a function in my main form when it is being exited, asynchronously? I know that it works when I use the WaitForExit() function, but then my Form freezes and waits until notepad closes. I want the user to be able to open up other txt files with the editor and when one editor is being closed that the richtextbox text ist being updated in my GUI.
/Edit/ Now Solved. Thanks to Woodman's answer, I replaced
editor.Exited += delegate {
NotePadHasEnded(node);
};
with
editor.Exited += delegate
{
this.Invoke((MethodInvoker)delegate()
{
NotePadHasEnded(node);
});
};