问题描述
我是ASP.NET Core 3.1的新手,这个问题有点笼统,因此请您提前道歉。我找不到任何可以满足我需求的示例/教程。因此,我设置了这个简单的服务类,我想创建一个页面,该页面在每次在服务中调用GenerateMsg()时都会触发一个“事件”,然后从那里我想从_latestMsg中提取文本并显示它。这里的标准方法是什么?
{
public class MsgGenerator : IHostedService
{
private Timer? _timer;
private int _counter;
public string _latestMsg;
public Task StartAsync(CancellationToken cancellationToken)
{
ScheduleNextMsg();
return Task.CompletedTask;
}
public Task StopAsync(CancellationToken cancellationToken)
{
if (_timer == null)
{
return Task.CompletedTask;
}
return _timer.DisposeAsync().AsTask();
}
private void ScheduleNextMsg()
{
var nextTime = TimeSpan.FromMilliseconds(5000);
_timer = new Timer((state) => { GenerateMsg(); },null,nextTime,TimeSpan.Zero);
}
private void GenerateMsg()
{
_counter++;
_latestMsg = "This is message no. " + _counter.ToString();
System.Diagnostics.Debug.WriteLine(_latestMsg);
ScheduleNextMsg();
}
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)