简体   繁体   中英

Why is method returning true without break but false with break? C#

I have a bit of a puzzler here.

Background: I have an application that logs files that have been viewed after a user selects which files to show.

However, I also have a WinForm appear after they have selected the file that remains hidden until they close out of the file they are viewing.

Here is the relevant code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Diagnostics;
using System.Threading;

namespace ViewTracker
{
    public partial class NewFile : Form
    {
        //checks to see if a file is open       
        public NewFile()
        {
            InitializeComponent();
            //
            OpenDocuments();
            //starts the timer component
            tm_CountDown.Start();


            while (CheckFileIsOpen() == false)
            {
                this.Hide();
            }
            this.Show();
        }

        //timer to count down
        //executes every 1 second, interval of the timer component
        private void tm_CountDown_Tick(object sender, EventArgs e)
        {
        }
        #endregion

        #region METHODS AND EVENTS

        //opens documents based on file selection
        private void OpenDocuments()
        {
        }

        //SHUT DOWN EVERYTHING (files at least)
        private void CloseEverything()
        {
        }

        //checks to see if a file is open and when the file closes shows the new file select dialog
        private bool CheckFileIsOpen()
        {
            Process[] pr_excel = Process.GetProcessesByName("EXCEL");
            Process[] pr_word = Process.GetProcessesByName("WINWORD");
            Process[] pr_pdf = Process.GetProcessesByName("ACROBAT");
            if (pr_excel.Count() != 0)
            {
                while (pr_excel[0].HasExited == false)
                {
                    return false;
                }
            }

            else if (pr_word.Count() != 0)
            {
                while (pr_word[0].HasExited == false)
                {
                    return false;
                }
            }

            else if (pr_pdf.Count() != 0)
            {
                while (pr_pdf[0].HasExited == false) ;
                {
                    return false;
                }
            }

            else if (prisonImages.Visible == true)
            {
                while (prisonImages.Visible == true)
                {
                    return false;
                }
            }

            return true;
        }
    }
}

The issue arises at while (CheckFileIsOpen() == false) . If I put a break in Visual Studio at the line and then step through, the program runs as expected (the form stays hidden until the processes are gone). However, if I run without the break, it appears as if the process was never running.

I have tried Thread.Sleep(1000) before the while (CheckFileIsOpen() == false) statement to see if maybe just stopping the thread for a few seconds may give it a chance to let the process open, but then the whole application just freezes indefinitely.

The questions: Is my application just reacting too fast to catch the processes and firing before they open? And if so, what are some options I can use to try and keep it from jumping straight to assuming no processes are open?

Thank you for your time.

EDIT:

I ended up finding a solution a few minutes after posting this. I changed around some of execution steps and ended up using the Process.WaitForExit() method to meet my requirements.

In case anyone is wondering, here is how CheckFileIsOpen() works now:

 private void CheckFileIsOpen()
    {
        Process[] pr_excel = Process.GetProcessesByName("EXCEL");
        Process[] pr_word = Process.GetProcessesByName("WINWORD");
        Process[] pr_pdf = Process.GetProcessesByName("ACROBAT");
        if (pr_excel.Count() != 0)
        {
            pr_excel[0].WaitForExit();
        }

        else if (pr_word.Count() != 0)
        {
            pr_word[0].WaitForExit();
        }

        else if (pr_pdf.Count() != 0)
        {
            pr_pdf[0].WaitForExit();
        }
      }

With your while-loop approach you are blocking the gui thead and your application freezes.

Call this.Hide() in your constructor and use another timer. In the timer tick event call your method CheckFileIsOpen() if this returns false call this.Show() and stop the timer.

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