I am trying to offer customers the ability to edit image files that are on a network drive by using ProcessStart within a WinForms application to launch the editing program for the file. It launches the editing program just fine with the image; however, it will not allow them to save changes resulting in an error:
A sharing violation occurred while accessing [File location and Name].
If I keep the launched editing application open and close the WinForms application that launched the editor and then attempt to save the changes, it allows the changes to be saved 100% of the time without a problem. Believing that the ProcessStart was not actually launching the process in a decoupled thread, I tried to launch the editing application using a new thread and that resulted in the same situation. How can I launch the editing program from the WinForms app as a standalone, decoupled, program so the user can save the changes without closing the WinForms application? I appreciate any insight into what I am not doing or not considering as an option.
This is the method I am calling to launch the editing program. The ProcessExe is the name of the editing program on the users machines, the workingDirectory is the same value as the network file location, and the args is the fileLocation and Name:
private void pbxImage_ButtonClick(object sender, EventArgs e)
{
try
{
string strImageNum = BDL.Data["ImageNumber"].ToString();
string strDirectory = ConfigurationManager.AppSettings["ImageLocation"];
string fileName = string.Empty;
fileName = string.Format("{0}\\{1}{2}", strDirectory, strImageNum, ".jpg");
HelperFunctions.RunProcess("mspaint.exe", strDirectory, fileName);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public static void RunProcess(string ProcessExe, string workingDirectory, params string[] args)
{
Process myProcess = new Process();
try
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.Arguments = HelperFunctions.EscapeArguments(args); startInfo.CreateNoWindow = false;
startInfo.FileName = App.Shared.HelperFunctions.FindExePath(ProcessExe); startInfo.WorkingDirectory = workingDirectory;
startInfo.RedirectStandardError = false;
startInfo.RedirectStandardInput = false;
startInfo.RedirectStandardOutput = false;
startInfo.UseShellExecute = true;
myProcess.StartInfo = startInfo;
Thread thread = new Thread(new ThreadStart(delegate { myProcess.Start(); }));
thread.TrySetApartmentState(ApartmentState.STA);
thread.Start();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}