Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / March 2006

Tip: Looking for answers? Try searching our database.

BackgroundWorker Capability For Non UI Classes

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Brian R. - 15 Mar 2006 07:04 GMT
I like the concept and features of the BackgroundWorker.  However, I observed
that it will only work for clients that implement ISynchronizeInvoke (namely
forms and controls).  My instinct is that BeginInvoke uses windows message
loops to synchronize back to the main (UI) thread.

I want this same capability in a business level component so that it can
fire client delegates on the main (UI) thread if it did some asynchronous
work on a background thread, but I don't want to have to force it to carry
around a full 'System.Windows.Forms' form so that I can kludge the
BeginInvoke methods.  

Is there a ligthweight WindowImpl that I can programmatically use to post
messages to and handle the callback in a WndProc on the main UI?  In ATL I
would create a simple CWindow and host it without a resource file for it or a
visible window.  I could receive messages from it and use it to perform
worker thread synch.

Is there anythign comparable in .NET?  Or must I carry around the weight of
a complete System.Windows.Form?

Thanks,

Signature

Brian R.

Dmytro Lapshyn [MVP] - 15 Mar 2006 09:48 GMT
Hi Brian,

I used a plain System.Windows.Forms.Control instance to marshal async
callbacks to the UI thread. You can derive from the Control class and create
a Singleton that all the business components will be aware of. It is not
necessary to make the control visible in any way, but you should ask for its
handle to make the control create the owned child window.

>I like the concept and features of the BackgroundWorker.  However, I
>observed
[quoted text clipped - 21 lines]
>
> Thanks,
Brian R. - 15 Mar 2006 17:38 GMT
Thanks,  I will give this a try as well.

Signature

Brian R.

> Hi Brian,
>
[quoted text clipped - 29 lines]
> >
> > Thanks,
Stoitcho Goutsev (100) - 15 Mar 2006 16:11 GMT
Brian,

BackgroundWorker doesn't require any control's to be created. If the thread
that calls the RunWorkerAsync has a message loop runing at the moment of the
method call the BackgroundWorker will marshal correctly the ProgressrReport
and RunWorkerCompleted events to the UI thread. It doesn't matter if there
are any forms or controls created.

Actually unless you use PInvoke I don't see how a managed application can
create a UI thread (run the message pump) without using the Application
class, which is part of System.Windows.Forms.dll.

One last thing. I don't understand your worries about the windows forms dll.
It is already installed on the machine as part of the .NET framework.

Signature

HTH
Stoitcho Goutsev (100)

>I like the concept and features of the BackgroundWorker.  However, I
>observed
[quoted text clipped - 21 lines]
>
> Thanks,
Brian R. - 15 Mar 2006 17:38 GMT
The main thread does have a UI, however the main message pump is a Visual
Basic 6.0 implementation.  Our system is a COM based plugin architecture and
we are using interop to develop .NET based components.  

The invoking component is on the main thread and, hence, does have a UI,
however I was concerned that it wasn't a .NET message pump and the client
that invokes the BackgroundWorker is merely a .NET class with no UI
components.

Are you confident that the call would be marshalled back to the main UI in
this instance.

Also, I wasn't concerned about loading or distributing System.Windows.Forms,
I was more concerned about having to show a form or keep a form around when
it seemed like more code and capability than needed.

Thanks,

Signature

Brian R.

> Brian,
>
[quoted text clipped - 36 lines]
> >
> > Thanks,
Stoitcho Goutsev (100) - 15 Mar 2006 22:58 GMT
Brian,

I was looking at the code of the BackgroundWorker. There is nothing that
uses any windows forms control unlike the implementation of
System.Timers.Timer class where the sychronization is based on
ISychornizeInvoke interface (usualy controls).

BackgroundWorker uses the AsyncOperationManager class and works without
controls, but the thread has to have  running pump. I believe that it still
uses windows messages but it post them directly to the thread not to any
window.

I tested it with message loop and no controls, but the message loop was
created using the Application class. I don't know if it works with UI
threads where the message loop is created in some other way. You should give
it a try. I can't set up such an environment easy enough in order to test
it.

Signature

Stoitcho Goutsev (100)

> The main thread does have a UI, however the main message pump is a Visual
> Basic 6.0 implementation.  Our system is a COM based plugin architecture
[quoted text clipped - 70 lines]
>> >
>> > Thanks,

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.