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 6241989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:52:07+00:00 2026-05-24T11:52:07+00:00

I have a timer and I want to put timer callbacks into separate functions,

  • 0

I have a timer and I want to put timer callbacks
into separate functions, however, I get this error.

An object reference is required to access non-static field, method, or property ”…

If I declare these callbacks as delegate events and
member variables as static, it works fine. Should I leave it that way?

class MainClass
{
    private Timer _timer = null;
    private TimeSpan _millisecs;

    public static void Main (string[] args)
    {
        Application.Init();
        MainWindow win = new MainWindow();

        Label lbl = new Label();
        lbl.Text = "00:00";

        Table tbl = new Table(2, 2, true);
        tbl.Name = "tbl";

        Button btn = new Button("Start");
        tbl.Attach(lbl, 0, 2, 0, 1);
        tbl.Attach(btn, 0, 1, 1, 2);
        Button btn_stop = new Button("Stop");
        tbl.Attach(btn_stop, 1, 2, 1, 2);

        btn.Clicked += StartClick;
        btn_stop.Clicked += StopClick;

        win.Add(tbl);
        win.ShowAll();

        Application.Run ();
    }

    private void StartClick(object obj, EventArgs args)
    {
        if (_timer == null) {
            _timer = new Timer();
            _timer.Elapsed += delegate(object sender, ElapsedEventArgs e) {
                _millisecs = _millisecs.Add(new TimeSpan(0, 0, 0, 0, 50));
                lbl.Text = new DateTime(_millisecs.Ticks).ToString("ss:ff");
            };
            _timer.Interval = 50;
            _timer.Enabled = true;                  
        }

        _timer.Start();             
    }

    private void StopClick(object obj, EventArgs args)
    {
        _timer.Stop();          
    }

}
  • 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-05-24T11:52:08+00:00Added an answer on May 24, 2026 at 11:52 am

    It depends what you’re trying to do. You can either make things static, or you can create an instance:

    MainClass instance = new MainClass();
    btn.Clicked += instance.StartClick;
    btn_stop.Clicked += instance.StopClick;
    

    I assume this isn’t your real application, so it’s hard to say what you should do in your real code. Personally I’d lean towards creating an instance – static variables represent global state which is usually a bad idea (in terms of testability etc). We can’t tell whether that affects your situation or not though.

    What’s important is that you understand why you’re getting the error message and why the above change fixes it. Once you understand that, you’ll be in a better situation to make the best decisions.

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

Sidebar

Related Questions

I parse XML data and put it into an object. This takes fairly long
This is what I want to do: Have a timer with some interval In
I have a simple quiz application and I want display a nice timer /
I have created a very simple sharepoint timer job. All i want it to
I want to learn .NET and I have 2 weeks time of this. I
I have a web application and I'm attempting to put an ajax timer control
I want to create a date object, and have the time saved as I
I've created a RESTful API that supports GET/POST/PUT/DELETE requests. Now I want my API
I have a paragraph field where I want to put the seconds remaining from
I have a timer that loops through following code every second. this code reads

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.