473,799 Members | 3,106 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

scary (but good) .NET 2.0 feature

All,

I was just reading over a new MSDN Magazine article by Juval Lowy on the
new features in C# 2.0 (its a wonderful article, see link below.) In
the section titled "Anonymous Method Example" he makes reference to an
aspect of .NET 2.0 that may have a significant impact on many of your
applications, if they are multi-threaded and have a user interface.

Now we all know that the right way to update any parts of the user
interface in a multi-threaded application is to marshal the call to the
thread that created the interface control and perform the updates there.
But (be honest now) how many of us really do that for 100% of our calls?
I know I don't (maybe I'm the only one??). Apparently .NET 2.0 will
force you to do this - throwing an exception if you don't.

Note that I'm not complaining - this is surely a good thing for program
realibility. I'm just giving a heads up to those of us that tend to
relax the rules on occasion... :)

However this does bring up a question - how do most people deal with
this? Specifically...

When I do things the right way, things seem a bit cumbersome. I need a
delegate and a separate method for any function which updates the
interface.

So now I have to "control.Invoke (delegate, new object[] { ... });" which
seems to (a) make the code difficult to read (b) creates object[]'s all
over the place.

Now I've only been programming .NET for about a year, but I haven't seen
anything to indicate if there is there a better way to do it? And more
to the point, why didn't the .NET framework programmers build in thread
safety into the .NET WinForms components??

Link to article by Juval Lowy:
http://msdn.microsoft.com/msdnmag/is...0/default.aspx

-mdb
Nov 16 '05 #1
6 1272
> why didn't the .NET framework programmers build in thread
safety into the .NET WinForms components??
How? By throwing exceptions or otherwise ???
--
Regards,
Alvin Bruney [ASP.NET MVP]
Got tidbits? Get it here...
http://tinyurl.com/27cok
"Michael Bray" <mb************ *******@SkPiAlM l.ctiusa.com> wrote in message
news:Xn******** *************** ***********@207 .46.248.16...
All,

I was just reading over a new MSDN Magazine article by Juval Lowy on the
new features in C# 2.0 (its a wonderful article, see link below.) In
the section titled "Anonymous Method Example" he makes reference to an
aspect of .NET 2.0 that may have a significant impact on many of your
applications, if they are multi-threaded and have a user interface.

Now we all know that the right way to update any parts of the user
interface in a multi-threaded application is to marshal the call to the
thread that created the interface control and perform the updates there.
But (be honest now) how many of us really do that for 100% of our calls?
I know I don't (maybe I'm the only one??). Apparently .NET 2.0 will
force you to do this - throwing an exception if you don't.

Note that I'm not complaining - this is surely a good thing for program
realibility. I'm just giving a heads up to those of us that tend to
relax the rules on occasion... :)

However this does bring up a question - how do most people deal with
this? Specifically...

When I do things the right way, things seem a bit cumbersome. I need a
delegate and a separate method for any function which updates the
interface.

So now I have to "control.Invoke (delegate, new object[] { ... });" which
seems to (a) make the code difficult to read (b) creates object[]'s all
over the place.

Now I've only been programming .NET for about a year, but I haven't seen
anything to indicate if there is there a better way to do it? And more
to the point, why didn't the .NET framework programmers build in thread
safety into the .NET WinForms components??

Link to article by Juval Lowy:
http://msdn.microsoft.com/msdnmag/is...0/default.aspx

-mdb

Nov 16 '05 #2
Michael Bray wrote:
All,

I was just reading over a new MSDN Magazine article by Juval Lowy on the
new features in C# 2.0 (its a wonderful article, see link below.) In
the section titled "Anonymous Method Example" he makes reference to an
aspect of .NET 2.0 that may have a significant impact on many of your
applications, if they are multi-threaded and have a user interface.

Now we all know that the right way to update any parts of the user
interface in a multi-threaded application is to marshal the call to the
thread that created the interface control and perform the updates there.
But (be honest now) how many of us really do that for 100% of our calls?
I know I don't (maybe I'm the only one??). Apparently .NET 2.0 will
force you to do this - throwing an exception if you don't.

Note that I'm not complaining - this is surely a good thing for program
realibility. I'm just giving a heads up to those of us that tend to
relax the rules on occasion... :)

However this does bring up a question - how do most people deal with
this? Specifically...

When I do things the right way, things seem a bit cumbersome. I need a
delegate and a separate method for any function which updates the
interface.

So now I have to "control.Invoke (delegate, new object[] { ... });" which
seems to (a) make the code difficult to read (b) creates object[]'s all
over the place.

Now I've only been programming .NET for about a year, but I haven't seen
anything to indicate if there is there a better way to do it? And more
to the point, why didn't the .NET framework programmers build in thread
safety into the .NET WinForms components??

Link to article by Juval Lowy:
http://msdn.microsoft.com/msdnmag/is...0/default.aspx


I find it a real pain in the backside, and I usually work around it by
making another class, whose purpose is to marshall things.

This class has, as part of its state, a reference to the relevant
form(s) that need to be updated. It responds to events from the
specific object by calling Invoke() on the relevant form.
Nov 16 '05 #3
"Alvin Bruney [MVP]" <vapor at steaming post office> wrote in
news:eM******** ******@TK2MSFTN GP12.phx.gbl:
why didn't the .NET framework programmers build in thread

safety into the .NET WinForms components??
How? By throwing exceptions or otherwise ???


Just as he describes for the 'SafeLabel' in his article. For example,
they could have done something like:

public class Label : Control
{
....
private string text;
public string Text
{
get { return text; }
set
{
if (InvokeRequired )
{
... (marshal the call)
}
else
{
... (set it directly)
}
}
}
}

but apparently they didn't do that.

-mdb
Nov 16 '05 #4
"Michael Bray" <mb************ *******@SkPiAlM l.ctiusa.com> wrote in message
news:Xn******** *************** ***********@207 .46.248.16...
I was just reading over a new MSDN Magazine article by Juval Lowy on the
new features in C# 2.0 (its a wonderful article, see link below.) In
the section titled "Anonymous Method Example" he makes reference to an
aspect of .NET 2.0 that may have a significant impact on many of your
applications, if they are multi-threaded and have a user interface.

Now we all know that the right way to update any parts of the user
interface in a multi-threaded application is to marshal the call to the
thread that created the interface control and perform the updates there.
But (be honest now) how many of us really do that for 100% of our calls?
I, for one. I don't see any reason to touch UI directly. It's asking for
trouble.
I know I don't (maybe I'm the only one??). Apparently .NET 2.0 will
force you to do this - throwing an exception if you don't.

Note that I'm not complaining - this is surely a good thing for program
realibility. I'm just giving a heads up to those of us that tend to
relax the rules on occasion... :)

However this does bring up a question - how do most people deal with
this? Specifically...
delegate + invoke
When I do things the right way, things seem a bit cumbersome. I need a
delegate and a separate method for any function which updates the
interface.
So? What is wrong with that?
So now I have to "control.Invoke (delegate, new object[] { ... });" which
seems to (a) make the code difficult to read (b) creates object[]'s all
over the place.

Now I've only been programming .NET for about a year, but I haven't seen
anything to indicate if there is there a better way to do it? And more
to the point, why didn't the .NET framework programmers build in thread
safety into the .NET WinForms components??


Probably because that would make UI code slower in general.

Tom.
Nov 16 '05 #5


"Michael Bray" <mb************ *******@SkPiAlM l.ctiusa.com> wrote in message
news:Xn******** *************** ***********@207 .46.248.16...
All,

I was just reading over a new MSDN Magazine article by Juval Lowy on the
new features in C# 2.0 (its a wonderful article, see link below.) In
the section titled "Anonymous Method Example" he makes reference to an
aspect of .NET 2.0 that may have a significant impact on many of your
applications, if they are multi-threaded and have a user interface.

Now we all know that the right way to update any parts of the user
interface in a multi-threaded application is to marshal the call to the
thread that created the interface control and perform the updates there.
But (be honest now) how many of us really do that for 100% of our calls?
I know I don't (maybe I'm the only one??). Apparently .NET 2.0 will
force you to do this - throwing an exception if you don't.

Note that I'm not complaining - this is surely a good thing for program
realibility. I'm just giving a heads up to those of us that tend to
relax the rules on occasion... :)

However this does bring up a question - how do most people deal with
this? Specifically...

When I do things the right way, things seem a bit cumbersome. I need a
delegate and a separate method for any function which updates the
interface.

So now I have to "control.Invoke (delegate, new object[] { ... });" which
seems to (a) make the code difficult to read (b) creates object[]'s all
over the place.

Now I've only been programming .NET for about a year, but I haven't seen
anything to indicate if there is there a better way to do it? And more
to the point, why didn't the .NET framework programmers build in thread
safety into the .NET WinForms components??

Link to article by Juval Lowy:
http://msdn.microsoft.com/msdnmag/is...0/default.aspx

-mdb


..NET 2.0 also introduces a new class in the Windows.Forms namespace called
BackgroundWorke r that handles all UI delegation/marshalling for you.

Willy.
Nov 16 '05 #6
Willy Denoyette [MVP] wrote:
"Michael Bray" <mb************ *******@SkPiAlM l.ctiusa.com> wrote in
message news:Xn******** *************** ***********@207 .46.248.16... [...] .NET 2.0 also introduces a new class in the Windows.Forms namespace
called BackgroundWorke r that handles all UI delegation/marshalling
for you.


And Juwal Löwy even provides a .NET 1.1 implementation on the iDesign web
site -- see
http://www.idesign.net/idesign/Deskt...ndex=5&tabid=8

Cheers,

--
Joerg Jooss
jo*********@gmx .net

Nov 16 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1096
by: Damodar Periwal | last post by:
-------------------------------------------------------------- Software Tree Revs Up JDX OR-Mapper With Innovative And High-Performance Features -------------------------------------------------------------- Software Tree has announced JDX 4.5, the versatile and patented Object-Relational Mapping (OR-Mapping) software that significantly accelerates the development of Java/J2EE applications by eliminating tedious, low-level SQL coding...
72
4857
by: The Plankmeister | last post by:
Is doing this bad: <h3>Some title or other...<a href="#pagetop">back to top</a></h3> I have a feeling it is... My problem is I'm using CSS to style the H3 into a block that spans the whole containing element. I would like the <a> to appear next to the title, but I'm sure this is bad practice (for screen readers and heading-level navigation etc etc) So... is it acceptable to do this:
15
2668
by: Jam Pa | last post by:
I would like to hear recommendations on good CSS editors. Personally, I would like a CSS editor with 'code-completion'. In effect, I would like to be able to see different properties and their possible values as I type, or at least next to the css-code window in a reference window. I have tried Style Master, but did not like it at all, it wasn't fast enough, and not 'pro' enough for a text-editor-kinda CSS designer like me.
10
1414
by: sam | last post by:
hi, i am new to C/C++ and have just finished a program based on the UK national lottery, where you enter 6 numbers, six are generated by the computer and there are appropriate messages and a compare function to decide your winnings. I am using Microsoft's C++ version 6 standard, the program is as follows: /* * * * * * * * * * * * * * * * * * * * * * * Lottery Simulation By Sam...
0
1158
by: Damodar Periwal | last post by:
-------------------------------------------------------------- Software Tree Revs Up JDX OR-Mapper With Innovative And High-Performance Features -------------------------------------------------------------- Software Tree has announced JDX 4.5, the versatile and patented Object-Relational Mapping (OR-Mapping) software that significantly accelerates the development of Java/J2EE applications by eliminating tedious, low-level SQL coding...
18
1845
by: Damodar Periwal | last post by:
-------------------------------------------------------------- Software Tree Revs Up JDX OR-Mapper With Innovative And High-Performance Features -------------------------------------------------------------- Software Tree has announced JDX 4.5, the versatile and patented Object-Relational Mapping (OR-Mapping) software that significantly accelerates the development of Java/J2EE applications by eliminating tedious, low-level SQL coding...
150
6589
by: tony | last post by:
If you have any PHP scripts which will not work in the current releases due to breaks in backwards compatibility then take a look at http://www.tonymarston.net/php-mysql/bc-is-everything.html and see if you agree with my opinion or not. Tony Marston http://www.tonymarston.net
165
6920
by: Dieter | last post by:
Hi. In the snippet of code below, I'm trying to understand why when the struct dirent ** namelist is declared with "file" scope, I don't have a problem freeing the allocated memory. But when the struct is declared in main (block scope) it will segfault when passing namelist to freeFileNames().
0
9687
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9541
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10251
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10228
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9072
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5463
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3759
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.