Hack Forums
[C#] Application closes with while loop running? - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [C#] Application closes with while loop running? (/showthread.php?tid=5436420)



Application closes with while loop running? - Baitable - Oct 17, 2016

Hey, I currently have a while loop running in my application (download and execute from link) and it seems that my stub shuts itself down even though I have a while loop running and have not instructed it to close.

Code
    public static void Persistance(FileInfo file) {
    
    while(true) {
        if(!IsFileinUse(file)) {
             StartFile(file.FullName);
        }
    }    
    }

Code
        if(persistance) {
            IsFileinUse(file);
        }

Code
public static bool IsFileinUse(FileInfo file)
{
     FileStream stream = null;

     try
     {
         stream = file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None);
     }
     catch (IOException)
     {
         return true;
     }
     finally
     {
         if (stream != null)
         stream.Close();
     }
     return false;
}



* - He B TeMy - Oct 18, 2016

Is this your UI thread? (If you have a UI ofc)


* - ASMR - Oct 19, 2016

at first I'd suspect that the stream.Close() causes an exception and that makes it fall out of the while loop on exception. but not sure.


* - Ninja - Oct 19, 2016

Debug it. See when it exits the loop.