Q: Set timer in ASP.net mvc website In payment window When client clicks on payment button I want to start a timer. And if the payment processing takes more than let's say 5 second I want to redirect to a page. Below is what I could think of.
 [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult SavePayment(PaymentFormMV data)
        {
            if (Session["startTime"] == null)
            {
                Session["startTime"] = DateTime.Now;
            }

            var ticksRemaining = DateTime.Now - (DateTime)Session["startTime"];
           int x = int.Parse(ticksRemaining.ToString()); // Format Unhandled exception
            if(x == 5)
            {
                return RedirectToAction("Index", "Home");
            }
            // Payment Logic Here to 3rd Party API
             return View("PaymentConfirmation", returnData);
}

But it's not working as I expect when I calculate ticksRemaining. Do I need some theading or something here. I am new to development please guide me. I just want see the time duration between button click and current time. If it crosses 5 second I want to redirect the user to a new page.  

A: It is much more reasonable to implement this on client side. You can use JavaScript. For example this might help you: 
Page Redirect after X seconds wait using JavaScript
JQuery Redirect to URL after specified time

A: TimeSpan ticksRemaining = DateTime.Now - (DateTime)Session["startTime"];
int x = ticksRemaining.TotalSeconds;


A: in the controller just use some thing like bellow
public ActionResult YourcurrentAction()
{
   TempData["msg"] = "Your desire message here";
   return RedirectToAction("Action_Which_You_Gonna_Redirect_To", "YourControlName");
}

now in redirected action use code bellow
<center> <h2> @TempData["msg"].ToString() </h2> </center>
 @TempData.Remove("msg")

    <script type="text/javascript">
        window.setTimeout(function () {
            window.location.href = "Your_Desire_Link_To_Redirect";
        }, 5000);
    </script>

If you need just client side use my code second part . other wise use both but the way you need.
by the way Remember that if you are moving between diffrent actions you must use TempData to have stored values like message in our example. and remember to remove it for freeing memory after usage. otherwise Use ViewBag.
Wish it help you, Heydar;
