C#计数器最多可计算一个目标数 [英] C# counter to count up to a target number

查看:59
本文介绍了C#计数器最多可计算一个目标数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出是否有人知道已经存在的c#插件,它将以指定的速度增加到目标数量.

我希望能够指定:

  • 起始号码
  • 结束号码
  • 从头到尾需要花费的时间.
  • 一个自定义的回调函数,可以在计数器为完成.

我发现这些插件一个用于javascript,一个用于asp.net:

https://inorganik.github.io/countUp.js/

https://www.nuget.org/packages/Wisej-2-CountUp/

我正在使用ASP.NET Core和blazor编写应用程序,我需要一些适合blazor的东西,因为它是一项新技术,关于如何实现javascript插件的信息也很少

如果他们有如何在asp.net核心中实现countup.js的任何示例他们会对我有很大帮助

解决方案

您可以创建一个可以在许多情况下为您服务的计时器服务:

创建服务类:

 公共类BlazorTimer{私人计时器_timer;内部无效SetTimer(双间隔){_timer = new Timer(interval);_timer.Elapsed + = NotifyTimerElapsed;_timer.Enabled = true;_timer.Start();}私有无效NotifyTimerElapsed(对象发送者,ElapsedEventArgs e){OnElapsed?.Invoke();}公共事件Action OnElapsed;} 

在Program.Main方法中,将服务以瞬时方式添加到DI容器中:

  builder.Services.AddTransient(config =>{var blazorTimer = new BlazorTimer();blazorTimer.SetTimer(1000);返回blazorTimer;}); 

用法

  @page"/"@实现IDisposable@inject BlazorTimer计时器@ count.ToString()@代码{private int count = 0;受保护的重写void OnInitialized(){Timer.OnElapsed + = NotifyTimerElapsed;base.OnInitialized();}私有void NotifyTimerElapsed(){//注意:WebAssembly Apps当前仅支持一个线程,该线程//这就是为什么您不必打电话的原因//InvokeAsync方法中的StateHasChanged方法.但它//是考虑到未来的变化(例如,//可以在多个线程中运行WebAssembly Apps.InvokeAsync(()=> {count ++; StateHasChanged();});}公共无效Dispose(){Timer.OnElapsed-= NotifyTimerElapsed;} 

}

I'm trying to find out if anyone knows about an already existing c# plugin that will count up to a target number at a specified speed.

I'd like to be able to specify:

  • The start number
  • The end number
  • The amount of time it should take to get from start to end.
  • A custom callback function that can execute when a counter is finished.

I found these plugins one for javascript and one for asp.net:

https://inorganik.github.io/countUp.js/

https://www.nuget.org/packages/Wisej-2-CountUp/

I am making an application with asp.net core and blazor, I need something that fits blazor since it is a new technology and there is not much information on how to implement javascript plugins

if they had any example of how to implement countup.js in asp.net core they would help me a lot

解决方案

You can create a timer service that can serve you on many occasions:

Create the service class:

public class BlazorTimer
    {
        private Timer _timer;

        internal void SetTimer(double interval)
        {
            _timer = new Timer(interval);
            _timer.Elapsed += NotifyTimerElapsed;
            _timer.Enabled = true;
            _timer.Start();
        }

        private void NotifyTimerElapsed(object sender, ElapsedEventArgs e)
        {
            OnElapsed?.Invoke();
        }

        public event Action OnElapsed;
    }

Add the service to the DI container, in the Program.Main method, as transient:

builder.Services.AddTransient(config =>
        {
            var blazorTimer = new BlazorTimer();
            blazorTimer.SetTimer(1000);
            return blazorTimer;
        });

Usage

@page "/"

@implements IDisposable
@inject BlazorTimer Timer

@count.ToString()


@code{
private int count = 0;

protected override void OnInitialized()
{
    Timer.OnElapsed += NotifyTimerElapsed;

    base.OnInitialized();
}

private void NotifyTimerElapsed()
{
    // Note: WebAssembly Apps are currently supporting a single thread, which 
    // is why you don't have to call
    // the StateHasChanged method from within the InvokeAsync method. But it 
    // is a good practice to do so in consideration of future changes, such as 
    // ability to run WebAssembly Apps in more than one thread.
    InvokeAsync(() => { count++; StateHasChanged(); });
}

public void Dispose()
{
    Timer.OnElapsed -= NotifyTimerElapsed;
}

}

这篇关于C#计数器最多可计算一个目标数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆