简体   繁体   中英

Error: 'The process cannot access the file because it is being used by another process' in Visual Studio c#

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?

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 wrt this answer. The code is based off of the code found here: FileStream Tutorial website

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM