0

I just created a simple clock alarm using datepicker by 'dynDateTime' on ASP.net. the alarm is working well, however, I want the alarm to be still running even though I have closed the browser. Is there any way to achieve this/ any idea on what kind of integration needed?

Here is the running code for the alarm clock

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Media;
using System.Threading;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Windows;

public partial class Default : System.Web.UI.Page
{
    System.Timers.Timer timer;
    private DateTime dob;


    protected void Page_Load(object sender, EventArgs e)
    {
        timer = new System.Timers.Timer();
        timer.Interval = 1000;
        timer.Elapsed += Timer_Elapsed;

    }

    private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
        DateTime currentTime = DateTime.Now;

        if (currentTime.Year == dob.Year && currentTime.Month == dob.Month && currentTime.Day == dob.Day && currentTime.Hour == dob.Hour && currentTime.Minute == dob.Minute && currentTime.Second == dob.Second)
        {
            timer.Stop();


            try
            {
                SoundPlayer player = new SoundPlayer();
                player.SoundLocation = @"C:\Users\mfahmi\Desktop\alarm.wav";
                player.PlayLooping();
            }
            catch (Exception ex)
            {
                Label2.Text = ex.Message;
                Label2.Visible = true;
            }

        }

    }

    protected void btnSave_Click(object sender, EventArgs e)
    {

        dob = DateTime.Parse(Request.Form[TextBox1.UniqueID]);
        Label1.Text = dob.ToString();
        timer.Start();
    }

}

and below is the code for design page

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CS.aspx.cs" Inherits="Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<script src="Scripts/jquery.dynDateTime.min.js" type="text/javascript"></script>
<script src="Scripts/calendar-en.min.js" type="text/javascript"></script>
<link href="Styles/calendar-blue.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
    $(document).ready(function () {
        $("#<%=TextBox1.ClientID %>").dynDateTime({
            showsTime: true,
            ifFormat: "%Y/%m/%d %H:%M",
            daFormat: "%l;%M %p, %e %m,  %Y",
            align: "BR",
            electric: false,
            singleClick: false,
            displayArea: ".siblings('.dtcDisplayArea')",
            button: ".next()"
        });
    });
</script>
</head>
<body>
    <form id="form1" runat="server">
<asp:TextBox ID="TextBox1" runat="server" ReadOnly = "true" OnTextChanged="TextBox1_TextChanged"></asp:TextBox>
        <img src="calender.png" />
<asp:Button ID="btnSave" runat="server" Text="Save" onclick="btnSave_Click" />
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
    </form>
</body>
</html>

*the thing is, i'm trying to build a scheduler for my asp.net web application where user set a schedule to run a task on the datepicker.

fahmijaafar
  • 175
  • 5
  • 14
  • Ignoring the very strange idea to trigger sound on server (which for example is sitting in a datacenter in the middle of US), this question is essentially "how to have long running task in ASP.Net" (even if your "task" is just waiting a day) - that has well known "no, you don't do that and use external scheduled tasks instead" (see duplicate). If that is not what you are asking - [edit] this question or maybe ask new one. – Alexei Levenkov Jan 30 '20 at 01:13
  • Note that for actual client side of alarm clock implemented on the server you would also need some sort of *push notifications*. I'd suggest polling from client side but that approach would not need any background tasks as one can simply compare current time with alarm time on each poling request and return true if it is time for alarm. – Alexei Levenkov Jan 30 '20 at 01:16
  • Don't run a schedule in a web app, it's not made for it. Make it a service, use quart.net, a scheduled task calling a console application, whatever. – David Libido Jan 30 '20 at 01:18
  • thank you for the suggestions. I've decided to use scheduled task by calling console and it seems to solve the issue :D – fahmijaafar Jan 30 '20 at 08:20

0 Answers0