繁体   English   中英

为什么方法不间断返回true却不间断返回false? C#

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

我在这里有些困惑。

背景:我有一个应用程序记录用户选择要显示的文件后查看过的文件。

但是,在他们选择了隐藏的文件直到关闭正在查看的文件之前,我还会出现一个WinForm。

以下是相关代码:

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;
        }
    }
}

该问题出现在while (CheckFileIsOpen() == false) 如果我在Visual Studio稍稍休息一下然后逐步执行,程序将按预期运行(窗体保持隐藏状态,直到进程消失)。 但是,如果我不中断运行,则好像该进程从未运行过。

我在while (CheckFileIsOpen() == false)语句之前尝试过Thread.Sleep(1000) ,看看是否只是将线程停止几秒钟就可以让它有机会打开进程,但是整个应用程序只是无限期冻结。

问题:我的应用程序是否反应太快而无法捕获进程并在它们打开之前触发? 如果是这样,我有什么选择可以用来防止它直接跳到假定没有进程打开的情况?

感谢您的时间。

编辑:

发布此消息后几分钟,我终于找到了解决方案。 我改变了一些执行步骤,最后使用Process.WaitForExit()方法满足了我的要求。

万一有人怀疑,这是CheckFileIsOpen()现在的工作方式:

 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();
        }
      }

使用while循环方法时,您将阻止gui thead,并且应用程序将冻结。

在构造函数中调用this.Hide()并使用另一个计时器。 在计时器滴答事件中,如果返回false,则调用方法CheckFileIsOpen() ,请调用this.Show()并停止计时器。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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