title: Run a process for at least 5 seconds in C# WPF
body: <p>My goal is to force a process to run for at least 5 seconds (any amount of time really).  I am working of the .NET Framework 3.5, with Service Pack 1.  The idea is that the document holds some information that the user must see, so to safe guard against them immediately clicking to close the document, we can force it to stay open for some time.  I developed a small test UI, consisting of a button, and then three radio buttons (one for each document).  Here is my code behind...</p>
  
  <p>I define the strings for the file paths, the string for the chosen file's path, and int to store the process's ID, a boolean for if they can exit the program, and the thread and timer declarations such as..</p>
  
  <pre><code>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;
  </code></pre>
  
  <p>In the constructor, I initialize the thread and timer, setting the thread's start method to a method called TimerKeeper(), and then I start the thread.</p>
  
  <pre><code>processTimer = new System.Timers.Timer();
  processThread = new System.Threading.Thread(new System.Threading.ThreadStart(timeKeeper));
  processThread.Start();
  </code></pre>
  
  <p>I have the timer set to count to 5 seconds, upon which it will set the canExit boolean to true.</p>
  
  <pre><code>    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;
      }
  </code></pre>
  
  <p>The rest is my button's click event, which deicides which file path to use to start the process, starts the timer, and then starts the process itself..</p>
  
  <pre><code>    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());
          }
  </code></pre>
  
  <p>This actually works, for the most part.  The user still can close the document, but if it has not been 5 seconds, it will reopen.  Except for the word document (.docx).  Things go smoothly for the powerpoint and text files, but the word document has some strange behavior (please note that all 3 files are in the same file directory).  When I choose the word documents radio button and click the button, the word document opens, BUT I am also prompted with the message box from the catch block, alerting me that a "Object reference not set to an instance on an object" exception was thrown.  This only occurs for the word document.  Like I said, the word document still opens (I can see it's contents, just like the powerpoint or textfile).  The exception causes the lines that check to see if they can exit to be skipped, so the document can close immediately, which is a problem.</p>
  
  <p>Can anyone see my issue here?  Or if there is a better way to doing all of this (I am a wpf/c# newbie)?  I just don't understand why this only occurs for the word document, and not the powerpoint and text files.</p>