-1

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

    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);
        }
    }

Calling Code:

    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);
        }
    }

I used the code from VDohnal (stackoverflow.com/a/20623302/2224701) and verified that the image file was in fact locked by the host WinForms application.

Community
  • 1
  • 1
Edward
  • 261
  • 2
  • 10
  • 4
    Doesn't sound like a threading problem. Sounds like your program has a lock on the image file. You aren't showing us that code. – LarsTech Oct 31 '14 at 01:19
  • Use this code to find out what process exactly locks the file http://stackoverflow.com/a/20623302/2224701 – Vojtěch Dohnal Oct 31 '14 at 07:44
  • I will try out the code from the link. You are correct that the program creates a lock on the image file when it launches the ProcessStart. As far as the calling code goes it is a simple ButtonClick event" – Edward Oct 31 '14 at 14:58

1 Answers1

0

Thank you to Garth J. Lancaster. Garth pointed out that I was using Picture.Load(file) to display the image in the WinForms application. The act of doing that locked the image file; so, when I then tried to open the image file into MS Paint with the click event the file was already locked creating a sharing condition. The solution was to create a bitmap from a byte[] from the image file and using Picture.Image = bitmap. I was not aware that the Picture.Load() created a lock on the source image file.

Edward
  • 261
  • 2
  • 10