Async Delegates polling for vb.net examples

  • Thread starter Thread starter Viet
  • Start date Start date
V

Viet

I have a couple of questions that hopefully someone could clarify for me. I
have an app that uses the threading.timer to constantly poll a scanner to
scan in documents. I understand that Async delegates can also be used for
polling purposes. Would it be more efficient to use async delegates in this
case? Are their any examples on how to use this under vb.net forms?

Thanks,
Jonathan
 
Hi Viet,

If your interface library to the scanner exposes an asynchronous means
of receiving data you should use it. It's more efficient in two senses:

1) you don't consume the resources of a System.Threading.Timer, and
2) you don't waste time using ThreadPool threads that aren't going to be
able to read any data anyway (which happens most of the time when you poll).

Async delegates/callbacks make for a much cleaner program structure in
my opinion.

Using asynchronous delegates with Windows Forms isn't difficult at all.
All you need to remember is that you make sure you synchronise access to
your UI elements (remember: never access UI objects from a thread other
than the UI thread - Control.Invoke() and Control.InvokeRequired).

If your scanning library doesn't provide support for asynchronous
reading of data, well, there may be other ways but it all depends on how
your library is implemented. You might be able to make a blocking call
in another thread or something.

Regards,
-Adam.
 
Adam,

Thanks for your advice!

Yes, I have noticed in my app that the use of System.Threading.Timers is
very resource intensive especially when I have to constantly poll a scanner
to read in docs. I have implemented other approaches like the use of form
timers, etc but still the same problem. I am able to implement async timers
to my scanner routine but I am stuck in one simple area which I cannot
resolve without the use of threading timers. Consider the following code:

Sub AsyncCallBack()

Dim objAsyncDel As New AsyncPollScanDelegate(AddressOf RunThirdThread_proc)

Dim objAsync As New AsyncCallback(AddressOf MyCallBackThreadPoolRoutine)

Dim objAR As IAsyncResult

objAR = objAsyncDel.BeginInvoke(objAsync, Nothing)

Do Until objAR.IsCompleted

'Do processing stuff

Loop

'objAsyncDel.EndInvoke(objAR)

End Sub

As you can see this will execute once and complete but how to I have the
program execute in an infinite loop without the use of timers?? The objAR
delegate will return IsCompleted and this routine will complete. For me to
implement a periodic polling technique is there another approach to above
without the use of threading timers?

Also, you mentioned ThreadPool threads. My app not only has to scan in
documents, but it has to process the documents by executing another program
to crop, despeckle, etc...and to produce thumbnail images of the scanned
images. The original programmer split up the processing into 4 main routines
(in VB6) which I implemented into 4 threadpool threads for my conversion to
VB.NET. Architecturally speaking, do you feel it is more efficient to split
these 4 threadpool threads into 4 async delegates instead??



Thanks!

Viet
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top