locked
Calling a web api 2 on an interval RRS feed

  • Question

  • User64294366 posted

    Not sure if this is the right place for this post but I'll give it a try. I created a web api that returns a count of rows in a sql server table. I will need to call this web api every 5 seconds.

    I am thinking about using the timer class in C# to implement the interval. My question is how to wait for the api response. Since the timer will not pause. I've been looking for an example but have not found what I need yet. I would appreciate it if someone could point me to an example. This program will be running on Windows 10 IOT and the database will be local to the device. There will never be more than 10 records in the database.

    Thanks,

    RD

    Saturday, July 8, 2017 5:53 PM

Answers

  • User475983607 posted

    Why would you need a Web API and IIS local to even do this?

    It seems to me that you could use a self-hosting WCF service, an exe,  to do this running on the local machine, Windows desktop solution with the form removed out it so that it's ust an exe with no form running local on the machine, a console application, an exe or a Windows service, an exe,  hosting a WCF service running local on the machine.

    I imagine the RDowdall is running IoT on an embedded network device and simply wants to hit the device from a remote system.  The details are not real clear but IoT kinda' indicates an embedded device.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, July 8, 2017 9:28 PM

All replies

  • User475983607 posted

    A timer fires at some frequency.  It sounds like you want to wait for the response plus 5 seconds before sending another request.

    namespace ConsoleDemo
    {
        class Program
        {
            static void Main(string[] args)
            {
                while(true)
                {
                    DoSomeWork();
    
                    DateTime now = DateTime.Now;
                    DateTime later = now.AddSeconds(5);
                    
                    while(now < later)
                    {
                        now = DateTime.Now;
                    }
                }
            }
    
            static void DoSomeWork()
            {
                Console.WriteLine("Work started:\t{0}", DateTime.Now);
                Thread.Sleep(6000);
                Console.WriteLine("Work Completed:\t{0}", DateTime.Now);
                Console.WriteLine();
            }
        }
    }

    Saturday, July 8, 2017 6:53 PM
  • User1120430333 posted

    Why would you need a Web API and IIS local to even do this?

    It seems to me that you could use a self-hosting WCF service, an exe,  to do this running on the local machine, Windows desktop solution with the form removed out it so that it's ust an exe with no form running local on the machine, a console application, an exe or a Windows service, an exe,  hosting a WCF service running local on the machine.

    Saturday, July 8, 2017 7:45 PM
  • User475983607 posted

    Why would you need a Web API and IIS local to even do this?

    It seems to me that you could use a self-hosting WCF service, an exe,  to do this running on the local machine, Windows desktop solution with the form removed out it so that it's ust an exe with no form running local on the machine, a console application, an exe or a Windows service, an exe,  hosting a WCF service running local on the machine.

    I imagine the RDowdall is running IoT on an embedded network device and simply wants to hit the device from a remote system.  The details are not real clear but IoT kinda' indicates an embedded device.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, July 8, 2017 9:28 PM
  • User64294366 posted

    Hello Thanks for you response,

    I am building a specialty alarm system that will monitor room occcupancy where there should never be less than 2 people in the room. To enter the room, a person scans a door badge reader, when they scan in, a record is inserted into a database table that is included in the access system, when a person scans out of the room the record is deleted. 

    In addition the room has a infrared sensor which monitors activity in the room, if the count is zero and there is motion sensed, then an audible alarm will sound and email notifications sensed. I've built 3 systems running on PCs, but now want to use a Raspberry Pi 3 and windows 10 IOT Core for a self contained system that will have less possible points of possible failure.

    The room is a high security area and must be monitored 24x7.

    The Web API is built, however, I am needing to determine the best way to call the web api and handle the response. 

    The logic is like this:

    count= API.CheckCount

    If count >2; continue

    if count=1; wait 20 seconds //provides time for last person to exit

    count = API.CheckCount

    if count=1; API.SendNotificationEmail;

    if count=0 and gpio = high (motion sensed)

    API.SendZeroOccupancyEmail

    My PC version of this used a loop, with thread.sleep to pause, however I've seen time variances after Microsoft patches were applied.

    The PC costs are $500 a piece vs $35 a piece for Raspberry Pi 3. Company policy dictates that the PCs be replace when the warranty expires.

    Using the Raspberry Pi, it would only be replaced upon failure.

    Due to the low cost a spare can be stored in the enclosure, preprogrammed and ready to go. 

    Hope this gives you a better overview of what I am doing.

    Regards,

    Sunday, July 9, 2017 6:36 PM
  • User64294366 posted

    Thanks for the response. Your solution is similar to what I am using in my PC based system. I am using thread.sleep instead of date.add.

    You solution may provide for greater accuracy.

    Regards,

    Sunday, July 9, 2017 6:39 PM
  • User1404573039 posted

    Hi RDowdall,

    If there is no other issue, I would suggest you mark the helpful reply as answer to close this thread.

    Regards,

    Tony

    Monday, July 10, 2017 3:09 AM