ASP.NET Core 3.1-如何在服务中进行函数调用以触发页面上的操作

问题描述

我是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 (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...