C# Timer passes null into Elapsed event handler's source parameter

0

Anytime I try to run this c# timer, the application errors out saying "parameter cannot be null: object source" I based my implementation off of a microsoft docs page. It's worth noting that this happens when the event is synchronous as well.

private void SetTimer()
        {
            // TODO set to actual week
            WeeklyNotificationTimer = new System.Timers.Timer(30000);
            // Hook up the Elapsed event for the timer. 
            WeeklyNotificationTimer.Elapsed += async (sender, e) => await OnTimerFinishedAsync(sender, e);
            WeeklyNotificationTimer.Start();
            WeeklyNotificationTimer.AutoReset = true;
            WeeklyNotificationTimer.Enabled = true;
        }

private async Task OnTimerFinishedAsync(Object source, ElapsedEventArgs e)
        {
            await RunWeeklyNotification();
        }

WeeklyNotificationTimer is declared as

private System.Timers.Timer WeeklyNotificationTimer;

Any help is appreciated.

EDIT:

here's the full error text:

System.ArgumentNullException
  HResult=0x80004003
  Message=Value cannot be null.
Parameter name: source
  Source=System.Linq
  StackTrace:
   at System.Linq.Enumerable.Contains[TSource](IEnumerable`1 source, TSource value, IEqualityComparer`1 comparer)
   at System.Linq.Enumerable.Contains[TSource](IEnumerable`1 source, TSource value)
   at Columbus.WeeklyNotificationHandler.<RunWeeklyNotification>d__13.MoveNext() in C:\Users\(user)\Source\Repos\InternalTools\(project)\WeeklyNotificationHandler.cs:line 81
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Columbus.WeeklyNotificationHandler.<OnTimerFinishedAsync>d__12.MoveNext() in C:\Users\(user)\Source\Repos\InternalTools\(project)\WeeklyNotificationHandler.cs:line 74
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Columbus.WeeklyNotificationHandler.<<SetTimer>b__11_0>d.MoveNext() in C:\Users\(user)\Source\Repos\InternalTools\(project)\WeeklyNotificationHandler.cs:line 66

Line 81 is from RunWeeklyNotification:

if (SelfReportedUsers.Contains(conversationReference.User)) { continue; }

Line 74 is

await RunWeeklyNotification();

Line 66 is

 WeeklyNotificationTimer.Elapsed += async (sender, e) => await OnTimerFinishedAsync(sender, e);
c#
timer
asked on Stack Overflow Jun 18, 2019 by Arjemon • edited Jun 18, 2019 by Arjemon

1 Answer

0

Your Timer class looks fine.

Can you use a debugger and see which values are null on line 81?

Every elapsed time is calling RunWeeklyNotification() which a value in there is null.

answered on Stack Overflow Jun 18, 2019 by WadeTheFade

User contributions licensed under CC BY-SA 3.0