Button click from timer

I have a timer set up as a customer method that I invoke in the Load() of my page. When the timer fires, it currently executes the Load() event again. I don't want to keep executing the load event. I set up a button that has only the code I want to execute, but can't seem to get the click event of the button to fire from within the timer.

Here is my timer code:

using System;
using System.Linq;
using System.Collections.Generic;
using System.Threading.Tasks;
using Radzen;
using Radzen.Blazor;
using System.Timers;

namespace PowerMoto.Pages
{
public partial class ShowroomLogComponent
{
Timer timer;
private void InitTimer()
{
base.OnInitialized();
timer = new Timer(10000);
timer.Elapsed += (s, e) => { InvokeAsync(Timer_Elapsed); };
timer.Start();
UriHelper.LocationChanged += (s, e) => { InvokeAsync(StopTimer); };
}
private void Timer_Elapsed()
{
Load();
}

    private void StopTimer()
    {
        timer.Stop();
    }

}

}

the button is called buttonRefresh
the only option it gives me is to use ButtonRefreshClick

I tried to use it in place of Load(); like this ButtonRefreshClick(); but it does not fire...

Any help is appreciated!

It should have worked - checked the generated code to see if the name is correct and you pass the required arguments.

I get this error


looks like missing variable, but I have tried several iterations of variables in ButtonRefreshClick(). Can't seem to get it right.

As the error suggest the method lacks an argument for its single parameter. Not sure how to help you further :slight_smile:

Ok! So I should definitely read the error message completely for asking! I kept trying to put in parameters instead of a single parameter. Works perfectly now! Thank you.

Also as a side note, Looking at that code, can you tell my why the timer won't stop when I leave the page?

I know I can use dispose() and you created a partial dispose, but I am not sure how to implement it in the partial method.

I would really appreciate a little guidance here...

Your help has been invaluable!!!

I had thought that would be the answer. I adjusted my code as follows:

using System;
using System.Linq;
using System.Collections.Generic;
using System.Threading.Tasks;
using Radzen;
using Radzen.Blazor;
using System.Timers;

namespace PowerMoto.Pages
{
public partial class ShowroomLogComponent : IDisposable
{
Timer timer;
private void InitTimer()
{
base.OnInitialized();
timer = new Timer(10000);
timer.Elapsed += (s, e) => { InvokeAsync(Timer_Elapsed); };
timer.Start();
UriHelper.LocationChanged += (s, e) => { InvokeAsync(StopTimer); };
}
private void Timer_Elapsed()
{
ButtonRefreshClick(null);
}

    private void Dispose()
    {
        timer.Stop();
        timer.Dispose();
    }

}

}

When I try to compile and run I get the following error

dotnet: c:\PowerMoto\server\Pages\ShowroomLog.razor.designer.cs(28,21): error CS0111: Type 'ShowroomLogComponent' already defines a member called 'Dispose' with the same parameter types [c:\PowerMoto\server\PowerMoto.csproj]

Using a timer to update a page - #10 by enchev you can check your other thread :slight_smile: Radzen already generates Dispose and provides a partial method which you can define. Pay attention to compilation error messages and check the generated code in the *.designer.cs files.

I would also like to remind you that the Radzen support in does not cover explaining C# compilation error messages and generic .NET related questions.

I completely understand. In this particular case I was trying to make the partial method you created work but it turned out not to be the solution I needed. All I needed to do was stop the timer prior to calling the button click. Works perfectly now.

I will refrain from asking c#/.net questions in the future.

I have sometimes asked these questions because the option under resources is labeled community. There are a lot of "Community" members that have .net questions that relate to using c# and .net in Radzen. Maybe creating a seperate forum for the "Community" and one for Support.

A general purpose forum for programming questions already exists - StackOverflow. We don't plan to maintain one of our own.