471,624 Members | 2,059 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,624 software developers and data experts.

Async Delegates polling for vb.net examples

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

Nov 21 '05 #1
2 6078
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.

Viet wrote:
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


Nov 21 '05 #2
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


"Adam Goossens" <ad**********@users.sourceforge.net> wrote in message
news:#K**************@TK2MSFTNGP09.phx.gbl...
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.

Viet wrote:
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


Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Stephen | last post: by
reply views Thread by Shawn Meyer | last post: by
10 posts views Thread by Shawn Meyer | last post: by
2 posts views Thread by Viet | last post: by
reply views Thread by Viet | last post: by
11 posts views Thread by atlaste | last post: by
13 posts views Thread by Dog Ears | last post: by
2 posts views Thread by =?Utf-8?B?RGF2ZQ==?= | last post: by
reply views Thread by leo001 | last post: by
1 post views Thread by ZEDKYRIE | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.