Skip to content

Conversation

@pkulikov
Copy link
Contributor

@pkulikov pkulikov commented Jul 2, 2018

This PR cleans both the C# and VB Guides from the duplicate thread timer topics:
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/threading/thread-timers
https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/concepts/threading/thread-timers

Also updates the related topic in the .NET Guide (with new code snippets by dotnet/samples#156):
https://docs.microsoft.com/en-us/dotnet/standard/threading/timers

That topic is simplified and only mentions two timers: System.Threading.Timer and System.Timers.Timer that are ones recommended for the use in a multithreaded environment.

Contributes to #6180

Copy link
Contributor

@rpetrusha rpetrusha left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks really great, @pkulikov! We really appreciate your doing this work. I've left a number of comments and suggestions.

- "timers, about timers"
ms.assetid: 7091500d-be18-499b-a942-95366ce185e5
author: "rpetrusha"
ms.author: "ronpet"
Copy link
Contributor

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

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.
Copy link
Contributor

Choose a reason for hiding this comment

The 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:
".NET Standard 2.0 includes two timers:

  • <xref:System.Threading.Timer?displayProperty=fullName>, which executes a single callback method on a thread pool thread at regular intervals.
  • <xref:System.Timers.Timer?displayProperty=fullName>, which fires an event and executes the code in one or more event sinks at regular intervals.

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

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the introduction added back, I'd add a header here:
## The System.Threading.Timer class

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

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 the single call to a delegate in a specified time interval. The delegate is executed each time on a <xref:System.Threading.ThreadPool> thread.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the single call --> a single call
executed each time --> executed

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The first deleted paragraph is still important, since it provides background for using the control that doesn't have to be gleaned from the example. I'd include a modified version:

You create a <xref:System.Threading.Timer?displayProperty=nameWithType> object by passing one of its constructors a <xref:System.Threading.TimerCallback> delegate that defines the callback method, an optional state object that is passed to the callback, an initial raise time, and a time representing the period between callback invocations. To cancel a pending timer, call the <xref:System.Threading.Timer.Dispose%2A?displayProperty=nameWithType> method.


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 the single call to a delegate in a specified time interval. The delegate is executed each time on a <xref:System.Threading.ThreadPool> thread.

The following example creates a timer that starts after one second (1000 milliseconds) and calls the provided delegate every two seconds. When you create a timer, you can provide the object that will be passed to every delegate call. In the example, such an object 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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the object that will be passed --> an object that is passed

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

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add another level-2 head here:
## The System.Timers.Timer class

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.

> [!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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Though not as well done here as it should have been, the note was intended to differentiate between timer types in the .NET Framework, which was an enormous source of confusion and complaint. I think it is still useful to add a note before the first level-2 head:

> [!NOTE]
> Some .NET implementations may include additional `Timer` classes. See the <xref:System.Threading.Timer?displayProperty=nameWithType> class for their differences and usage scenarios.

@rpetrusha
Copy link
Contributor

Closing and reopening to begin new build.

@rpetrusha rpetrusha closed this Jul 3, 2018
@rpetrusha rpetrusha reopened this Jul 3, 2018
@pkulikov
Copy link
Contributor Author

pkulikov commented Jul 3, 2018

@rpetrusha I liked your feedback a lot; it helped to make the topic much better. Thanks! As I've added a lot of new information to the topic, please review it again.

@rpetrusha
Copy link
Contributor

Thanks for making the corrections, @pkulikov. I'll review your PR sometime tomorrow.

Copy link
Contributor

@rpetrusha rpetrusha left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great, @pkulikov, so I'm going to merge it now. Thank you so much for doing this; you've not only eliminated triplication in the documentation, but you've made the single version of the topic much better!

@rpetrusha rpetrusha merged commit 5a5fae3 into dotnet:master Jul 5, 2018
@pkulikov pkulikov deleted the thread-timers-update branch July 5, 2018 16:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants