-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Removed duplicate thread timers topics #6268
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| --- | ||
| title: "Timers" | ||
| ms.date: "03/30/2017" | ||
| description: Learn what .NET timers to use in a multithreaded environment. | ||
| ms.date: "07/03/2018" | ||
| ms.technology: dotnet-standard | ||
| dev_langs: | ||
| - "csharp" | ||
|
|
@@ -10,23 +11,47 @@ helpviewer_keywords: | |
| - "threading [.NET Framework], timers" | ||
| - "timers, about timers" | ||
| ms.assetid: 7091500d-be18-499b-a942-95366ce185e5 | ||
| author: "rpetrusha" | ||
| author: "pkulikov" | ||
| ms.author: "ronpet" | ||
| --- | ||
| # Timers | ||
| Timers are lightweight objects that enable you to specify a delegate to be called at a specified time. A thread in the thread pool performs the wait operation. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that I would add this section back and add to it, something like:
|
||
|
|
||
| Using the <xref:System.Threading.Timer?displayProperty=nameWithType> class is straightforward. You create a **Timer**, passing a <xref:System.Threading.TimerCallback> delegate to the callback method, an object representing state that will be passed to the callback, an initial raise time, and a time representing the period between callback invocations. To cancel a pending timer, call the **Timer.Dispose** function. | ||
|
|
||
|
|
||
| .NET provides two timers to use in a multithreaded environment: | ||
|
|
||
| - <xref:System.Threading.Timer?displayProperty=nameWithType>, which executes a single callback method on a <xref:System.Threading.ThreadPool> thread at regular intervals. | ||
| - <xref:System.Timers.Timer?displayProperty=nameWithType>, which by default raises an event on a <xref:System.Threading.ThreadPool> thread at regular intervals. | ||
|
|
||
| > [!NOTE] | ||
| > There are two other timer classes. The <xref:System.Windows.Forms.Timer?displayProperty=nameWithType> class is a control that works with visual designers and is meant to be used in user interface contexts; it raises events on the user interface thread. The <xref:System.Timers.Timer?displayProperty=nameWithType> class derives from <xref:System.ComponentModel.Component>, so it can be used with visual designers; it also raises events, but it raises them on a <xref:System.Threading.ThreadPool> thread. The <xref:System.Threading.Timer?displayProperty=nameWithType> class makes callbacks on a <xref:System.Threading.ThreadPool> thread and does not use the event model at all. It also provides a state object to the callback method, which the other timers do not. It is extremely lightweight. | ||
|
|
||
| The following code example starts a timer that starts after one second (1000 milliseconds) and ticks every second until you press the **Enter** key. The variable containing the reference to the timer is a class-level field, to ensure that the timer is not subject to garbage collection while it is still running. For more information on aggressive garbage collection, see <xref:System.GC.KeepAlive%2A>. | ||
|
|
||
| [!code-cpp[System.Threading.Timer#2](../../../samples/snippets/cpp/VS_Snippets_CLR_System/system.Threading.Timer/CPP/source2.cpp#2)] | ||
| [!code-csharp[System.Threading.Timer#2](../../../samples/snippets/csharp/VS_Snippets_CLR_System/system.Threading.Timer/CS/source2.cs#2)] | ||
| [!code-vb[System.Threading.Timer#2](../../../samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Threading.Timer/VB/source2.vb#2)] | ||
| > Some .NET implementations may include additional timers: | ||
| > | ||
| > - <xref:System.Windows.Forms.Timer?displayProperty=nameWithType>: a Windows Forms component that fires an event at regular intervals. The component has no user interface and is designed for use in a single-threaded environment. | ||
| > - <xref:System.Web.UI.Timer?displayProperty=nameWithType>: an ASP.NET component that performs asynchronous or synchronous web page postbacks at a regular interval. | ||
| > - <xref:System.Windows.Threading.DispatcherTimer?displayProperty=nameWithType>: a timer that is integrated into the <xref:System.Windows.Threading.Dispatcher> queue which is processed at a specified interval of time and at a specified priority. | ||
|
|
||
| ## The System.Threading.Timer class | ||
|
|
||
| The <xref:System.Threading.Timer?displayProperty=nameWithType> class enables you to continuously call a delegate at specified time intervals. You also can use this class to schedule a single call to a delegate in a specified time interval. The delegate is executed on a <xref:System.Threading.ThreadPool> thread. | ||
|
|
||
| When you create a <xref:System.Threading.Timer?displayProperty=nameWithType> object, you specify a <xref:System.Threading.TimerCallback> delegate that defines the callback method, an optional state object that is passed to the callback, the amount of time to delay before the first invocation of the callback, and the time interval between callback invocations. To cancel a pending timer, call the <xref:System.Threading.Timer.Dispose%2A?displayProperty=nameWithType> method. | ||
|
|
||
| The following example creates a timer that calls the provided delegate for the first time after one second (1000 milliseconds) and then calls it every two seconds. The state object in the example is used to count how many times the delegate is called. The timer is stopped when the delegate has been called at least 10 times. | ||
|
|
||
| [!code-cpp[System.Threading.Timer#2](../../../samples/snippets/cpp/VS_Snippets_CLR_System/system.Threading.Timer/CPP/source2.cpp#2)] | ||
| [!code-csharp[System.Threading.Timer#2](../../../samples/snippets/csharp/VS_Snippets_CLR_System/system.Threading.Timer/CS/source2.cs#2)] | ||
| [!code-vb[System.Threading.Timer#2](../../../samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Threading.Timer/VB/source2.vb#2)] | ||
|
|
||
| For more information and examples, see <xref:System.Threading.Timer?displayProperty=nameWithType>. | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add another level-2 head here: |
||
| ## The System.Timers.Timer class | ||
|
|
||
| Another timer that can be used in a multithreaded environment is <xref:System.Timers.Timer?displayProperty=nameWithType> that by default raises an event on a <xref:System.Threading.ThreadPool> thread. | ||
|
|
||
| When you create a <xref:System.Timers.Timer?displayProperty=nameWithType> object, you may specify the time interval in which to raise an <xref:System.Timers.Timer.Elapsed> event. Use the <xref:System.Timers.Timer.Enabled%2A> property to indicate if a timer should raise an <xref:System.Timers.Timer.Elapsed> event. If you need an <xref:System.Timers.Timer.Elapsed> event to be raised only once after the specified interval has elapsed, set the <xref:System.Timers.Timer.AutoReset%2A> to `false`. The default value of the <xref:System.Timers.Timer.AutoReset%2A> property is `true`, which means that an <xref:System.Timers.Timer.Elapsed> event is raised regularly at the interval defined by the <xref:System.Timers.Timer.Interval%2A> property. | ||
|
|
||
| For more information and examples, see <xref:System.Timers.Timer?displayProperty=nameWithType>. | ||
|
|
||
| ## See Also | ||
| <xref:System.Threading.Timer> | ||
| [Threading Objects and Features](../../../docs/standard/threading/threading-objects-and-features.md) | ||
| ## See also | ||
|
|
||
| <xref:System.Threading.Timer?displayProperty=nameWithType> | ||
| <xref:System.Timers.Timer?displayProperty=nameWithType> | ||
| [Threading Objects and Features](threading-objects-and-features.md) | ||
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can list yourself in the author field here. (thought not in the ms.author field).