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 / .NET Framework / New Users / October 2006

Tip: Looking for answers? Try searching our database.

Thread marshalling - changing a control's thread affinity

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Martin Maat - 10 Oct 2006 20:20 GMT
Hi,

I have a notification window that uses its own thread to animate itself. It
also fires events to the main UI.

The trouble (getting it thread safe) is not with the events fired, I can
marhall them just fine using (Begin)Invoke. It is with the internal thread
which updates the form itself. The form however was not created by this
animation thread which is only created later, after construction of the
form. It was created by the main UI thread.

In framework 1.1 there was no problem and with framework 2.0 I can just set
Control.CheckForIllegalCrossThreadCalls to false and all is fine again. I
tried to be a good boy though, just to be sure there will be no surprises,
to make it thread-safe.

Marshalling every call to the form's message loop using (Begin)Invoke is
pointless, it defeats the purpose of having a separate animation thread.
Animation got jerky, stalling the main UI thread. So I basically wanted the
form to be created on the animation thread itself.

I may be stupid but it took me a couple of days to come to terms with the
fact that this is impossible. It is like lifting yourself up by your shoe
laces.

Now I was thinking... can I not marshall the control to another thread
instead of marshalling each call to the appropriate thread?

Like a control is created on thread A. Then, when in thread B, I want to
mess with it. Then I want to be able to tell Windows "Hey, this control
which is owned by thread A, please consider it owned by thread B from now
on". And then call its methods from thread B.

This would be so much more convenient in my scenario. Can it be done?

Regards, Martin.
Dmytro Lapshyn [MVP] - 11 Oct 2006 08:59 GMT
Hey Martin,

Having two UI threads (even if one of them is used for animation) is
generally a bad idea. What prevents you from running the animation on the
main UI thread? If it's some heavy-duty processing that happens on the UI
thread, you should rather "offload" that processing to worker threads.

Signature

Regards,
Dmytro Lapshyn [MVP]
http://blogs.vbcity.com/DmytroL

> Hi,
>
[quoted text clipped - 32 lines]
>
> Regards, Martin.
Martin Maat - 12 Oct 2006 20:54 GMT
> Having two UI threads (even if one of them is used for animation) is
> generally a bad idea. What prevents you from running the animation on
> the main UI thread?

The resulting performance does. The application is an RSS reader. You may be
browsing articles using the main UI (thread) when the notification pane
shifts (or fades) in. This is an expensive operation that I do not want to
be performed on the main UI thread because it totally kills responsivenes
for the time the animation takes (I tried it, it sucks). Currently animation
is performed on a separate worker thread and everything performes really
smooth. It is only for the warnings I get about how this COULD one day
result in error because there is no guarantee Windows will not interfere at
an inconvenient moment, that I want to do it in a way that would be
absolutely safe. I never had any issues with it so far.

So if the CLR / Windows.Forms people urge me to do it "right" it would be
nice if they could also tell me what right is for my scenario which seems a
pretty common one. And performing animation on the main UI thread is not
good enough.

> If it's some heavy-duty processing that happens
> on the UI thread, you should rather "offload" that processing to
> worker threads.

That is exactly the case, hence my question.

Martin.

>> Hi,
>>
[quoted text clipped - 32 lines]
>>
>> Regards, Martin.
Mattias Sjögren - 12 Oct 2006 22:44 GMT
>So I basically wanted the
>form to be created on the animation thread itself.
>
>I may be stupid but it took me a couple of days to come to terms with the
>fact that this is impossible. It is like lifting yourself up by your shoe
>laces.

That sounds like the way to go. Why do you say it's impossible? First
create the thread, then instantiate the form from there.

Mattias

Signature

Mattias Sjögren [C# MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.


Rate this thread:







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.