PC Review Forums Newsgroups Microsoft DotNet Microsoft Dot NET Compact Framework threads & GUI design pattern

Reply

threads & GUI design pattern

 
Thread Tools Rate Thread
Old 05-03-2007, 03:51 PM   #1
zxc.err@gmail.com
Guest
 
Posts: n/a
Default threads & GUI design pattern


Hi, i am new to c# and would use some advice from more experienced
programmers .

i develop an application, that retrives data from the network. i start
a threadworker, that is running in the background and pooling for new
data from the server. what is proper pattern for updating GUI?

right know, i have an interfece, lets say MyForm, implemented by main
form. the interface has methods like: setStatusBarText,
setNotificationText that access forms' controls thru Control.invoke().
however, it requires the background thread to keep the reference to
the form's instance.

is it good practise? or should i switch to events handling?

regards, michal.

  Reply With Quote
Old 05-03-2007, 04:17 PM   #2
Fabien
Guest
 
Posts: n/a
Default Re: threads & GUI design pattern

Hi,

You should use events, it is cleaner.

BR


Fabien Decret
Windows Embedded Consultant


ADENEO (ADESET)
http://www.adeneo.adetelgroup.com/



On 5 mar, 16:51, zxc....@gmail.com wrote:
> Hi, i am new to c# and would use some advice from more experienced
> programmers .
>
> i develop an application, that retrives data from the network. i start
> a threadworker, that is running in the background and pooling for new
> data from the server. what is proper pattern for updating GUI?
>
> right know, i have an interfece, lets say MyForm, implemented by main
> form. the interface has methods like: setStatusBarText,
> setNotificationText that access forms' controls thru Control.invoke().
> however, it requires the background thread to keep the reference to
> the form's instance.
>
> is it good practise? or should i switch to events handling?
>
> regards, michal.



  Reply With Quote
Old 05-03-2007, 04:51 PM   #3
Guest
 
Posts: n/a
Default Re: threads & GUI design pattern

Even with events, the event handler runs in the context of the caller, so
those events will still have to call Control.Invoke. How to "best" handle
this really depends on the application. You could just raise the event and
let the consumer worry about marshaling it, or you could use your own
internal Control object to marshal to the right thread and raise from there,
so the consumer wouldn't have to know about your secondary thread.


--
Chris Tacke - Embedded MVP
OpenNETCF Consulting
Managed Code in the Embedded World
www.opennetcf.com
--


<zxc.err@gmail.com> wrote in message
news:1173109900.231829.3240@30g2000cwc.googlegroups.com...
> Hi, i am new to c# and would use some advice from more experienced
> programmers .
>
> i develop an application, that retrives data from the network. i start
> a threadworker, that is running in the background and pooling for new
> data from the server. what is proper pattern for updating GUI?
>
> right know, i have an interfece, lets say MyForm, implemented by main
> form. the interface has methods like: setStatusBarText,
> setNotificationText that access forms' controls thru Control.invoke().
> however, it requires the background thread to keep the reference to
> the form's instance.
>
> is it good practise? or should i switch to events handling?
>
> regards, michal.
>



  Reply With Quote
Old 05-03-2007, 04:53 PM   #4
Erel
Guest
 
Posts: n/a
Default Re: threads & GUI design pattern

You will still need to use Control.Invoke (inside the event handler)
as the event code will be run by the background thread.

  Reply With Quote
Old 05-03-2007, 08:16 PM   #5
Hilton
Guest
 
Posts: n/a
Default Re: threads & GUI design pattern

Michal,

Here's a really simple way:

Share a list. i.e. define IList list = new ArrayList () and make it
accessible by the thread and the form, obviously synchronize as required.
The thread just does a "list.Add ("Hello World")" and the form has a
System.Windows.Form.Timer firing every 1 sec or 0.5 seconds. It looks like:

while (this.KeepThreadRunning)
{
if (list.Count == 0)
{
Thread.Sleep (500);
}
else
{
while (list.Count > 0)
{
ShowInUI (list [0]);
list.Remove (0);
}
}

Change as appropriate, but basically the thread just adds to a list and the
UI thread pulls it out and displays it. For better efficiency use a
circular list, but if you only have a few 'operations' here and there, this
will be fine.

Hilton


  Reply With Quote
Reply



Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off