A timer for a service read a table in the comic book

Asked

Viewed 155 times

0

I need to get a service to read a table in the comic book every 5 or 10 seconds. I don’t see any other way to do that. When in this table a certain Flag is changed, then the service pushes a message to an App. I don’t know if this is the best way to automate my App, but it’s what’s coming into my head right now and I have no idea how to do it. The lambda that checks the table ok, I do not know if the timer or something else, how to do. How do I implement this timer? The idea would be to create a Task(async) that reads the table, but the problem is the timer. Web Api usage with REST and C#.

  • leaves the Task async running all the time... while(true) and inside it places the Task.Delay(5000); to wait the time

  • Thanks, @Rovannlinhalis, I’m gonna take this test and post the result.

  • 1

    Blz, look at what Albano said, it shouldn’t be in the api but on your server

1 answer

1

Using a timer in a web API is not a good idea. I would create another project, like windows service, and then use a timer.

Another option, and it depends on your database, would be to create a Rigger that makes the call from your web api in the act of the change you expect. For SQL Server a look here. In short:

Declare @Object as Int;
Declare @ResponseText as Varchar(8000);

Code Snippet
Exec sp_OACreate 'MSXML2.XMLHTTP', @Object OUT;
Exec sp_OAMethod @Object, 'open', NULL, 'get',
                 'http://www.webservicex.com/stockquote.asmx/GetQuote?symbol=MSFT', --Your Web Service Url (invoked)
                 'false'
Exec sp_OAMethod @Object, 'send'
Exec sp_OAMethod @Object, 'responseText', @ResponseText OUTPUT

Select @ResponseText

Exec sp_OADestroy @Object

Now, honestly, I would never, ever, under any circumstances, use a while(true). In addition to being inelegant, and having other alternatives, the web api waits for a first request to be started. Thus, it is yours while(true) will only start working after this request, the same applies to the timer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.