Click here to Skip to main content
15,895,084 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I am developing airlines reservation app. Here, user selects flight and logs in. After login 5 min time is to be provided to him to complete his reservation, payment and confirm ticket. If his task is not completed within 5 min time, his reservation is to be cancelled. For cancelling, I need some event to be triggered. This event should not be triggered if user operation is completed within 5 min.

What is the best practices to do this in MVC 5 and C#?

What I have tried:

I have found c# timer event method to accomplish the task but I am still not sure if I should use this or not. Is there better solution available? Also, not sure on how do I cancel reservation after time interval exceeds 5 min?
Posted
Updated 3-Jun-19 20:13pm

Quote:
I have found c# timer event method to accomplish the task but I am still not sure if I should use this or not.


Use it .. considering no one else has a clue what your method is ... except that it "accomplishes the task"; which is almost 100% the point.
 
Share this answer
 
The timer is fine, I believe.
To cancel the reservation, make sure a single method is in charge to completely commit it. This way, until such method has not been called your code may cancel the reservation without having to perform backward steps.
 
Share this answer
 
Timer is actually a C# object (or type) that you can use in your C# programs and applications, such as WPF, WinForms, etc. But since you have tagged MVC 5 with this question and most probably you want to implement this feature in a web application, the best approach would be to control this from client-side.

In JavaScript, you can do this,
JavaScript
/* 
 * bookingMade to be set to true if user has made a booking so timer 
 * can be ignored and user can continue, in case timer hits. 
 */
let bookingMade = false;
let minutes = 5 * 60 * 1000; // 5 minutes

setTimeout(function() {
    if(!bookingMade) {
        // Refresh the page and capture a new session
    }
    // else is not needed, as per your case.
}, minutes); // Attach the timeout
This way, you can control the requests from the client side and reject any request that comes after 5 minutes. Of course, this is just off the top of my head right now and you can do a better job than me. But in the nutshell this is the way that you need to take, in order to control this reservation system.

See here for more information on how to control this timer, WindowOrWorkerGlobalScope.setTimeout() - Web APIs | MDN[^]

A server-side implementation of session is also a good approach, since your server will itself reject any request that comes once the session has timed out, but I have omitted that part out since sessions typically do not follow timers—each time a request is made, a new instance is created and thus will force you to create a static instance of the timer and maintain it along with the request or session information, which is quite ugly as I imagine it—and you might have to write session timeouts yourself, see here Session timeout in ASP.NET - Stack Overflow[^]. This server-side implementation can control most of the stuff that you would have to implement yourself.
 
Share this answer
 
I found this code and it worked exactly the way I wanted.

private static void OnTimedEvent(object source, ElapsedEventArgs e)
    { 
        //do something
    }

    protected void Button1_Click(object sender, EventArgs e)
    {

        if (Session["Timer"] == null)
        {
            Session["Timer"] = new System.Timers.Timer();
        }
        
        ((System.Timers.Timer)Session["Timer"]).Interval = 5000;
        ((System.Timers.Timer)Session["Timer"]).Elapsed += OnTimedEvent;
        ((System.Timers.Timer)Session["Timer"]).AutoReset = true;
        ((System.Timers.Timer)Session["Timer"]).Enabled = true; 
    }

    protected void Button3_Click(object sender, EventArgs e)
    {
        ((System.Timers.Timer)Session["Timer"]).Enabled = false;
    }
 
Share this answer
 
This is the code I found in msdn blog. This is the best thing I found for me. I will be using this::

using System;
using System.Timers;
namespace Timer
{
    public partial class Index : System.Web.UI.Page
    {
        private static System.Timers.Timer aTimer;

        protected void Page_Load(object sender, EventArgs e)
        {          

        }

        // Specify what you want to happen when the Elapsed event is raised.
        private static void OnTimedEvent(object source, ElapsedEventArgs e)
        { 

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            // Create a timer and set a two second interval.
            aTimer = new System.Timers.Timer();
            aTimer.Interval = 5000; //10 sec

            // Hook up the Elapsed event for the timer. 
            aTimer.Elapsed += OnTimedEvent;

            // Have the timer fire repeated events (true is the default)
            aTimer.AutoReset = true; //AutoReset = false raises ElapsedEventHandler only once

            // Start the timer
            aTimer.Enabled = true; 
        }

        protected void Button2_Click(object sender, EventArgs e)
        {

            for (int i=0; i<100000000; i++)
            {
                System.Threading.Thread.Sleep(1000);
            }
            //aTimer.Enabled = true;
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            aTimer.Enabled = false;
        }
    }
}
 
Share this answer
 
Comments
Afzaal Ahmad Zeeshan 23-May-19 7:00am    
He would like to get a suggestion for MVC 5, this is for Web Forms.
Codes DeCodes 23-May-19 7:09am    
this should not be an issue because aTimer is global and instance of aTimer can be accessed in different phase of execution.
Richard Deeming 24-May-19 14:40pm    
Which MSDN blog was that from? The code is awful! By using a static variable, you will have a single timer across every user of your site. One user clicking the button will reset the timer for every other user.

And don't expect to be able to update any controls on your page from the timer's event handler. Or any session state. Or anything else for that matter. By the time it fires, both the page and the current HttpContext will be long gone.
Codes DeCodes 25-May-19 1:59am    
Thanks for the information.
So what do you suggest for maintaining timer for single user (chances are multiple user can log in via different browser on same computer).
Is it ok to not use static like this?

private System.Timers.Timer aTimer;

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900