0

I am having some trouble with this error in Visual Studio:

An unhandled exception of type 'System.IO.IOException' occurred in mscorlib.dll

Additional information: The process cannot access the file 'C:\Users\aheij\Desktop\KinectOutput\swipe.txt' because it is being used by another process.

What I Have tried: I have tried using these codes obtained from other solved StackOverflow questions similar to mine to try to solve the problem - but even that didn't seem to work? Ive tried checking if the file is in use, but with no success.

I also run Visual Studio as administrator.

The file is not read-only.

The folder is not open, and the file is not being used in any other program when executing the code - at least not that I can see/know of.

The code: So, to add some context to my code: I am writing some quick gesture detection code to the Kinect c# BodyBasics SDK v2 code freely available. This is a helper method that I added, that gets called in when a person is in view. If that person is executing the gesture, the method writes the frame time and gesture name to a text file.

Half the time, when the code does work, the gesture recognition works well. However, the other half of the time, the code breaks/stops because the writing to file bit causes the error.

Below is my code to see if the person is standing in the neutral position - its a bit waffly, so apologies about that. I have commented 'ERROR' where the error is (unsurprisingly):

private void Neutral_stance(VisualStyleElement.Tab.Body body, IReadOnlyDictionary<JointType, Joint> joints, IDictionary<JointType, Point> jointPoints, BodyFrame bf)
{
    CameraSpacePoint left_hand = joints[JointType.HandLeft].Position;
    CameraSpacePoint left_elbow = joints[JointType.ElbowLeft].Position;
    CameraSpacePoint left_shoulder = joints[JointType.ShoulderLeft].Position;
    CameraSpacePoint left_hip = joints[JointType.HipLeft].Position;
    CameraSpacePoint right_hand = joints[JointType.HandRight].Position;
    CameraSpacePoint right_elbow = joints[JointType.ElbowRight].Position;
    CameraSpacePoint right_shoulder = joints[JointType.ShoulderRight].Position;
    CameraSpacePoint right_hip = joints[JointType.HipRight].Position;

    double vertical_error = 0.15;
    double shoulderhand_xrange_l = Math.Abs(left_hand.X - left_shoulder.X);
    double shoulderhand_xrange_r = Math.Abs(right_hand.X - right_shoulder.X);

    if (bf != null)
    {
        TimeSpan frametime = bf.RelativeTime;
        string path_p = @"C:\Users\aheij\Desktop\KinectOutput\Punch.txt"; //write to punch file
        string path_s = @"C:\Users\aheij\Desktop\KinectOutput\swipe.txt"; //write to swipe file
        if (left_hand.Y < left_elbow.Y)
        {
            if (right_hand.Y < right_elbow.Y)
            {
                if (shoulderhand_xrange_l < vertical_error)
                {
                    if (shoulderhand_xrange_r < vertical_error)
                    {
                        Gesture_being_done.Text = "  Neutral";
                        File.AppendAllText(path_p, frametime.ToString() + "    Neutral" + Environment.NewLine); //ERROR
                        File.AppendAllText(path_s, frametime.ToString() + "    Neutral" + Environment.NewLine); //ERROR
                    }
                }
            }
        }
        else
        {
            Gesture_being_done.Text = "  Unknown";
            File.AppendAllText(path_p, frametime.ToString() + "    Unknown" + Environment.NewLine); //ERROR
            File.AppendAllText(path_s, frametime.ToString() + "    Unknown" + Environment.NewLine); //ERROR
        }
    }

}

Any solutions/ideas/suggestions to point me on the right track would be appreciated. I think that it would be good to use the 'using streamwriter' method as opposed to the method I am using here - but I am not sure how? Any help would be appreciated.

Additonal Info: Using Visual Studio 2015; Using windows 10.

Sidenote: I read somewhere that the Windows Search tool in Windows 10 can interfere and cause problems like this so I need to disable it?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Aheijna
  • 28
  • 5
  • Have you tried to open the file as `FileStream` as described [here](https://msdn.microsoft.com/en-us/library/5h0z48dh(v=vs.110).aspx), and then append the text? – Harry Apr 04 '17 at 16:09
  • These problems are almost always caused by opening the file in your own program and never closing it. Search everywhere the `swipe.txt` file is used in your program and ensure it is opened, modified, then closed soon. – Dour High Arch Apr 04 '17 at 16:11
  • Having this code run on a worker thread is another simple explanation. If it runs more than once then you'll have this problem. Use Debug > Windows > Threads when you get the exception, make it thread-safe with the `lock` statement. – Hans Passant Apr 04 '17 at 16:18
  • What you could do is check to see if the file is in use, then if it is, try to kill the connection, then try to do your business. Working with files is really a pain in the rear end. – TGarrett Apr 04 '17 at 16:32
  • @Harry the FileStream method seems to have done the trick nicely. I also ensured the file was closed after opening it, so so far no issues have come up. – Aheijna Apr 06 '17 at 12:19

1 Answers1

0

As suggested to me I used the Filestream method & ensured the file was closed after use. But, even this still caused the same error.

Thus, I also got rid of having two file-writing actions in rapid succession of each other. I dont know if this is technically right or even true, but based off of this post here: link, my error could be coming up because I am trying to execute the second 'write to text file' line whilst the previous 'write to text file' line is still executing/writing to that same folder & location - hence the clash? Please someone, correct me if I am wrong.

Either way, this seems to have worked. See below for my edited/corrected method:

        private void Neutral_stance(Body body, IReadOnlyDictionary<JointType, Joint> joints, IDictionary<JointType, Point> jointPoints, BodyFrame bf)
    {
        //cameraspace point joint stuff here again (see original post for this bit leading up to the if statements.)

        if (bf != null)
        {
            TimeSpan frametime = bf.RelativeTime;
            string path_s = @"C:\Users\aheij\Desktop\KinectOutput\swipe.txt";

            if (left_hand.Y < left_elbow.Y)
            {
                if (right_hand.Y < right_elbow.Y)
                {
                    if (shoulderhand_xrange_l < vertical_error)
                    {
                        if (shoulderhand_xrange_r < vertical_error)
                        {
                            Gesture_being_done.Text = "  Neutral";

                            FileStream fs_s = new FileStream(path_s, FileMode.Append); //swipe
                            byte[] bdatas = Encoding.Default.GetBytes(frametime.ToString() + "    Neutral" + Environment.NewLine);
                            fs_s.Write(bdatas, 0, bdatas.Length);
                            fs_s.Close();
                        }
                    }
                }
            }
            else
            {
                Gesture_being_done.Text = "  Unknown";
                FileStream fs_s = new FileStream(path_s, FileMode.Append);
                byte[] bdatas = Encoding.Default.GetBytes(frametime.ToString() + "    Unknown" + Environment.NewLine);
                fs_s.Write(bdatas, 0, bdatas.Length);
                fs_s.Close();
              }
        }

    }

Do let me know if there is any way I can make this more elegant or anything else I should be aware of w.r.t this answer. The code is based off of the code found here: FileStream Tutorial website

Community
  • 1
  • 1
Aheijna
  • 28
  • 5