VB.Net sample thread code

O

oop_dude

I'm trying to use a thread in the following situation, but having never
used threading before, I don't know where to begin.

My program has a timer set to fire every 10 seconds or so. In the tick
event I call a SQL Server stored procedure. As the stored proc has
grown in scope it sometimes takes more than 10 seconds to run.

When this happens a SECOND timer event fires before the first one
returns. This of course causes runtime errors (record conflicts) from
SQL Server.

I tried disabling the timer immediately upon entering the tick event
and then re-enabling it just prior to exiting the tick event. However,
doing this causes the program to become "sluggish", like menu
selections not responding properly, etc. A DoEvents would normally
help, but of course once the stored proc starts DoEvents doesn't work.

So, next thought is to make the tick event spawn a thread that in turn
calls the stored proc. Before spawing the thread to call the stored
proc, the tick event code should check to see if the previous thread is
still running. If so just exit out and try again during the firing of
the next tick event.

If someone could post a short snippet of code on how to accomplish
this, I'd be most appreciative.

tks

Oop
 
M

Michael C#

Sounds like you're using a System.Windows.Forms.Timer. Try using a
System.Threading.Timer or Systems.Timers.Timer. They fire off in a separate
thread so your application should maintain its responsiveness. Since they
are designed for multithreading use you don't have to do any old skool
'DoEvents' type stuff to keep the UI responsive.
 
O

oop_dude

Hey Michael C#...... Thanks a LOT...... You are right , I am
using the "old" System.Windows.Forms.Timer . I will try what you
suggest.

Oop
 
B

Brian Gideon

Also, remember to use the ISynchronizeInvoke.Invoke method when you
need to update a form or control from a thread other than the main UI
thread.
 
G

Guest

I tried using the system.timers.timer instead of the windows.forms.timer on
one of my applications and I get weird results. I am using a custom sound
control and use the timer to update the play time. In the timer.elapsed
event, I query the status using mcisendstring. The return is an error No.
263 with the system timer but I get no errors with the windows timer. Any
idea what would cause this?
 
O

oop_dude

again, my thanks to all........ a threading.timer works just fine
but has an interesting "side effect".

the original tick event (with the forms.timer) had a call to a stored
proc, followed by three Dataset.Clear and three SQLAdatper.Fill in
order to refresh three separate grids from tables updated by the stored
proc.

before using the threading.timer the grid refreshes were invisible
to the eye. with the threading.timer it is VERY apparent (flicker)
each time the event is fired.

Interesting. no??

Oop
 
M

Michael C#

Turn on double-buffering. Look into BeginUpdate and EndUpdate methods. Are
you using the InvokeRequired function and BeginIvoke before updating the UI?

A couple of things to look at.
 
M

Michael C#

Check the parameters you're using with MCISendString. MCI Error 263 is
"MCIERR_INVALID_DEVICE_NAME". In particular look at what you're passing in
as the lpszDeviceID in the MCI command string.
 

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

Top