0

I have no coding experience but have been trying to fix a broken program many years ago. I've been fumbling through fixing things but have stumbled upon a piece that I can't fix. From what I've gathered you get Alexa to append a Dropbox file and the program reads that file looking for the change and, depending on what it is, executes a certain command based on a customizable list in an XML document.

I've gotten this to work about five times in the hundred of attempts I've done, every other time it will crash and Visual Studio gives me: "System.IO.IOException: 'The process cannot access the file 'C:\Users\\"User"\Dropbox\controlcomputer\controlfile.txt' because it is being used by another process.'"

This is the file that Dropbox appends and this only happens when I append the file, otherwise, the program works fine and I can navigate it. I believe this is the code that handles this as this is the only mention of StreamReader in all of the code:

public static void launchTaskControlFile(string path)
    {
        int num = 0;
        StreamReader streamReader = new StreamReader(path);
        string str = "";
        while (true)
        {
            string str1 = streamReader.ReadLine();
            string str2 = str1;
            if (str1 == null)
            {
                break;
            }
            str = str2.TrimStart(new char[] { '#' });
            num++;
        }
        streamReader.Close();
        if (str.Contains("Google"))
        {
            MainWindow.googleSearch(str);
        }
        else if (str.Contains("LockDown") && Settings.Default.lockdownEnabled)
        {
            MainWindow.executeLock();
        }
        else if (str.Contains("Shutdown") && Settings.Default.shutdownEnabled)
        {
            MainWindow.executeShutdown();
        }
        else if (str.Contains("Restart") && Settings.Default.restartEnabled)
        {
            MainWindow.executeRestart();
        }
        else if (!str.Contains("Password"))
        {
            MainWindow.launchApplication(str);
        }
        else
        {
            SendKeys.SendWait(" ");
            Thread.Sleep(500);
            string str3 = "potato";
            for (int i = 0; i < str3.Length; i++)
            {
                SendKeys.SendWait(str3[i].ToString());
            }
        }
        Console.ReadLine();
    }

I've searched online but have no idea how I could apply anything I've found to this. Once again before working on this I have no coding experience so act like you're talking to a toddler.

Sorry if anything I added here is unnecessary I'm just trying to be thorough. Any help would be appreciated.

fatihyildizhan
  • 8,614
  • 7
  • 64
  • 88
  • _"...I believe this is the code that handles this..."_, no need to guess: exceptions come with a stack trace. Also note that that code could be greatly simplified with `File.ReadAllLines(path).Where(x => !x.StartsWith("#"))` (and few other things unrelated to this question). I'd say that it also depends on how this file is updated (replace or append?) and how your program monitors for changes (`FileSystemWatcher` or dumb polling?) You might need to wait and/or retry (see also https://stackoverflow.com/q/26741191/1207195). Best? MCVE please. – Adriano Repetti Aug 12 '20 at 07:17
  • If the file is already open, 9 times out of 10 its you that forgot to close it. Otherwise its some other program or your virus checker. – TheGeneral Aug 12 '20 at 07:44
  • I'm appending the file and I need the program to only read what has changed while ignoring the rest of the file. It's using FileSystemWatcher. As for it potentially being the case of the file being opened or another program opening it, the file stays closed constantly on my end so it's not that. As I said, another program opening it is the problem, Dropbox has to append the file and right as I trigger Dropbox to do so the program crashes. Since I can't fix the issue on Dropbox's end I have to fix it on mine. – Magical Trout99 Aug 13 '20 at 05:43
  • Change notification could be triggered while the file is still open. You could either postpone the action or (better) use a try/delay pattern (see linked SO post). If the file is in use (check HRESULT) then you wait for 100/200 ms and then try again (for a couple of times, at least). – Adriano Repetti Aug 13 '20 at 09:51

1 Answers1

0

I set up a try delay pattern like Adriano Repetti said and it seems to be working, however doing that flat out would only cause it to not crash so I had to add a loop around it and set the loop to stop when a variable hit 1, which happened whenever any command types are triggered. This takes it out of the loop and sets the integer back to 0, triggering the loop again. That seems to be working now.