在C#WPF中运行一个进程至少5秒

时间:2012-05-07 20:01:05

标签: c# wpf process timer

我的目标是强制进程运行至少5秒(真正的任何时间)。我正在使用带有Service Pack 1的.NET Framework 3.5。想法是该文档包含用户必须看到的一些信息,因此为了安全地防止它们立即单击以关闭文档,我们可以强制它保持打开状态一段时间我开发了一个小测试UI,包括一个按钮,然后是三个单选按钮(每个文档一个)。这是我背后的代码......

我定义了文件路径的字符串,所选文件路径的字符串,int用于存储进程的ID,如果可以退出程序则为boolean,以及线程和计时器声明,如..

string WordDocPath = @"Some file path\TestDoc_1.docx";
string PowerPointPath = @"Some file path\Test PowerPoint.pptx";
string TextFilePath = @"Some file path\TestText.txt";
string processPath;
int processID;
bool canExit = false;

System.Threading.Thread processThread;
System.Timers.Timer processTimer;

在构造函数中,我初始化线程和计时器,将线程的start方法设置为一个名为TimerKeeper()的方法,然后启动线程。

processTimer = new System.Timers.Timer();
processThread = new System.Threading.Thread(new System.Threading.ThreadStart(timeKeeper));
processThread.Start();

我将计时器设置为5秒,然后将canExit布尔值设置为true。

    public void timeKeeper()
    {
        processTimer.Elapsed += new System.Timers.ElapsedEventHandler(processTimer_Elapsed);
        processTimer.AutoReset = false;
        processTimer.Interval = 5000;               //5000 milliseconds = 5 seconds
    }

    void processTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
        canExit = true;
    }

其余的是我的按钮的点击事件,它取消了用于启动进程的文件路径,启动计时器,然后自己启动进程..

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        if ((bool)PowerPointRadioButton.IsChecked)
        {
            processPath = PowerPointPath;
        }

        if ((bool)WordDocRadioButton.IsChecked)
        {
            processPath = WordDocPath;
        }

        if ((bool)TextDocRadioButton.IsChecked)
        {
            processPath = TextFilePath;
        }

        try
        {
            canExit = false;
            processTimer.Start();

            while (!canExit)
            {
                processID = System.Diagnostics.Process.Start(processPath).Id;      
                System.Diagnostics.Process.GetProcessById(processID).WaitForExit();

                if (!canExit)
                {
                    processTimer.Stop();

                    MessageBox.Show("Document must remain open for at least 5 seconds.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);

                    processTimer.Start();                   
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error dealing with the process.\n" + ex.Message.ToString());
        }

这在大多数情况下确实有效。用户仍然可以关闭文档,但如果还没有5秒,它将重新打开。除了word文档(.docx)。对于powerpoint和文本文件,情况很顺利,但word文档有一些奇怪的行为(请注意,所有3个文件都在同一个文件目录中)。当我选择单词文档单选按钮并单击按钮时,单词文档打开,但我也会从catch块中提示消息框,提醒我“对象引用未设置为对象上的实例”异常是抛出。这仅适用于word文档。就像我说的那样,单词文档仍然打开(我可以看到它的内容,就像powerpoint或textfile一样)。该异常导致检查的行是否可以退出以跳过,因此文档可以立即关闭,这是一个问题。

有人能在这看到我的问题吗?或者,如果有更好的方法来做所有这些(我是一个wpf / c #newbie)?我只是不明白为什么这只出现在word文档中,而不是powerpoint和text文件。

2 个答案:

答案 0 :(得分:1)

如果在用户桌面上运行,则需要安装正确的应用程序(例如Word)及其配置方式。如果这些是共享上的只读文件,那么我可以将它们转换为XPS,以便您可以在DocumentViewer中显示它们。而不是强迫他们等待5秒钟点击让他们对一个他们已阅读并理解文档的对话框说“是”。或者在一个带有"我同意" MilkyWayJoe建议按钮。

答案 1 :(得分:0)

问题可能在于关联的应用程序不是单词应用程序本身,而是一些代表您启动单词的中间应用程序。

要查明,请保留对流程对象的引用,并检查它是否已经终止,它的可执行路径是什么。

话虽如此,你为什么需要这种烦人的行为?你不能阻止人们从另一个方向看。是否满足某些法律要求?