473,396 Members | 1,972 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Thread related design advice please

Hi,

I have a form application that needs to query a DB at a set timed interval
and then refresh the form with values received from the DB. Simple enough.

My design releated questions are ....

Should I create a worker thread that encapsulates a timer, (which I believe
will also create a seperate thread when the timer fires) , or should I
create a timer that creates a new thread when it fires ? Or neither ?

Which sort of timer should I use. "System.Timers.Timer" or
"System.Windows.Forms.Timer"

I was going to 'place' the results of the DB query into a queue of some sort
that can then be retrieved at a set interval ( another timer ? ) and display
them on the form. How do I make this queue threadsafe ?

Some general design pointers would be appreciated.

Thanks.
Jon.
Sep 12 '06 #1
10 1248
....also I have just noticed the 'System.Threading.Timer' !


"Bishman" <jo*************@btinternet.comwrote in message
news:eA**************@TK2MSFTNGP02.phx.gbl...
Hi,

I have a form application that needs to query a DB at a set timed interval
and then refresh the form with values received from the DB. Simple enough.

My design releated questions are ....

Should I create a worker thread that encapsulates a timer, (which I
believe will also create a seperate thread when the timer fires) , or
should I create a timer that creates a new thread when it fires ? Or
neither ?

Which sort of timer should I use. "System.Timers.Timer" or
"System.Windows.Forms.Timer"

I was going to 'place' the results of the DB query into a queue of some
sort that can then be retrieved at a set interval ( another timer ? ) and
display them on the form. How do I make this queue threadsafe ?

Some general design pointers would be appreciated.

Thanks.
Jon.

Sep 12 '06 #2

Could I use two forms timers, one to handle the DB query and one to handle
the UI update ?

If I take this approach I would assume I would still need to perform some
thread safety around the saving and retrieving of records ?

ie Thread 1 updates retrieves DB records and saves them away in the 'queue',
thread two reads from the 'queue' at set intervals and updates the ui ?

.........confused......

Jon


"Bishman" <jo*************@btinternet.comwrote in message
news:eA**************@TK2MSFTNGP02.phx.gbl...
Hi,

I have a form application that needs to query a DB at a set timed interval
and then refresh the form with values received from the DB. Simple enough.

My design releated questions are ....

Should I create a worker thread that encapsulates a timer, (which I
believe will also create a seperate thread when the timer fires) , or
should I create a timer that creates a new thread when it fires ? Or
neither ?

Which sort of timer should I use. "System.Timers.Timer" or
"System.Windows.Forms.Timer"

I was going to 'place' the results of the DB query into a queue of some
sort that can then be retrieved at a set interval ( another timer ? ) and
display them on the form. How do I make this queue threadsafe ?

Some general design pointers would be appreciated.

Thanks.
Jon.

Sep 12 '06 #3
Jon,

There are several acceptable options. I would probably use the
System.Timers.Timer to execute the DB code. After the DB code ran I
would call Control.Invoke to marshal a delegate onto the UI thread to
update the form with the results. Avoid setting the
SynchronizingObject property on the Timer because you want the DB code
to execute off the UI thread. That way the UI remains responsive.

The caveat here is that System.Timers.Timer will use a ThreadPool
thread when SynchronizingObject is null. I typically recommend not
putting long running tasks on the ThreadPool, but I'm guilty of bending
the rules sometimes myself.

I think it would be fine if you chose to create and use a dedicate
thread that uses a Thread.Sleep (or other waiting mechanism) to execute
the DB code on an interval as well. If you have a worker thread
dedicated for the task then there's no need for a timer. Again, make
sure to use Control.Invoke to update the form.

As for the queue question, it's probably not needed. The Windows
message loop running on the UI thread is already implemented with a
queue. A message is sent to it already with Control.Invoke.

Brian

Bishman wrote:
Hi,

I have a form application that needs to query a DB at a set timed interval
and then refresh the form with values received from the DB. Simple enough.

My design releated questions are ....

Should I create a worker thread that encapsulates a timer, (which I believe
will also create a seperate thread when the timer fires) , or should I
create a timer that creates a new thread when it fires ? Or neither ?

Which sort of timer should I use. "System.Timers.Timer" or
"System.Windows.Forms.Timer"

I was going to 'place' the results of the DB query into a queue of some sort
that can then be retrieved at a set interval ( another timer ? ) and display
them on the form. How do I make this queue threadsafe ?

Some general design pointers would be appreciated.

Thanks.
Jon.
Sep 12 '06 #4
Bishman,

I would use the timer in the System.Timers namespace. When the event
fires, I would perform the fetching of the data (since the callback will be
on a non-ui thread anyways), and then update the UI through a call to
Invoke.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Bishman" <jo*************@btinternet.comwrote in message
news:eA**************@TK2MSFTNGP02.phx.gbl...
Hi,

I have a form application that needs to query a DB at a set timed interval
and then refresh the form with values received from the DB. Simple enough.

My design releated questions are ....

Should I create a worker thread that encapsulates a timer, (which I
believe will also create a seperate thread when the timer fires) , or
should I create a timer that creates a new thread when it fires ? Or
neither ?

Which sort of timer should I use. "System.Timers.Timer" or
"System.Windows.Forms.Timer"

I was going to 'place' the results of the DB query into a queue of some
sort that can then be retrieved at a set interval ( another timer ? ) and
display them on the form. How do I make this queue threadsafe ?

Some general design pointers would be appreciated.

Thanks.
Jon.

Sep 12 '06 #5
Thanks for that.

Can I update a Queue in the same way ?
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:uO**************@TK2MSFTNGP02.phx.gbl...
Bishman,

I would use the timer in the System.Timers namespace. When the event
fires, I would perform the fetching of the data (since the callback will
be on a non-ui thread anyways), and then update the UI through a call to
Invoke.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Bishman" <jo*************@btinternet.comwrote in message
news:eA**************@TK2MSFTNGP02.phx.gbl...
>Hi,

I have a form application that needs to query a DB at a set timed
interval and then refresh the form with values received from the DB.
Simple enough.

My design releated questions are ....

Should I create a worker thread that encapsulates a timer, (which I
believe will also create a seperate thread when the timer fires) , or
should I create a timer that creates a new thread when it fires ? Or
neither ?

Which sort of timer should I use. "System.Timers.Timer" or
"System.Windows.Forms.Timer"

I was going to 'place' the results of the DB query into a queue of some
sort that can then be retrieved at a set interval ( another timer ? ) and
display them on the form. How do I make this queue threadsafe ?

Some general design pointers would be appreciated.

Thanks.
Jon.


Sep 12 '06 #6
Brian,

Many thanks for the reply.

I will try and digest what all that means then give it a go !

Just two points. Can you explain a little more about what you mean by "Avoid
setting the SynchronizingObject property on the Timer". Do you mean attach
the synchronizing object to the UI Thread not the Timer Thread ? (If that
makes sense ....).

Also I was going to use a queue as the UI will display the results of the
each record selected for a set time, then move onto the next record and so
on. That way the worker thead can continue to pull back various records at
odd intervals, but the UI thread can display each record for a set period,
then move on.

I think this will work.


"Brian Gideon" <br*********@yahoo.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
Jon,

There are several acceptable options. I would probably use the
System.Timers.Timer to execute the DB code. After the DB code ran I
would call Control.Invoke to marshal a delegate onto the UI thread to
update the form with the results. Avoid setting the
SynchronizingObject property on the Timer because you want the DB code
to execute off the UI thread. That way the UI remains responsive.

The caveat here is that System.Timers.Timer will use a ThreadPool
thread when SynchronizingObject is null. I typically recommend not
putting long running tasks on the ThreadPool, but I'm guilty of bending
the rules sometimes myself.

I think it would be fine if you chose to create and use a dedicate
thread that uses a Thread.Sleep (or other waiting mechanism) to execute
the DB code on an interval as well. If you have a worker thread
dedicated for the task then there's no need for a timer. Again, make
sure to use Control.Invoke to update the form.

As for the queue question, it's probably not needed. The Windows
message loop running on the UI thread is already implemented with a
queue. A message is sent to it already with Control.Invoke.

Brian

Bishman wrote:
>Hi,

I have a form application that needs to query a DB at a set timed
interval
and then refresh the form with values received from the DB. Simple
enough.

My design releated questions are ....

Should I create a worker thread that encapsulates a timer, (which I
believe
will also create a seperate thread when the timer fires) , or should I
create a timer that creates a new thread when it fires ? Or neither ?

Which sort of timer should I use. "System.Timers.Timer" or
"System.Windows.Forms.Timer"

I was going to 'place' the results of the DB query into a queue of some
sort
that can then be retrieved at a set interval ( another timer ? ) and
display
them on the form. How do I make this queue threadsafe ?

Some general design pointers would be appreciated.

Thanks.
Jon.

Sep 12 '06 #7

Bishman wrote:
Brian,

Many thanks for the reply.

I will try and digest what all that means then give it a go !

Just two points. Can you explain a little more about what you mean by "Avoid
setting the SynchronizingObject property on the Timer". Do you mean attach
the synchronizing object to the UI Thread not the Timer Thread ? (If that
makes sense ....).
SynchronizingObject accepts an ISynchronizeInvoke object. Forms and
controls implement this interface. When the property is set to a form
or control from your application the timer will automatically marshal
the execution of the Elapsed event onto the UI thread. If you did that
then you DB code wouldn't be executed on a separate thread. Think of
it this this way. The Elapsed event will run on the same thread
hosting the SynchronizingObject which is usually a Form or Control. If
the SynchronizingObject is null the Elapsed event will run a thread
from the ThreadPool.
>
Also I was going to use a queue as the UI will display the results of the
each record selected for a set time, then move onto the next record and so
on. That way the worker thead can continue to pull back various records at
odd intervals, but the UI thread can display each record for a set period,
then move on.
Okay, I think I understand what you're saying. The worker or timer
thread will be enqueueing records into the queue and the UI will
dequeue them. And you want it that way because the enqueueing interval
may be different than the dequeueing interval. Is that right?

Sep 12 '06 #8
Care to elaborate on what you mean by a Queue?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Bishman" <jo*************@btinternet.comwrote in message
news:Ot**************@TK2MSFTNGP02.phx.gbl...
Thanks for that.

Can I update a Queue in the same way ?
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote
in message news:uO**************@TK2MSFTNGP02.phx.gbl...
>Bishman,

I would use the timer in the System.Timers namespace. When the event
fires, I would perform the fetching of the data (since the callback will
be on a non-ui thread anyways), and then update the UI through a call to
Invoke.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Bishman" <jo*************@btinternet.comwrote in message
news:eA**************@TK2MSFTNGP02.phx.gbl...
>>Hi,

I have a form application that needs to query a DB at a set timed
interval and then refresh the form with values received from the DB.
Simple enough.

My design releated questions are ....

Should I create a worker thread that encapsulates a timer, (which I
believe will also create a seperate thread when the timer fires) , or
should I create a timer that creates a new thread when it fires ? Or
neither ?

Which sort of timer should I use. "System.Timers.Timer" or
"System.Windows.Forms.Timer"

I was going to 'place' the results of the DB query into a queue of some
sort that can then be retrieved at a set interval ( another timer ? )
and display them on the form. How do I make this queue threadsafe ?

Some general design pointers would be appreciated.

Thanks.
Jon.



Sep 12 '06 #9

"System.Collections" namespace - "Queue" Class .


"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:%2******************@TK2MSFTNGP04.phx.gbl...
Care to elaborate on what you mean by a Queue?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Bishman" <jo*************@btinternet.comwrote in message
news:Ot**************@TK2MSFTNGP02.phx.gbl...
>Thanks for that.

Can I update a Queue in the same way ?
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote
in message news:uO**************@TK2MSFTNGP02.phx.gbl...
>>Bishman,

I would use the timer in the System.Timers namespace. When the event
fires, I would perform the fetching of the data (since the callback will
be on a non-ui thread anyways), and then update the UI through a call to
Invoke.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Bishman" <jo*************@btinternet.comwrote in message
news:eA**************@TK2MSFTNGP02.phx.gbl...
Hi,

I have a form application that needs to query a DB at a set timed
interval and then refresh the form with values received from the DB.
Simple enough.

My design releated questions are ....

Should I create a worker thread that encapsulates a timer, (which I
believe will also create a seperate thread when the timer fires) , or
should I create a timer that creates a new thread when it fires ? Or
neither ?

Which sort of timer should I use. "System.Timers.Timer" or
"System.Windows.Forms.Timer"

I was going to 'place' the results of the DB query into a queue of some
sort that can then be retrieved at a set interval ( another timer ? )
and display them on the form. How do I make this queue threadsafe ?

Some general design pointers would be appreciated.

Thanks.
Jon.



Sep 13 '06 #10
OK - I think I get that ! Thanks very much.

Jon.
"Brian Gideon" <br*********@yahoo.comwrote in message
news:11*********************@d34g2000cwd.googlegro ups.com...
>
Bishman wrote:
>Brian,

Many thanks for the reply.

I will try and digest what all that means then give it a go !

Just two points. Can you explain a little more about what you mean by
"Avoid
setting the SynchronizingObject property on the Timer". Do you mean
attach
the synchronizing object to the UI Thread not the Timer Thread ? (If that
makes sense ....).

SynchronizingObject accepts an ISynchronizeInvoke object. Forms and
controls implement this interface. When the property is set to a form
or control from your application the timer will automatically marshal
the execution of the Elapsed event onto the UI thread. If you did that
then you DB code wouldn't be executed on a separate thread. Think of
it this this way. The Elapsed event will run on the same thread
hosting the SynchronizingObject which is usually a Form or Control. If
the SynchronizingObject is null the Elapsed event will run a thread
from the ThreadPool.
>>
Also I was going to use a queue as the UI will display the results of the
each record selected for a set time, then move onto the next record and
so
on. That way the worker thead can continue to pull back various records
at
odd intervals, but the UI thread can display each record for a set
period,
then move on.

Okay, I think I understand what you're saying. The worker or timer
thread will be enqueueing records into the queue and the UI will
dequeue them. And you want it that way because the enqueueing interval
may be different than the dequeueing interval. Is that right?

Sep 13 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

31
by: AlexeiOst | last post by:
Everywhere in documentation there are recommendations to use threads from thread pooling for relatively short tasks. As I understand, fetching a page or multiple pages (sometimes up to 50 but not...
1
by: sleepyant | last post by:
Hi. I try to call msgbox("somehting") inside a sub which is run in a different background thread. The problem is the message box is not pop-up as a modal form, so whenever there is a message box...
6
by: Tony Proctor | last post by:
Hi everyone We're experiencing some serious anomalies with the scheduling of ASP threads. I'd be interested to hear if anyone knows what algorithm is used (e.g. simple round-robin, or something...
6
by: ±è¿ë°Ç | last post by:
my project has critical bug that TTS(text to speech) engine has two thread one thread is socket thread that receives string from socket server and other thread is engine thread that processes the...
6
by: m | last post by:
Hello, I have an application that processes thousands of files each day. The filenames and various related file information is retrieved, related filenames are associate and placed in a linked...
10
by: Lenn | last post by:
Hello, I have always used a certain design pattern for multithreaded Windows app; Start new worker thread from UI thread, use events to notify UI threads when something happens, update UI...
5
by: jzlondon | last post by:
Hi, I have a question that I wonder if someone might be able to help me with... I have an application which handles real-time financial data from a third party source. The data comes in via...
8
by: =?Utf-8?B?cmFuZHkxMjAw?= | last post by:
I have an application with several BackgroundWorker threads. I hoped I'd be able to just type backgroundworker1.Name = "bw1"; but I don't see a name property. Any thoughts on how to name a...
8
by: Brad Walton | last post by:
Hello. First post, but been doing a bit of reading here. I am working on a project in Java, but decided to switch over to C# after seeing some of the additional features I can get from C#. One of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.