UserControl always run in seperate thread

  • Thread starter Thread starter Joe
  • Start date Start date
J

Joe

I need a control to always run in a separate thread from the application.
I'm not too sure where to begin with this since the control could be dropped
on the form at design time.

One possible idea I had was to wrap my control in another control which
creates the thread and than creates the control.

Any suggestions?

-Joe
 
This is not my strongest area in .NET but I don't think this would
work.

(Most) controls aren't thread safe and the UI thread would be calling
methods on your control (e.g. Paint, because you've added it to the
main forms control collection) even though you created the control on
your own different thread.

I maybe wrong and I'd be very interested to hear a solution.

You might want to try posting to winforms newsgroup.

Josh
http://www.thejoyofcode.com/
 
Hi Josh,

Actually, all the control does is display a spinning wheel (like in SQL
2005) when something is happening which will take a while - sort of like a
progress bar and displays a message like Please wait...

I was thinking about calling BeginInvoke but I would need something to
prevent it from returning until I'm done.

I guess the only other idea is to make all the work happen on another
thread.

-Joe
 
| Hi Josh,
|
| Actually, all the control does is display a spinning wheel (like in SQL
| 2005) when something is happening which will take a while - sort of like a
| progress bar and displays a message like Please wait...
|

That 'something' should be done on a separate background thread, the
updating of the UI (your spinning wheel) should be done using
Control.BeginInvoke or Control.Invoke.

Willy.
 
Hi Willy,

The problem is the spinning wheel is just an animated GIF. I want to show my
UserControl in a panel (which is in the main thread) and allow the GIF to
remain animated.

I'm not sure how to use BeginInvoke to work in this case.

-Joe
 
Joe said:
Actually, all the control does is display a spinning wheel (like in SQL
2005) when something is happening which will take a while - sort of like a
progress bar and displays a message like Please wait...

I was thinking about calling BeginInvoke but I would need something to
prevent it from returning until I'm done.

I guess the only other idea is to make all the work happen on another
thread.

See http://www.pobox.com/~skeet/csharp/threads/winforms.shtml
 
Hi Joe,

I agree with Willy on using background thread together with using the
Control.Invoke method....
Actually the controls you're using are someones which display the waiting
UI and message, yes? I think we can just define some method in the Form
class which used to control the UI (updating the control's states....),
then since our long run task are being processed in background thread and
we should not directly call those Form's method to update UI, so we can use
Control(Form).Invoke method to call those method to update UI in the
background thread's thread function..... e.g:

suppose our backgroun thread do the following function:

while (xxxxxx)
{

//do the work....

_form.Invoke(UpdateUIFunction_Delegate......);
}

UpdateUIFunction_Delegate just wrapper a Form's member function which
update some of the controls on the form(maybe your GIF components or....)

Some other related reference articles:

$Give Your .NET-based Application a Fast and Responsive UI with Multiple
Threads
http://msdn.microsoft.com/msdnmag/issues/03/02/Multithreading/

#Making a Rich Client Smart : Using Multiple Threads
http://blogs.msdn.com/dphill/archive/2004/08/12/183503.aspx


Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)




--------------------
| From: "Joe" <[email protected]>
| References: <[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
| Subject: Re: UserControl always run in seperate thread
| Date: Mon, 16 Jan 2006 16:37:05 -0500
| Lines: 32
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
| X-RFC2646: Format=Flowed; Original
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: 69.37.58.70.adsl.snet.net 69.37.58.70
| Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
| Xref: TK2MSFTNGXA02.phx.gbl
microsoft.public.dotnet.languages.csharp:378903
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| Hi Willy,
|
| The problem is the spinning wheel is just an animated GIF. I want to show
my
| UserControl in a panel (which is in the main thread) and allow the GIF to
| remain animated.
|
| I'm not sure how to use BeginInvoke to work in this case.
|
| -Joe
|
| | >
| > | > | Hi Josh,
| > |
| > | Actually, all the control does is display a spinning wheel (like in
SQL
| > | 2005) when something is happening which will take a while - sort of
like
| > a
| > | progress bar and displays a message like Please wait...
| > |
| >
| > That 'something' should be done on a separate background thread, the
| > updating of the UI (your spinning wheel) should be done using
| > Control.BeginInvoke or Control.Invoke.
| >
| > Willy.
| >
| >
|
|
|
 
Back
Top