本文介绍了Blazor中的轮询线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用REST API和Web界面构建Blazor应用程序.我还将在应用程序的监视部分中每秒对来自许多不同数据源的数据进行轮询.我在一个单独的类中创建了一个长时间运行的线程,该线程仅轮询我想要的数据,并且看起来运行良好.我正在使用的应用程序模板是Blazor ASP.NET Server应用程序.就像这样:

I am building a Blazor application with a REST API and Web interface. I will also have a monitoring part of the application that will poll data each second from a lot of different data sources. I have created a long running thread in a separate class that simply polls the data I want and it seems to be working fine. The application template I am using is a Blazor ASP.NET Server application. Simply like this:

 m_pollThread = new Thread(new ThreadStart(PollThread))
 {
    IsBackground = true
 };
 m_pollThread.Start();

我现在想知道的是:将这种类型的轮询线程放入Blazor应用程序本身内部,在编程模式方面是完全错误的吗?这样是否存在一些问题,这些问题稍后会适得其反(内存消耗,应用程序其余部分的性能)?我之所以问,是因为据我所知,Blazor和ASP.NET Core应用程序是常规的按需"应用程序,在请求某些内容时会唤醒,并且不会执行长时间运行的无休止的轮询任务.我不知道是否可以在IIS中运行它.

What I am wondering now is: is it completely wrong with respect to programming patterns to put this type of polling threads inside of the Blazor application itself? Is there some problems doing like this that will backfire later on (memory consumption, performance of the rest of the application)? The reason why I am asking is because as far as I know, Blazor and ASP.NET Core applications are general "on-request" and wakes up when something is requested, and not executing long-running endless polling tasks. I do not know if I could run that within IIS for instance.

推荐答案

是的.它不会立即中断,但正在引起麻烦.

Yes. It won't break immediately but it is asking for trouble.

尽管该解决方案很简单,但是ASP.NET Core允许您运行多个主机线程非常容易.

The solution is easy though, ASP.NET core lets you run multiple Host threads very easily.

新的工作程序模板可能现在是首选方法,但是您真正需要的只是

The new worker template is probably the preferred way now but all you really need is

class MyPollingService : BackgroundService { ... }

services.AddHostedService<MyPollingService>();

您的收获是与框架更好的集成以及对Start和Stop等的更多控制.

Your gain is better integration with the framework and more control over Start and Stop etc.

这篇关于Blazor中的轮询线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-24 16:24