473,237 Members | 1,220 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,237 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 1543
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: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.