I have an application that needs to check a website feed every second.
Sometimes the request to the server is longer than a second. In this case, the application needs to wait until the first request completes, then start a new request immediately. How could I implement this?
Also, the request should not freeze the GUI.
What Is a Triggering Event? A triggering event is a tangible or intangible barrier or occurrence which, once breached or met, causes another event to occur. Triggering events include job loss, retirement, or death, and are typical for many types of contracts.
You can select the Via console trigger operation while creating an event trigger to allow invoking the event trigger on rows manually using the Hasura console. Click on the event trigger you want to run and a modal will pop up with the request and response.
The trigger() method triggers the specified event and the default behavior of an event (like form submission) for the selected elements. This method is similar to the triggerHandler() method, except that triggerHandler() does not trigger the default behavior of the event.
I would use a simple variation of the producer-consumer pattern. You'd have two theads, a producer and a consumer, that share an integer variable. The producer would have a System.Threading.Timer
that fired every second, at which time it would Interlocked.Increment
the variable and call the consumer. The consumer logic repeatedly checks the feed and Interlocked.Decrement
the counter, while the counter is greater than zero. The consumer logic would be protected by a Monitor.TryEnter
which would handle re-entrancy. Here's sample code.
public static FeedCheck{
int _count = 0;
static object _consumingSync = new object();
static Threading.Timer _produceTimer;
private static void Consume() {
if (!Monitor.TryEnter(_consumingSync)) return;
try {
while(_count > 0) {
// check feed
Interlocked.Decrement(ref _count);
}
}
finally { Monitor.Exit(_consumingSync); }
}
private static void Produce() {
Interlocked.Increment(ref _count);
Consume();
}
public static void Start() {
// small risk of race condition here, but not necessarily
// be bad if multiple Timers existed for a moment, since only
// the last one will survive.
if (_produceTimer == null) {
_produceTimer = new Threading.Timer(
_ => FeedCheck.Produce(), null, 0, 1000
);
}
}
}
Usage:
FeedCheck.Start();
A good resource on .NET Threading (besides the MSDN Library stuff) is Jon Skeet's documentation, which includes this example of producer-consumer under "More Monitor
methods".
By the way, a true producer-consumer pattern revolves around a collection of work data, with one or more threads producing work by adding data to that collection, while one or more other threads consumer work by removing data from that collection. In our variation above, the "work data" is merely a count of the number of times we need to immediately check the feed.
(Another way to do it, instead of having the timer callback call Consume, is for the timer callback to lock and pulse a Monitor
that Consume waits on. In that case, Consume has an infinite loop, like while(true)
, which you kick off one time in its own thread. Therefore there is no need to support re-entrancy with the call to Monitor.TryEnter
.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With