-1

I am new to mvc4. I have to implement something like Timer OnTick event in asp.net forms. i.e. I want a method to check particular database entry after every 10 seconds and if entry is found, send it to view or something else.

Please give me some idea to implement this.

tereško
  • 58,060
  • 25
  • 98
  • 150
Vivek Mishra
  • 1,772
  • 1
  • 17
  • 37
  • It's quite a broad question. Have you got a connection to the DB yet? How? Any ORM? Is this async or sync? How do you want this polling to start? Have you considered other approaches? (i.e. are you in fact looking for a push technology?) Is the client involved in any way? –  Oct 23 '15 at 12:42
  • yes all at server side. I am using EF – Vivek Mishra Oct 23 '15 at 12:42
  • 1
    Please post your current method. – anmarti Oct 23 '15 at 12:43
  • I have db connection and all. I just want to know about the implementation part of that function – Vivek Mishra Oct 23 '15 at 12:43
  • Yes but that will depend on the context in which you're writing it. Which we can't see. It may sound like I'm being pedantic - but we could spend time devising a solution that could be written off as irrelevant because all along you've catered for a different approach. –  Oct 23 '15 at 12:45
  • you need to use `jquery ajax polling` http://stackoverflow.com/questions/6835835/jquery-simple-polling-example – Kishore Sahasranaman Oct 23 '15 at 12:45
  • @JayMEE thats what I am asking about the approach? My question is how to implement this functionality in MVC 4 – Vivek Mishra Oct 23 '15 at 12:46
  • @VIVEK post your code so far. –  Oct 23 '15 at 12:47
  • try this .. http://stackoverflow.com/a/5939166/2074346 – Kishore Sahasranaman Oct 23 '15 at 12:56

2 Answers2

2

Use System.Timers:

Timer timer; = new Timer();
timer.Interval = 10000; // 10 seconds
timer.Elapsed += new ElapsedEventHandler(Timer_Elapsed);
timer.Enabled = true;
timer.Start();


void Timer_Elapsed(object sender, ElapsedEventArgs e)
{
    timer.Stop();

    // DO YOUR WORK HERE

    timer.Start();
}
anmarti
  • 5,045
  • 10
  • 55
  • 96
2

It seems that you need to use SignalR.

SignalR can be used to add any sort of "real-time" web functionality to your ASP.NET application. While chat is often used as an example, you can do a whole lot more. Any time a user refreshes a web page to see new data, or the page implements Ajax long polling to retrieve new data, is candidate for using SignalR.

billybob
  • 2,859
  • 6
  • 35
  • 55