Timer object handler's method is not executed

Giacomo

New member
Joined
Sep 14, 2022
Messages
4
Programming Experience
Beginner
I need to attribute the value false to the boolean property _show after 3 seconds
But the value doesn't change
Timer object:
 public void hiderefreqConfirmPopUp(Object source, ElapsedEventArgs e)//this is the handler of te Timer method
    {
        //eventhandler
        _show = false;
    }

    private Timer _timer;

    private void StartTimer()
    {
        _timer = new Timer();
        _timer.Interval = 3000;
        _timer.Elapsed += hiderefreqConfirmPopUp;
    }
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
5,698
Location
Chesapeake, VA
Programming Experience
10+
You may have a method named StartTimer(), but where did you call this method? And more importantly where did you call _timer.Start()?

 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
4,643
Location
Sydney, Australia
Programming Experience
10+
It seems that you have a Windows Forms project but you are not using a Windows Forms Timer. The System.Windows.Forms.Timer has a Tick event, while the System.Timers.Timer has an Elapsed event. That may or may not be the right thing to do but you should understand exactly what the implications are.

The WinForms Timer raises events on the UI thread. The system Timer can raise events on the UI thread but raises them on a thread pool thread by default. Which you should want depends on how long the event handler takes to execute and whether it access the UI.

In most cases, a WinForms application should use a WinForms Timer. You should add that Timer to the form in the designer and then double-click it to generate a Tick event handler, just as you would add a control and generate a handler for its default event. You can set the Interval property in the designer and then simply call Start and Stop in code, or set the Enabled property based on some bool, e.g. the Checked property of a CheckBox. The way you're doing it looks dodgy.
 

Giacomo

New member
Joined
Sep 14, 2022
Messages
4
Programming Experience
Beginner
You may have a method named StartTimer(), but where did you call this method? And more importantly where did you call _timer.Start()?

I'm tryin to show you, but today I have th Compiler Error CS0246, because the namespace System could be found, and I didn't touched tha assembly... I can't fix it, maybe you could help me, thanks!
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
5,698
Location
Chesapeake, VA
Programming Experience
10+
Check your source control. Something must have changed. Even a random gamma ray that could potentially make into your computer's memory won't be enough to completely remove a dependency.

Anyway, if the assembly is missing, just add it.
 

Giacomo

New member
Joined
Sep 14, 2022
Messages
4
Programming Experience
Beginner
Check your source control. Something must have changed. Even a random gamma ray that could potentially make into your computer's memory won't be enough to completely remove a dependency.

Anyway, if the assembly is missing, just add it.
Inexplicably he fixed himself 😳
This is the code, I call the method at line n. 54 as you can see
Thanks!

Timer object:
�@using DB_Pro;
@using System.Timers;
@inject Data.DataService DS;
<li>
    @prospectRequested.BusinessName
    <button class="btn btn-primary" @onclick="ShowPopup">Request</button>
</li>
<li>
    @prospectRequested.PerformanceActivity.Description
</li>
@if (_show)
{
    <h3>Chiedi a @receivingUser.Name di essere referenziato</h3>

    <textarea rows="4" cols="50" @bind="@subject" bind:event="oninput">
    Ciao @receivingUser.Name vorrei essere referenziato a @prospectRequested.BusinessName
    </textarea>
    <br>

    <textarea @bind="@referenceReason" bind:event="oninput"
          placeholder="Inserisci qui le motivazione per cui @prospectRequested.BusinessName dovrebbe essere interessata al tuo prodotto/servizio"></textarea>

    <button class="btn btn-primary" @onclick="HidePopUp">Submit </button>

}
    @if (_showconfirm)
{
    <h6>Richiesta di referenza inviata con successo!</h6>
}




@code {

    public bool _showconfirm { get; set; }
    [Parameter]
    public bool _show { get; set; }
    [Parameter]
    public User receivingUser { get; set; }
    [Parameter]
    public User askingUser { get; set; }
    [Parameter]
    public Prospect prospectRequested { get; set; }
    public void HidePopUp()//here I call the method "StarTimer"
    {
        List<User> usersToSave = new List<User>();
        usersToSave.Add(receivingUser);
        usersToSave.Add(askingUser);

        var newReq = CreateRefRequest(askingUser, receivingUser, prospectRequested, subject, referenceReason);
        SaveRefRequest(usersToSave);
        ShowRefReqConfirmedMsg();
        StartTimer();

    }
    public void ShowPopup()
    {
        _show = true;
        subject = $"Ciao {receivingUser.Name} vorrei essere referenziato a {prospectRequested.BusinessName}";
    }
    public void HideRefReq()
    {
        _show = false;
    }

    public void ShowRefReqConfirmedMsg()
    {
        _showconfirm = true;
    }
    string subject = "";
    string referenceReason = "";
    public Reference_Request CreateRefRequest(User applicant, User receiver, Prospect prospect, String subject, String referenceReason)
    {


        Reference_Request refRequest = new Reference_Request();
        refRequest.Applicant = applicant;
        refRequest.Receiver = receiver;
        refRequest.Prospect = prospect;
        refRequest.Subject = subject;
        refRequest.ReferenceReason = referenceReason;
        return refRequest;
    }
    public void SaveRefRequest(List<User> usersToSave)
    {
        DS.SaveToDisk(usersToSave);
    }

    public void hiderefreqConfirmPopUp(Object source, ElapsedEventArgs e)
    {
        //eventhandler
        _show = false;
    }

    private Timer _timer;

    private void StartTimer()
    {
        _timer = new Timer();
        _timer.Interval = 3000;
        _timer.Elapsed += hiderefreqConfirmPopUp;
    }
 
Last edited:

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
5,698
Location
Chesapeake, VA
Programming Experience
10+
Okay that was completely unexpected. That is not the WinForms code I was expecting to see whenever someone asked about timers.

I think you need to step back and tell us what kind of project you are working on. Is it for Razor pages? ASP.NET MVC? Server side Blazor? Web assembly Blazor? .NET MAUI?
 

Giacomo

New member
Joined
Sep 14, 2022
Messages
4
Programming Experience
Beginner
Okay that was completely unexpected. That is not the WinForms code I was expecting to see whenever someone asked about timers.

I think you need to step back and tell us what kind of project you are working on. Is it for Razor pages? ASP.NET MVC? Server side Blazor? Web assembly Blazor? .NET MAUI?
Ok sorry, this is a razor page, I'm working with Blazor
Thanks
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
5,698
Location
Chesapeake, VA
Programming Experience
10+
I did a quick and dirty Blazor Server App, and I could see that my timer event handler was being called. Unsurprisingly, any state changes I made within the timer event handler was not reflected on the UI. I wasn't surprised because as far as I was concerned, I wasn't doing anything to tell Blazor that it needed to re-render the Razor component and send the deltas to the browser. So what I did was add the following to the end of my timer event handler: InvokeAsync(StateHasChanged);. Then my UI stated getting updated.

Index.razor:
@page "/"
@using System.Timers;

<PageTitle>Index</PageTitle>

<h1>Hello, world!</h1>

<p>Welcome to your new app.</p>

<div>
    <button id="toggle" @onclick="btnToggle_Click">@_buttonText</button>
</div>

@if (_isShown)
{
    <SurveyPrompt Title="How is Blazor working for you?" />
}



@code
{
    bool _isShown = true;
    string _buttonText = "";
    Timer _timer = new Timer() { Interval = 2000 };

    protected override Task OnInitializedAsync()
    {
        _timer.Elapsed += (o, e) => { _timer.Stop(); Toggle(); };
        Toggle();
        return base.OnInitializedAsync();
    }

    void Toggle()
    {
        _isShown = !_isShown;
        _buttonText = _isShown ? "Hide" : "Show";
        InvokeAsync(StateHasChanged);
    }

    void btnToggle_Click()
    {
        Toggle();
        if (_isShown)
            _timer.Start();
    }
}
 
Last edited:
Top Bottom