PC Review


Reply
Thread Tools Rate Thread

threads & GUI design pattern

 
 
zxc.err@gmail.com
Guest
Posts: n/a
 
      5th Mar 2007
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
 
 
 
 
Fabien
Guest
Posts: n/a
 
      5th Mar 2007
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
 
Guest
Posts: n/a
 
      5th Mar 2007
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
--


<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
 
Erel
Guest
Posts: n/a
 
      5th Mar 2007
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
 
Hilton
Guest
Posts: n/a
 
      5th Mar 2007
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
design pattern vb.net guoqi zheng Microsoft VB .NET 3 18th Nov 2007 08:29 PM
Design/Pattern guidance to refector my current design for unit testing sklett Microsoft C# .NET 1 22nd Aug 2007 01:58 PM
Design Pattern? pitachu@gmail.com Microsoft C# .NET 6 31st Jan 2006 11:31 PM
Design Pattern? pitachu@gmail.com Microsoft Dot NET Framework 2 31st Jan 2006 07:36 PM
design pattern Hugo Batista Microsoft Dot NET Framework 3 25th Feb 2005 08:22 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:35 PM.