473,395 Members | 1,689 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,395 software developers and data experts.

Some high level C# design advice needed (?multi threading).

bob
I am unsure how to approach a C# windows App.

Essentially I want to do this.

I want to write an app that reads from database_A performs some calculations
updates the pretty dials on the screen then writes the 'cooked' data to
database_B (probably not in that order).
Also I will have some buttons on this main window that open some other
winforms so I can capture other bits of info and adjust the main window's
pretty dials and textboxes accordingly.

Being new to C# ( and very rusty at windows apps ) I found the wonderful
drag-and-drop timer double clicked it and low and behold a timer1_Tick event
appeared, wonderful thought I, I'll just put my database reads and screen
update calls in here.

But alas as was kindly pointed out to me every thing is running on a single
thread and while the app patiently waits for these DB calls and whatever
else I've piled into this event (timer1_Tick ) the main window (buttons,
dials etc) sit there completely unresponsive. I've never tried to make this
sort of 'polling' app before the closest I came was making my child windows
non-modal so they didn't freeze up the parent.

So how would more experienced C# programmer approach this at a high level.
the horrid words 'multi-threading' leap to my mind ...

Would it be wise to continue to run the timer on UI thread but each
timer_tick event start a separate thread to read/write the database and
perhaps when that thread has completed create a new thread to update my
screen. Ah bah humbug I really have no clue.

A starting point would be much appreciated ... Google and the beginners
guide to threading will help me with the rest. :-)

regards Bob.

Feb 8 '06 #1
8 1547
bob wrote:

<snip>
Would it be wise to continue to run the timer on UI thread but each
timer_tick event start a separate thread to read/write the database and
It's not clear to me why you need a timer. I didn't notice anything
periodic in your description. Could you expand on that a bit?
perhaps when that thread has completed create a new thread to update my
screen. Ah bah humbug I really have no clue.
You don't need to create a new thread to update the screen - you need
to marshall the call back to the UI thread using
Control.Invoke/BeginInvoke.
A starting point would be much appreciated ... Google and the beginners
guide to threading will help me with the rest. :-)


Have a look at http://www.pobox.com/~skeet/csharp/threads
and in particular
http://www.pobox.com/~skeet/csharp/t...winforms.shtml

Jon

Feb 8 '06 #2
bob

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:11*********************@g43g2000cwa.googlegro ups.com...

It's not clear to me why you need a timer. I didn't notice anything
periodic in your description. Could you expand on that a bit?


I think I need the timer because I have to read database_A every few seconds
.... in reality datbase_A is a place holder for an external Device that
intermittedly has results that need to be first retrieved then analysed and
then finally stored into a Database on the PC.

Polling at regular intervals at this point is the only way to get the info
from the device.
Feb 8 '06 #3
Hi,

Being new to C# ( and very rusty at windows apps ) I found the wonderful
drag-and-drop timer double clicked it and low and behold a timer1_Tick
event appeared, wonderful thought I, I'll just put my database reads and
screen update calls in here.
Almost there, a better way is that the event handler create a thread wich is
the one that do the reads/calculations , after its finish you send an event
for updating the UI , kind of:

void timer1_tick( ... )
{
new Thread( new ThreadStart( workermethod)).Start();
}
void workermethod()
{
//do the stuff
a_control_in_the_UI.Invoke( new MethodInvoker( runAtUI_Thread) );
}

void runAtUI_Thread()
{
//this will execute in the UI thread
//update controls.
}
Would it be wise to continue to run the timer on UI thread but each
timer_tick event start a separate thread to read/write the database and
perhaps when that thread has completed create a new thread to update my
screen. Ah bah humbug I really have no clue.


Yep, use the code above as a guide, I wrote it right here in OE so it would
not compile most probably.

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Feb 8 '06 #4
bob <bo*@bob.com> wrote:
It's not clear to me why you need a timer. I didn't notice anything
periodic in your description. Could you expand on that a bit?


I think I need the timer because I have to read database_A every few seconds
... in reality datbase_A is a place holder for an external Device that
intermittedly has results that need to be first retrieved then analysed and
then finally stored into a Database on the PC.

Polling at regular intervals at this point is the only way to get the info
from the device.


Right, that's fine - it's the polling bit which wasn't clear.

However, you don't need to use a System.Windows.Forms timer. See
http://www.pobox.com/~skeet/csharp/threads/timers.shtml for the various
different options available to you.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 8 '06 #5
Hello Bob

Strictly speaking you should be using a thread to do the work and then
update your GUI using InvokeRequired / Invoke. But there is a quick and
dirty way to achive the same thing if the workload is not to heavy

private void timer1_tick(...)
{
DoSomeWork();
Application.DoEvents();
DoSomeMoreWork();
Application.DoEvents();
....
UpdateGui();
Application.DoEvents();
}

Again ... this is a very quick and dirty way, so please donīt tell
anyone that you got from me :-)

Regards
Bjarne

Feb 8 '06 #6
Hello Bob

Strictly speaking you should be using a thread to do the work and then
update your GUI using InvokeRequired / Invoke. But there is a quick and
dirty way to achive the same thing if the workload is not to heavy

private void timer1_tick(...)
{
DoSomeWork();
Application.DoEvents();
DoSomeMoreWork();
Application.DoEvents();
....
....
Application.DoEvents();
}

Again ... this is a very quick and dirty way, so please donīt tell
anyone that you got from me :-)

Regards
Bjarne

Feb 8 '06 #7
Hello Bob

Strictly speaking you should be using a thread to do the work and then
update your GUI using InvokeRequired / Invoke. But there is a quick and
dirty way to achive the same thing if the workload is not to heavy

private void timer1_tick(...)
{
DoSomeWork();
Application.DoEvents();
DoSomeMoreWork();
Application.DoEvents();
....
UpdateGui();
Application.DoEvents();
}

Again ... this is a very quick and dirty way, so please donīt tell
anyone that you got from me :-)

Regards
Bjarne

Feb 8 '06 #8
bob
Thanks gents.

I think I have an idea of where I'm going and now its just getting into the
books for a bit to understand what I want to do.

So if I don't process too much in the on_tick1 event I can look at
marshalling the call back to the UI thread using
Control.Invoke/BeginInvoke or maybe the amount of processing isn't important
because this marshalling is keeping the UI responsive, further reading will
enlighten.

And/or I can start a new thread for the time intensive operations in the
on_tick1 event then when it returns update my gauges etc.

And the Application.DoEvents() is a quick and dirty way that will lead me
down the path to purdition from whence I shall be shunned by my betters. ;-)

Time to read play and swear for a while until it all becomes clear

regards Bob.
Feb 8 '06 #9

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

Similar topics

2
by: nitrogenycs | last post by:
Hello, I need a way to get a notification whenever a variable of an object changes. The approach should be non-intrusive so that I can use existing objects without modifying them. I want to be...
5
by: Nick Malik | last post by:
reposting to a wider audience "Nick Malik" <nickmalik@hotmail.nospam.com> wrote in message news:WYONc.203854$XM6.119642@attbi_s53... > My turn to ask a question > > I am working on a plug-in...
4
by: Nick Malik | last post by:
My turn to ask a question I am working on a plug-in for Sharepoint that will allow a developer to add workflow rules. One of the rules will inform the adapter that it should load a DLL that the...
3
by: Jonathan Woods | last post by:
I have a bridge windows service application that provide service for two applications running on Oracle and SQL server 2000 respectively. Oracle application has trip info and SQL application has...
1
by: Rob | last post by:
What is the best way to safely access a companies ERP data (SQL server) from a website that is hosted outside the firewall ?
4
by: mazdotnet | last post by:
Hi guys, We have an events page in our company that's database driven and we need to shared it with our partners. They need to pull information from our site. They can be running asp, php,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.