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.