-1

I am trying to repeat seleced random record from the table every few seconds with timer, but it doesnt work.

    protected void Page_Load(object sender, EventArgs e)
    { 


        var timer = new System.Timers.Timer(3000); // every 3 second
        timer.Elapsed += HandleTimerElapsed;

    }
    public void HandleTimerElapsed(object sender, ElapsedEventArgs e)
    { OleDbCommand cmd = new OleDbCommand("SELECT TOP 1 userdaten.image FROM userdaten ORDER BY Rnd(ID)", con);
        con.Open();
        OleDbDataReader dr = cmd.ExecuteReader();

        if (dr.HasRows)
        {
            while (dr.Read()) { PictureTrainLabel.Text = "~/Image/" + dr["image"].ToString(); }

            PictureTrain.ImageUrl = PictureTrainLabel.Text;



        }
        con.Close();
    }

Choosing a random row works, but executing its code with the repeating methode it doesnt. Can I repeat a function without using timer in C#.

Opijkkk
  • 1
  • 6
  • 1
    Just querying DB every second seem to be … *not very useful*. Can you please [edit] post to clarify what you hope to achieve with that? – Alexei Levenkov Feb 10 '20 at 22:52
  • If this is a WinForms app you should use [System.Windows.Forms.Timer](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.timer?view=netframework-4.8) instead of `System.Timers.Timer`. The `Windows.Forms` version is optimized for UI. – Jason Wadsworth Feb 10 '20 at 23:16
  • 1
    You might need to start the timer – devNull Feb 10 '20 at 23:22
  • I dont use window Forms. It is running in browser – Opijkkk Feb 10 '20 at 23:27
  • 1
    Web application doesn't work the way you think they work. The page object is Initialized and destroyed every time a request is sent to server. That means Page_Load is executed Everytime a request is sent to the server. So timer created in there will be destroyed when the page completes it's execution. You can not actually get the timer work in web application the same way as in windows application. To serve your purpose you need to have JavaScript code written which will call Page Method repeatedly after some duration. The page method should return the data and JS should display the data. – Chetan Feb 11 '20 at 01:03
  • https://stackoverflow.com/questions/7089760/how-to-call-an-asp-net-c-sharp-method-using-javascript – Chetan Feb 11 '20 at 01:04
  • https://www.codeproject.com/Questions/478135/Callingpluspageplusmethodsplusfromplusjavascript – Chetan Feb 11 '20 at 01:04
  • https://www.c-sharpcorner.com/UploadFile/abhikumarvatsa/calling-an-Asp-Net-C-Sharp-method-web-method-using-javascript/ – Chetan Feb 11 '20 at 01:04

3 Answers3

0

There is a better approach to add a timer to a page to update a control in specific time intervals. Please check This post discussion

sm101
  • 562
  • 3
  • 10
  • 28
-1

We use Hangfire (https://www.hangfire.io/) to handle background jobs, it works with ASP.net, it is simple to setup and to use, and it is a widely used framework.

This is a nice article that compares it to webjobs. The article is focused on Azure, but it is the same for other environments that I have used it in: https://rimdev.io/considering-hangfire-in-windows-azure-instead-of-webjobs/

-1

Can you try out like below

private void Form1_Load(object sender, EventArgs e)
{
    Timer timer = new Timer();
    timer.Interval = (10 * 1000); //10 seconds
    timer.Tick += new EventHandler(HandleTimerElapsed);
    timer.Start();
}

private void HandleTimerElapsed(object sender, EventArgs e)
{
//add code
}
sm101
  • 562
  • 3
  • 10
  • 28
  • @Opijkkk Refer https://www.dotnetperls.com/timer for a whole range of timer usages even in ASP.NET pages – sm101 Feb 11 '20 at 08:51