Are you ready to consider a reasonable alternative? I think that asynchronous APIs are just the leftovers of the days when threading was not a commonplace. Threading implementation is more straightforward, easier to design and debug and more universal. It has sequential logic; you have more control. In return, you have to focus on thread synchronization, but synchronization are universal, not specific to application, so you can use the same techniques over and over.
I described my schema of database polling here:
Polling Database with Timer[
^].
It can be applied to your problem as well.
[EDIT: answering a follow-up question on samples; please see commends below]
Unfortunately, the shortest comprehensive code sample would require too much of code, more then even I usually write in this forum (I tend to write much longer answers compared to an average one).
Please see my other referenced above. First, it references some code showing a pretty good ways working with the thread using a thread wrapper. The following answers contain detailed code samples and explain why this is a good way:
How to pass ref parameter to the thread[
^],
change paramters of thread (producer) after it started[
^].
You can work with ADO.NET in such separate thread, but not with to UI. Also, the thread polling the database should be is a such separate thread, which can get to a wait state waiting for the event wait handle set in the timer. When the data is retrieved from the database, you should notify the UI thread with new data, for example, through binding.
You cannot call anything related to UI from non-UI thread. Instead, you need to use the method
Invoke
or
BeginInvoke
of
System.Windows.Threading.Dispatcher
(for both Forms or WPF) or
System.Windows.Forms.Control
(Forms only).
You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke()[
^],
Problem with Treeview Scanner And MD5[
^].
See also more references on threading:
How to get a keydown event to operate on a different thread in vb.net[
^],
Control events not firing after enable disable + multithreading[
^].
Assuming you know how to work with ADO.NET, you now have all the missing pieces. Any further questions? You are welcome to ask them, but I would be able to effectively answer if they are related to the design elements I have just depicted.
—SA