Replies: 1 comment 1 reply
-
With the book printed code, there are too many curly braces. This should be with the using declaration. private static void ThreadingTimer()
{
void TimeAction(object? o) =>
Console.WriteLine($"System.Threading.Timer {DateTime.Now:T}");
using Timer t1 = new(
TimeAction,
null,
dueTime: TimeSpan.FromSeconds(2),
period: TimeSpan.FromSeconds(3));
Task.Delay(15000).Wait();
} I've updated updates.md As an alternative way - with the using statement this could be done: private static void ThreadingTimer()
{
void TimeAction(object? o) =>
Console.WriteLine($"System.Threading.Timer {DateTime.Now:T}");
using (Timer t1 = new(
TimeAction,
null,
dueTime: TimeSpan.FromSeconds(2),
period: TimeSpan.FromSeconds(3)))
{
Task.Delay(15000).Wait();
}
} Before C# 8 only the using statement was possible. See Using, using, using with C# 8 on differences. With a previous C# version, it was also not possible to use static void TimeAction(object? o) =>
Console.WriteLine($"System.Threading.Timer {DateTime.Now:T}");
using Timer t1 = new(
TimeAction,
null,
dueTime: TimeSpan.FromSeconds(2),
period: TimeSpan.FromSeconds(3));
await Task.Delay(15000); Remember, the |
Beta Was this translation helpful? Give feedback.
-
Hi,
I am not sure but I've seen some differences(or mistakes?)
Does the above code have a compiler error?
await Task.Delay(15000);
But in the book, you wrote:
Task.Delay(15000).Wait();
Beta Was this translation helpful? Give feedback.
All reactions