0

I have an application in C# where I get update events from my database repository for each update in the database. When large operations are made on the database multiple events are fired and I want to update the GUI of the application when these events are received, but updating the GUI for each update is not exactly optimal.

What is a good way to "pool" events and only react when no more events are coming in, my first idea was to use a timer with a short duration and reset it for each new event received but this seems a bit clunky. Is there a better/faster/simpler way to solve this problem?

I would prefer not to have visible delays during normal updates.

Kristoffer L
  • 728
  • 1
  • 9
  • 20
  • Define the condition of `no more events are coming` – Sanjeevakumar Hiremath Mar 29 '11 at 06:39
  • For example I update 10 rows in the database I get 10 events back, in rapid succession. After that I get no more events unless there are other updates. – Kristoffer L Mar 29 '11 at 06:47
  • How do you detect that? That is my question, How do you detect that there are no more events coming in? – Sanjeevakumar Hiremath Mar 29 '11 at 06:49
  • Rephrase: When I receive an event I want to update the GUI, but if I receive several events in quick succession I want to delay the GUI update so I do not force multiple updates. There is no (easy) way of knowing how many events will be triggered beforehand so the only solution to detecting if "no more events" are coming is by checking the time since the last event. Bulk updates usually come in in a rather quick succession so the time between events is < 1 second. – Kristoffer L Mar 29 '11 at 07:45

4 Answers4

1

I think your way would be the way to go. I would write something like a EventAggregator that has a timer. This timer will start when the first event comes in and will be reset each time a new event comes in within the waiting time. If the waiting time is reached it will raise its own event.

But this leads also to some delay when you only have a single event. So be sure where you want such a behaviour. Also be sure that you fire your async event in the right thread or you'll run into Cross-thread exeception. Maybe the article Implementing the Event-based Asynchronous Pattern article can give you some hints on this question. Also take a look into this Timer article to take the correct timer class for your purpose.

Oliver
  • 43,366
  • 8
  • 94
  • 151
  • This is basically what I stated in my initial post as the only idea I had (but alas more fleshed out). I would like to see that small infrequent updates (single rows) are updated immediately while large updates (several rows at a time) are updated when all updates are made. – Kristoffer L Mar 29 '11 at 07:53
1

You may be able to consider the Reactive Framwework.

How to throttle event stream using RX?

Community
  • 1
  • 1
yzxben
  • 862
  • 6
  • 11
  • Reactive Framework looks very interesting, I will examine if this is applicable in my application. – Kristoffer L Mar 29 '11 at 08:15
  • 1
    Reactive Framework both makes event handling easier and also allows for throttling and limitations on events (or anything really) so it really helped me out. Thanks. – Kristoffer L Apr 02 '11 at 10:36
1

Create a separate Thread which waits on a ManualResetEvent. Use a Queue<DbEvent> to enqueue new events. Signal the ManualResetEvent when a new db event arrives.

Remember to lock the queue each time you dequeue or enqueue items.

In this way you can add new db events from several threads and still get a proper handling with one event at a time.

jgauffin
  • 99,844
  • 45
  • 235
  • 372
0

In my opinion a good approach will be:

When the database is updated, do not make the updates on the guy immediately, wait for a short but determined period to see if any more updates on the database are made.After the period has passed the you should commit the updates to the gui. Then your gui will not be updated in real time, but i think a few seconds is not such a big problem(depending of the usage of the api).The nice thing is that you do not need to update the gui at every database update event.

I hope this will help you.

Cheers Vlad.

Marginean Vlad
  • 329
  • 1
  • 6
  • A few seconds delay on every database update is not acceptable for this application. It would make the application behave a bit randomly as you do an operation and then some time later the application updates to reflect the changes you have made. – Kristoffer L Mar 29 '11 at 07:51
  • Then it seems you need another approach.I am sorry a can't give you an appropriate answer on this mater. – Marginean Vlad Mar 29 '11 at 07:53