Click here to Skip to main content
15,905,068 members

Comments by dageraad (Top 3 by date)

dageraad 2-Feb-24 4:38am View    
Yes refactoring is a good advice. But it was not causing the issue at hand.
dageraad 2-Feb-24 4:36am View    
No way, it works very fine! Thanks a lot this is great :D
dageraad 1-Feb-24 14:32pm View    
I tried just that but the `catch` seems unreachable. After the exception the program just halts. I tried catching different kinds of Exceptions but IOException should work. As seen in code I tried an extensive IsFileLocked check too.

int retryCount = 0;
bool fileOpenedSuccessfully = false;

while (!fileOpenedSuccessfully && retryCount < maxRetries)
{
    try
    {
        using (FileStream fs = new FileStream(imagePath, FileMode.Open, FileAccess.Read, FileShare.None))
        {
            fileOpenedSuccessfully = true;
        }
        //if (!IsFileLocked(imagePath))
        //{
        //    fileOpenedSuccessfully = true;
        //}
    }
    catch (Exception ex)
    {
        Console.WriteLine($"Exception: {ex.GetType().Name}, Message: {ex.Message}");
        Thread.Sleep(retryDelayMilliseconds);
        retryCount++;
    }
}

if (fileOpenedSuccessfully)
{
    .....