我试图根据使用时间跨度的时间在标签中显示计数 [英] I am trying to show the count in a label based on the time using timespan

查看:78
本文介绍了我试图根据使用时间跨度的时间在标签中显示计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图根据使用时间跨度的时间显示标签中的计数

公共部分类_Default:System.Web.UI.Page
{
public int j;
public int offers = 6000;
public int i;
保护无效的Page_Load(对象发件人,EventArgs的)
{
的setInterval(()=>计算(),TimeSpan.FromSeconds(10));
}
public void calculate()
{
DateTime time = DateTime.Now;
int hour = time.Hour;
int minute = time.Minute;
int second = time.Second;
int numdays = 90;
int num = Check_Prime(分钟);
int numofperday = offers / numdays;
if(分钟%2 == 0)
{
j = j + 2;
i = numoffers - j;
}
else
{
if(num == 1)
{
j = j + 3;
i = numoffers - j;
}
其他
{
j = j + 5;
i = numoffers - j;
}
}
if(j> = 6000)
{
Thread.Sleep(TimeSpan.FromSeconds(100000));
}
Label2.Text = i.ToString();
}
public async Task SetInterval(Action action,TimeSpan timeout)
{
await Task.Delay(timeout).ConfigureAwait(false);
action();
SetInterval(动作,超时);
}


}





我的尝试:



我试图根据使用时间的时间显示标签中的计数

但标签始终为null

而我调试代码有表示标签而不是在浏览器上

解决方案
庵。
$ b中的计数值$ b你确实意识到那个代码是做什么的吗?



你要打击你的堆栈 - 可能很快,因为这是一个网站,堆栈通常比桌面小应用程序获得。

无限递归绝不是一个好主意...并且由于您的页面在页面完全加载之前从未发送到浏览器,因此在Load事件结束之前您将无法获得值。哪个不是,因为它被困在一个递归循环中,直到它吹掉堆栈或用完了会话时间。



这样的定时器在客户端更好实现在javascript中。在服务器上执行它们会占用明显有限的资源,并会降低所有用户的速度。


I am trying to show the count in a label based on the time using timespan

public partial class _Default : System.Web.UI.Page
{
    public int j;
    public int offers = 6000;
    public int i;
    protected void Page_Load(object sender, EventArgs e)
    {         
           SetInterval(() => calculate(), TimeSpan.FromSeconds(10));        
    }
    public void calculate()
    {
        DateTime time = DateTime.Now;
        int hour = time.Hour;
        int minute = time.Minute;
        int second = time.Second;
        int numdays = 90;        
        int num = Check_Prime(minute);
        int numofperday = offers / numdays;
        if (minute % 2 == 0)
        {
            j = j + 2;
            i = numoffers - j;
        }
        else
        {
            if (num == 1)
            {
                j = j + 3;
                i = numoffers - j;
            }
            else
            {
                j = j + 5;
                i = numoffers - j;
            }
        }
        if (j >=6000)
        {
            Thread.Sleep(TimeSpan.FromSeconds(100000));
        }
        Label2.Text = i.ToString();
    }
    public  async Task SetInterval(Action action, TimeSpan timeout)
    {        
        await Task.Delay(timeout).ConfigureAwait(false);        
        action();
        SetInterval(action, timeout);        
    }
   

}



What I have tried:

I am trying to show the count in a label based on the time using timespan
but label always null
while i am debugging the code there are showing the count value in the label but not on the browser

解决方案

Um.
You do realise what that code does?

You are going to blow your stack - probably very quickly, given this is a website and stacks are generally smaller than desktop apps get.
Unlimited recursion is never a good idea ... and since your page is never sent to the browser until the page is fully loaded, you won't get a value until your Load event ends. Which is doesn't, because it's trapped in a recursive loop until it blows the stack or runs out of session time.

Timers like this are better implemented at the client in javascript. Doing them at the server is hogging resources that are decidedly finite and will slow down all users.


这篇关于我试图根据使用时间跨度的时间在标签中显示计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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