0

I am trying to use a thread timer to call a method every 3 seconds over and over again. Here is the code i'm using at the moment

public void Main()
    {
        TextBoxMessage.Text = "Started! " + DateTime.Now.ToString();
        Timer aTimer = new Timer(new TimerCallback (SendMessage), null, 3000, 3000);

    }

    public void SendMessage(Object sender)
    {
        TextBoxMessage.Text = "Timer Tick! " + DateTime.Now.ToString();
    }

As of now, I get the "Started!" message but I never see the "Timer Tick!" message. This is obviously pretty basic code and I am just trying to figure out how to use this timer but have searched and searched and can't figure out what I am doing wrong.

Thanks in advance for the help!

3
  • The duplicate shows solutions for non-ASP.Net code which based on the code posted as the sample is the goal. If you actually looking for ASP.Net timer please clarify what you want to achieve (as it is really hard to get server side timer to send updates to web page). Commented Nov 23, 2015 at 23:54
  • While this probably IS a duplicate question, the proposed duplicate has NOTHING to do with this question. The duplicate question is for Winforms, but this question uses asp.net. Commented Nov 23, 2015 at 23:56
  • @JoelCoehoorn has imo the correct awnser you should try implementing ajax or similar to achieve a timer on the page. Commented Nov 24, 2015 at 0:02

2 Answers 2

3

You have a fundamental misunderstanding about how ASP.Net Webforms work.

Your web forms page is still entirely about processing HTTP requests in order to produce HTTP responses. Whenever someone views your page, an HTTP request goes to your web server. The server then creates a new instance of your Page class in order to process this request into an HTTP response. The Page class works through the ASP.Net Page Life Cycle, sends the HTTP response to the web browser, and then ASP.Net destroys the instance of your Page class to free up resources for next HTTP request. Further interactions with your Page, even postbacks to the same page by the same user, are handled by entirely new instances of the class.

Let's now apply this knowledge to your question. You want a timer running on the web server that sets a property in the Page class. It should be clear now that at the time you want the timer's elapsed code to fire, the Page object you're working with no longer exists. At best, you're causing a null reference exception on the server for trying to use an object that's not there. Or maybe the timer will prevent ASP.Net from reclaiming the old page class (eventually crashing your server when it runs out of resources). Another options is the running timer prevents the Page Life Cycle from finishing, so no response is ever sent to the user and page or browser times out. But what actually happens is that ASP.Net will destory your timer when it reclaims the page, so it won't ever even tick.

What you need to do instead is write this code in javascript to run on the client.

If you really want this to run on the server, there are options available, but they're all a LOT more complicated. One option that's relatively easy and has some traction in the ASP.Net world is SignalR.

Sign up to request clarification or add additional context in comments.

3 Comments

I believe I am understanding what you have said but has me wondering how to achieve my end goal now. Basically, I am trying to have a simple web form that updates relatively periodically with values that I am getting from an OPC server (this will be a simple HMI that can be viewed to see machine status, at the moment). So I just need a simple textbox that updates when its value without refreshing the page constantly.
The easiest way is likely to use javascript
See previous edit. Accidentally sent response without full details.
0

You just init the Timer object. Next step is call the method Start() to start the thread.

aTimer.Start()

Hope this help :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.