By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,344 Members | 1,142 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,344 IT Pros & Developers. It's quick & easy.

VB.Net sample thread code

P: n/a
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

Nov 21 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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.

<oo******@hotmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
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

Nov 21 '05 #2

P: n/a
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

Nov 21 '05 #3

P: n/a
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.

Michael C# wrote:
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.

<oo******@hotmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
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


Nov 21 '05 #4

P: n/a
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?
--
Dennis in Houston
"Michael C#" wrote:
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.

<oo******@hotmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
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


Nov 21 '05 #5

P: n/a
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

Nov 21 '05 #6

P: n/a
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.

<oo******@hotmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
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

Nov 21 '05 #7

P: n/a
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.

"Dennis" <De****@discussions.microsoft.com> wrote in message
news:4C**********************************@microsof t.com...
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?
--
Dennis in Houston
"Michael C#" wrote:
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.

<oo******@hotmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
> 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
>


Nov 21 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.