exceptionForHR in long polling from API, timer goes in two..

Eduardo Gomez 3,426 Reputation points
2024-02-17T14:52:03.1033333+00:00

Hello I have my JoingMeetingViewModel

    private string? elapsedTimeString;
    private readonly Stopwatch? stopwatch = new();
    private Timer? meetingTimer;
    private Timer? breakTimeTimer;
    private MeetingData? data;
    private Timer? apiRequestDataTimer;
    [RelayCommand]
    async Task EntryCompleted() {
        await VerifyAddressAsync(appService);
    }
    public async Task InitializeApiPolling() {
        StartMeetingApiPolling();
        await UpdateMeetingData();
    }
    private void StartMeetingApiPolling() {
        apiRequestDataTimer = new Timer(
            async state => await GetMeeetingData(),
            null,
            TimeSpan.Zero,
            TimeSpan.FromSeconds(30)
        );
    }
    private void StartBreakTimeTimer() {
        breakTimeTimer = new Timer(
            state => ShowAlert(new BreakTimePopUp()),
            null,
            TimeSpan.FromSeconds(30),
            Timeout.InfiniteTimeSpan);
    }
    private void StartMeetingTimer() {
        stopwatch!.Start();
        meetingTimer = new Timer(state => UpdateElapsedTime(),
            null,
            TimeSpan.Zero,
            TimeSpan.FromSeconds(1));
    }
    private void UpdateElapsedTime() {
        TimeSpan elapsed = stopwatch!.Elapsed;
        ElapsedTimeString = $"{elapsed.Hours:D2}:{elapsed.Minutes:D2}:{elapsed.Seconds:D2}";
    }
    private async Task UpdateMeetingData() {
        if(data == null) {
            data = (MeetingData?)await meetingService.GetMeetingData(RoomName, Constants.DAILY_AUTH_TOKEN);
            await GetMeeetingData();
        }
; // Trigger data processing
    }
    private async Task VerifyAddressAsync(IAppService appService) {
        if(Text is not null && Text!.Contains("demy-ia")) {
            GotoSite();
        } else {
            await appService.DisplayAlert("Error", "Please paste a valid meeting link", "OK");
        }
    }
    private async void GotoSite() {
        if(!string.IsNullOrEmpty(Text)) {
            IsWebViewVisible = true;
            if(IsWebViewVisible) {
                ToolbarVisibility = !ToolbarVisibility;
                MeetingSearchVibiility = !MeetingSearchVibiility;
                appShell.FlyoutWidth = 0;
                StartBreakTimeTimer();
                await InitializeApiPolling();
                StartMeetingTimer();
            }
        }
    }
    private async Task GetMeeetingData() {
        if(data != null) {
            var meetingData = data;
            bool isOngoing = meetingData!.data[0].ongoing;
            if(isOngoing is false) {
                await appService.DisplayAlert(Title, "you are in the lobby", "OK");
            } else if(isOngoing is true) {
                await appService.DisplayAlert(Title, "you are in the meeting", "OK");
            }
        }
    }
}


Ok so the way this is supposed to work is: When I enter a meeting, I am waiting in the lobby, therefore ongoing is false. User's image

and when I enter the meeting the ongoing changes to true

{
    "total_count": 45,
    "data": [{
        "id": "5fd28059-c9a3-4d25-af75-543189b851c1",
        "room": "Programming101",
        "start_time": 1708179804,
        "duration": 90,
        "ongoing": true,
        "max_participants": 1,
        "participants": [{
            "user_id": null,
            "participant_id": "917dacbb-504a-4e7c-afca-331b5a266f92",
            "user_name": "eduardo",
            "join_time": 1708179804,
            "duration": 90


but for some reason when my

StartMeetingApiPolling()

calls again, the value dosen change and I get an exception User's image

also the

private void StartMeetingTimer() {
    stopwatch!.Start();
    meetingTimer = new Timer(state => UpdateElapsedTime(),
        null,
        TimeSpan.Zero,
        TimeSpan.FromSeconds(1));
}


for some reason at certain point in the app, it will skip a number, for example from 21 to 23, or 30 to 32. Demo https://github.com/eduardoagr/DemyAI Video I want to start the

StartBreakTimeTimer and StartMeetingTimer, when I am inside the room


and to destroy the polling thread when I enter the meeting.

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,415 questions
{count} votes

Accepted answer
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 42,081 Reputation points Microsoft Vendor
    2024-02-21T07:28:53.1933333+00:00

    Hello,

    After testing, it was an accuracy issue in the timer that caused the time jump.

    When using TimeSpan.FromSeconds(1) for timing, the TimeSpan has an error of about 50ms.

    So, in some cases, the timer doesn't actually go full 1s, causing the number of seconds in StopWatch to not change and update 2s at a time after the next second timer is done.

    Solution:

    You could use the millisecond timer with higher precision.

    TimeSpan.FromMilliseconds(1005.0));
    

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.