English 中文(简体)
Using C# Timer to stop executing a Program
原标题:
  • 时间:2009-11-12 14:35:29
  •  标签:
  • c#
  • timer

I want a program to stop executing for a certain amount of time. And i want this to happen in regular intervals. For example, i want a program to run for 5 minutes and then it should stop for 2 mintues and continue running for another 5 minutes after that. Is this possible with the C# Timer class?

最佳回答

You re looking for Thread.Sleep() passing in the number of milliseconds to pause execution for.

问题回答

I m not sure this is desirable behaviour, so if you update your question you might get a better answer than this.

You can use a timer that does little more than toggle a variable (e.g. bool). If that bool is used by the application, then you can use it to control whether the application is "running".

I m suggesting this instead of Thread.Sleep() because at least your application is still responsive. If you want to pause a non-UI thread, then Thread.Sleep() will suffice, but don t call Thread.Sleep() on the UI thread, even with very short durations.

As previous answer say: what is stop ? The user can t use the program for 2 minutes? If so you could pop a modal dialog (with text) and the user can t close it.

If this is a Windows Forms application, you might want to consider moving this into a threaded execution model. You can do this using either the BackgroundWorker control, thread pooling with the ThreadPool class, asynchronous methods Begin and End (such as Stream.BeginWrite), or by manually handling the thread yourself (bit more complex).

A BackgroundWorker will provide the easiest form of development by allowing you to handle events for the asynchronous code, and update a progress bar or label to show the current state of the execution. This will allow you to use Thread.Sleep without the system warning the user that the application has hung.

Basically, in Windows Forms development you should be using some form of threading to handle long executions.

You can use Window.Forms.Timer to register a callback each 1000ms

        private int counter;
        void StartTimer()
        {
            counter = 0;
            Timer timer = new Timer();
            timer.Interval = 1000;
            timer.Enabled = true;
            timer.Tick += Timer_Tick;
        }

In the event function simply increment a global counter variable that executes when a condition fulfills


        private void Timer_Tick(object sender, EventArgs e)
        {
            counter++;
        }




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签