WPF Real time delay. TPL,timer, backgroundworker

Leon NG 101 Reputation points
2023-01-05T07:24:43.933+00:00

Hi there,

I'm trying to write a function to send a CAN message in every 5ms.
At first, I use the Dispatcher Timer, but the received msg isn't in 5ms.

    unsafe public void ProfileCycle(object sender, EventArgs e)  
    {  
        VCI_CAN_OBJ frameinfo = new VCI_CAN_OBJ();  
        frameinfo.ID = System.Convert.ToUInt32("0x790", 16);  
        frameinfo.DataLen = System.Convert.ToByte(8);  
        frameinfo.RemoteFlag = 0;  
        frameinfo.ExternFlag = 0;  
        frameinfo.SendType = 0;  

        if (profile_data_counter< profile_data_number)  
        {  
            frameinfo.Data[0] = Profile.data[profile_data_counter].Data.Byte0;  
            frameinfo.Data[1] = Profile.data[profile_data_counter].Data.Byte1;  
            frameinfo.Data[2] = Profile.data[profile_data_counter].Data.Byte2;  
            frameinfo.Data[3] = Profile.data[profile_data_counter].Data.Byte3;  
            frameinfo.Data[4] = Profile.data[profile_data_counter].Data.Byte4;  
            frameinfo.Data[5] = Profile.data[profile_data_counter].Data.Byte5;  
            frameinfo.Data[6] = Profile.data[profile_data_counter].Data.Byte6;  
            frameinfo.Data[7] = Profile.data[profile_data_counter].Data.Byte7;  
            VCI_Transmit(m_devtype, m_devind, m_canind, ref frameinfo, 1);  
            profile_data_counter += 5;  
        }  
        else  
        {  
            send_profile.Stop();  
        }  
    }  

I try to use background worker and TPL by adding a delay, but it doesn't work.

Best Regards,
Leon

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,813 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,208 questions
0 comments No comments
{count} votes

Accepted answer
  1. Hui Liu-MSFT 48,601 Reputation points Microsoft Vendor
    2023-01-06T02:32:47.443+00:00

    Hi, @Leon NG . Welcome Microsoft Q&A.
    Based on your code, I cannot reproduce your problem. I made a sample code. You could try to refer to it. If there is still a problem, please show the relevant definition and code of the following code( Definitions of VCI_CAN_OBJ , Profile, methods and parameters VCI_Transmit ) or show me the complete code that can reproduce the problem for analysis.

    frameinfo.Data[7] = Profile.data[profile_data_counter].Data.Byte7;  
                  VCI_Transmit(m_devtype, m_devind, m_canind, ref frameinfo, 1);  
    

    Code:

    using System;  
    using System.Collections.Generic;  
    using System.Threading;  
    using System.Threading.Tasks;  
    using System.Windows;  
    
    public partial class MainWindow : Window  
        {  
            public MainWindow()  
            {  
                InitializeComponent();  
                DataContext = this;  
                CancellationTokenSource tokenSource = new CancellationTokenSource();  
    
    
    
      Task timerTask = RunPeriodically(sendRequest, TimeSpan.FromMilliseconds(5), tokenSource.Token);  
    }  
    public int num { get; set; } =5;  
    private void sendRequest()  
    {  
    
    num += 5;  
     MessageBox.Show(num.ToString () + DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss.fff tt"));  
    
    }  
    
    
    
    async Task RunPeriodically(Action action, TimeSpan interval, CancellationToken token)  
    {  
    
    while (true)  
    {  
        action();  
        await Task.Delay(interval, token);  
    }  
    }  
    
    }  
    

    The result:
    ![276761-14.gif][1]

    Update: When you set the Interval of the timer to 5 milliseconds, the timer will not be executed every 5 milliseconds. The timer doesn't have that high resolution.

    From the documentation:

    Timers are not guaranteed to execute exactly when the time interval occurs, but they are guaranteed to not execute before the time interval occurs. This is because DispatcherTimer operations are placed on the Dispatcher queue like other operations. When the DispatcherTimer operation executes is dependent on the other jobs in the queue and their priorities.


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our [documentation][2] 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.