浏览器关闭时如何维护计时器会话 [英] How to maintain timer session when browsers closed

查看:70
本文介绍了浏览器关闭时如何维护计时器会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

先生,我在asp.net遇到了问题。



我每隔15分钟就产生一次随机数。它工作正常,直到我在浏览器中打开它,但我关闭浏览器并在1小时后打开它然后它再次从头开始,我不想要它。我已经在Application begin方法的Global.asax文件中编写了我的代码。并在.aspx页面上放置一个计时器



i想要执行我的程序,即使浏览器关闭或打开



我的尝试:



sir i have a problem in asp.net .

i am generating a random no in every 15 minute . it works fine till when i opened it in browser but i close the browser and open it after 1 hour then it again starts from beginning and i don't want it. i have written my code in Global.asax file of Application begin method. and a timer is placed on .aspx page

i want to execute my programme even browser closed or opened

What I have tried:

public static string lbltime2;
 public static string Label5;
 public static string Label4;
public static string lbldate;
public static string lbltime;
public static string Label1;

  protected void Application_BeginRequest(object sender, EventArgs e)
  {
      string time3 = DateTime.UtcNow.AddHours(5.5).ToString("hh:mm tt");

      if (time3 == "8:00 AM")
      {
          lbltime2 = DateTime.UtcNow.AddHours(5.5).AddMinutes(15).ToString("hh:mm tt");
          no1 = 1;
          time1 = lbltime2;
      }

      if (no1 == 0)
      {
          lbltime2 = DateTime.UtcNow.AddHours(5.5).ToString("hh:mm tt");
             // DateTime.UtcNow.AddHours(5.5).AddMinutes(4).ToString("hh:mm tt");
          no1 = 1;
          time1 = lbltime2;
      }

      else
      {
          lbltime2 = time1;
          //lblcode = code;
          //Label2 = code2;
          //Label3 = code3;
            Label4 = code4;
            Label5 = code5;

      }

      lbldate = DateTime.UtcNow.AddHours(5.5).ToString("dd-MMM-yyyy");
     lbltime = DateTime.UtcNow.AddHours(5.5).ToString("hh:mm tt");
     Label1 = DateTime.UtcNow.AddHours(5.5).ToString("hh:mm:ss tt");
     Application["lbldate"] = lbldate;
     Application["lbltime"] = Label1;

      string time4 = DateTime.UtcNow.AddHours(5.5).ToString("hh:mm tt");

      if (Convert.ToDateTime(time4) > Convert.ToDateTime(lbltime2))
      {
          lbltime2 = DateTime.UtcNow.AddHours(5.5).AddMinutes(15).ToString("hh:mm tt");
          time1 = lbltime2;
          test();

      }

      if (DateTime.UtcNow.AddHours(5.5).Hour >= 22 || DateTime.UtcNow.AddHours(5.5).Hour <= 7)
      {
          lbltime2 = "8:15 AM";
      }
      else


      {
          // lbltime2.Text = DateTime.Now.ToString("hh:mm tt");
          string time = time1;
          if (time1 == lbltime)
          {
              Application["lbldtime"] = time1;
              test();
          }
      }

      Application["lblntime"] = lbltime2;



  }

推荐答案

此代码看起来很复杂我确信您可以在服务器上的单独进程中使用Timer类实例,当您的应用需要/





system.timers.timer
[ ^ ]
This code looks very convoluted and I'm sure you can use the Timer class instance in a separate process on the server which you can call and access when your app needs to/


system.timers.timer
[^]


如果要保持代码运行并保持一致,请不要使用ASP.Net。这是错误的技术。编写一个控制台应用程序,然后使用任务计划程序安排它或编写Windows服务。
If you want to keep code running and be consistent then do not use ASP.Net. It is the wrong technology. Write a console app and then schedule it using Task Scheduler or write a Windows Service.


这篇关于浏览器关闭时如何维护计时器会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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