How can I launch a new standalone app from a winforms app

Oct 31 2014 6:59 PM
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); //format the arguments correctly.
            startInfo.CreateNoWindow = false;
            startInfo.FileName = App.Shared.HelperFunctions.FindExePath(ProcessExe); //Name of the program to be opened
            //Tried setting the working directory to %USERPROFILE%\Documents and no difference
            startInfo.WorkingDirectory = workingDirectory;
            startInfo.RedirectStandardError = false;
            startInfo.RedirectStandardInput = false;
            startInfo.RedirectStandardOutput = false;
            startInfo.UseShellExecute = true;
            myProcess.StartInfo = startInfo;
            //myProcess.Start(); //Tried this straight forward method call and it failed
            //Tried to use a new thread
            Thread thread = new Thread(new ThreadStart(delegate { myProcess.Start(); }));
            //Tried without setting the apartment state, and setting as MTA and STA with the same result
            thread.TrySetApartmentState(ApartmentState.STA);
            thread.Start();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }
 

Answers (1)