Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7898289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T08:23:34+00:00 2026-06-03T08:23:34+00:00

My goal is to force a process to run for at least 5 seconds

  • 0

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…

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..

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;

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.

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

I have the timer set to count to 5 seconds, upon which it will set the canExit boolean to 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;
    }

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..

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

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.

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T08:23:36+00:00Added an answer on June 3, 2026 at 8:23 am

    The problem could be that the associated application is not the word application itself, but some intermediate application that launches word on your behalf.

    To find out, keep a reference to the process object, and check if it has already terminated, what it’s executable path is.

    Having said that, why do you need this annoying behavior? You cant stop people from looking the other way. Is it supossed to fullfill some legal requirement or something?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Goal Pass images generated by one process efficiently and at very high speed to
In Linux (CentOS 5.4), how do you force a process to stop locking a
can any one tell me how to force maven to precede mapping .hbm.xml files
Long time lurker, first time poster! Goal My ultimate goal is to make a
Code works, but feels very brute force, suggestions? Goal of the code is to
Goal: to programmatically determine the sizes (in bytes) of the fields of a class.
goal: I have the string 1234432144 I want to only replace the first 2
Goal To use a CREATE TYPE statement in HSQLDB 2.0.0 to create a user-defined
Goal : Our application is built using multiple types (e.g. Person, PersonSite(ICollection), Site -
Goal I need to alter a number of almost identical triggers on a number

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.