Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9058677
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T14:47:05+00:00 2026-06-16T14:47:05+00:00

I have this class to run functions in some amount of time: namespace Test

  • 0

I have this class to run functions in some amount of time:

namespace Test
{
    public static class At
    {
        private static void ExecuteDelayedAction(object o)
        {
            (o as Action).Invoke();
        }

        public static void Do(Action action, TimeSpan delay, int interval = Timeout.Infinite)
        {
            new Timer(new TimerCallback(At.ExecuteDelayedAction), action, Convert.ToInt32(delay.TotalMilliseconds), interval);
        }
        public static void Do(Action action, DateTime dueTime, int interval = Timeout.Infinite)
        {
            if (dueTime >= DateTime.Now) Do(action, dueTime - DateTime.Now, interval);
        }
        public static void Do(Action action, int delay, int interval = Timeout.Infinite)
        {
            Do(action, TimeSpan.FromMilliseconds(delay), interval);
        }
    }
}

And also in my application I have two pages – Login.xaml and MainPage.xaml. Just for test purposes I’ve puth the following code in MainPage constructor:

public MainPage()
{
    InitializeComponent();
    At.Do(delegate { Debug.WriteLine("MainPage"); }, 5000);
}

My problem is as follows: if application opens MainPage first then timer works fine and delegate gets executed. If, however, application opens Login first and then navigates to MainPage then timer just fails silently. Why is it happening and how can it be fixed?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T14:47:07+00:00Added an answer on June 16, 2026 at 2:47 pm

    I suspect that the timer you are creating is being garbage-collected (GC’ed), as you are not holding a reference to it anywhere but just dropping the reference to the newly created timer.

    As navigating between pages will cause quite a bit of memory to be created, a GC may be triggered then.

    Try fixing it by keeping a reference to the timer object around. I would create a Do class instance which you could hide a private timer inside, and keep a reference to that. However if you want to keep your current static class design you could just return an object that is the timer.

        public static object Do(Action action, TimeSpan delay, int interval = Timeout.Infinite)
        {
            var t = new Timer(new TimerCallback(At.ExecuteDelayedAction), action, Convert.ToInt32(delay.TotalMilliseconds), interval);
            return t;
        }
    

    and then you need to store a reference to your timer with your page.

    public MainPage()
    {
        InitializeComponent();
        this.doObject = At.Do(delegate { Debug.WriteLine("MainPage"); }, 5000);
    }
    

    Of course, if the MainPage is GC’ed then your timer will be too, but you can fix that by changing the scope of your timer to what is appropriate for the action you want to run.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this class: public static class CsvWriter { private static StreamWriter _writer =
So i have this class that includes some custom functions named class.php I have
I have this class in which I run a for loop 10 times. This
I have a pymzml.run.Reader class from the pymzml package. This is a generator object,
I have this class: Class B { private String D; private String E; }
I have this class which i would like to map: public class Contract {
i have this class public class Image { public string url { get; set;
I have a template class template <class T> class myClass { public: /* functions
I have the following class, which performs some calculations to fill its static arrays.
I have written two jQuery functions, the simplified version of which run thus: $('button.class').not('.selected').on('click',

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.