473,382 Members | 1,353 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Threading changes in .NET 2

Can anyone explain if there are any changes or improvements to the
threading system in .NET 2? I haven't found any in the "What's new"
webpage but have heard some improvements and changes have been made
(but nothing more specific than that)

Mar 22 '06 #1
12 2229
Chris S. wrote:
Can anyone explain if there are any changes or improvements to the
threading system in .NET 2? I haven't found any in the "What's new"
webpage but have heard some improvements and changes have been made
(but nothing more specific than that)


the only thing that comes to mind immediately is that you can set the
minimum and maximum number of threads in the default threadpool. so
rather than the max threads available being 25x(processor cores in the
system) you can set the max to, for example, 4.

this probably extends to any threadpool you create as well.

its early in my neck of the woods. that's all i can think of right now.
Mar 22 '06 #2
A related enhancement is the introduction of the BackgroundWorker component
for Windows Forms. Because Windows Forms are STA, threading has always been
problematic with them. While the BackgroundWorker class is not an
enhancement of the Threading model, it certainly comes in handy when one
needs to perform threaded tasks with Windows Forms.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Show me your certification without works,
and I'll show my certification
*by* my works.

"jeremiah johnson" <na*******@gmail.com> wrote in message
news:OX**************@TK2MSFTNGP09.phx.gbl...
Chris S. wrote:
Can anyone explain if there are any changes or improvements to the
threading system in .NET 2? I haven't found any in the "What's new"
webpage but have heard some improvements and changes have been made
(but nothing more specific than that)


the only thing that comes to mind immediately is that you can set the
minimum and maximum number of threads in the default threadpool. so
rather than the max threads available being 25x(processor cores in the
system) you can set the max to, for example, 4.

this probably extends to any threadpool you create as well.

its early in my neck of the woods. that's all i can think of right now.

Mar 22 '06 #3
I've read on this thread:

http://groups.google.co.uk/group/mic...52b054bb9ac568

about .NET 2.0 throwing exceptions with Control.Invoke. This is
probably the threading changes I was told about. But doesn't .NET 1.1
do this already when the thread doing the UI update hasn't got a handle
(and no InvokeRequired is used)?

Mar 22 '06 #4
Chris,

In addition to forms and controls not being thread-safe they also have
thread affinity requirements. They must only be accessed on the thread
that created them. Typically, that's the main UI thread. In .NET 1.0
and 1.1 you could access the control from another thread and it would
appear to work fine at least some of the time. However, the behavior
was unpredictable. I frequently observed the problems manifesting
themselves when a control would unexpectedly appear as a large red X
with a white background. In .NET 2.0 Microsoft decided to throw an
exception if a control was accessed from another thread immediately
informing the developer of a bug. That may be the exception you're
thinking of, but it isn't thrown by Control.Invoke.

Brian

Chris S. wrote:
I've read on this thread:

http://groups.google.co.uk/group/mic...52b054bb9ac568

about .NET 2.0 throwing exceptions with Control.Invoke. This is
probably the threading changes I was told about. But doesn't .NET 1.1
do this already when the thread doing the UI update hasn't got a handle
(and no InvokeRequired is used)?


Mar 22 '06 #5
Fixed a bug in Monitor to throw AbandonedMutexException as it should.
Semaphore class added. Interlocked class adds generic stuff,
ParameterizedThreadStart delegate added to Thread class, TP updates. I am
sure there are many more.

--
William Stacey [MVP]

"Chris S." <sl********@gmail.com> wrote in message
news:11*********************@e56g2000cwe.googlegro ups.com...
| Can anyone explain if there are any changes or improvements to the
| threading system in .NET 2? I haven't found any in the "What's new"
| webpage but have heard some improvements and changes have been made
| (but nothing more specific than that)
|
Mar 22 '06 #6
That is an interesting question. Where would they throw that error? Would
seem inside an enqueue method for the window message queue would be a good
single place. Control.Invoke/BeginInvoke must eventually call into some
enqueue logic I would think.

--
William Stacey [MVP]

"Brian Gideon" <br*********@yahoo.com> wrote in message
news:11*********************@e56g2000cwe.googlegro ups.com...
| Chris,
|
| In addition to forms and controls not being thread-safe they also have
| thread affinity requirements. They must only be accessed on the thread
| that created them. Typically, that's the main UI thread. In .NET 1.0
| and 1.1 you could access the control from another thread and it would
| appear to work fine at least some of the time. However, the behavior
| was unpredictable. I frequently observed the problems manifesting
| themselves when a control would unexpectedly appear as a large red X
| with a white background. In .NET 2.0 Microsoft decided to throw an
| exception if a control was accessed from another thread immediately
| informing the developer of a bug. That may be the exception you're
| thinking of, but it isn't thrown by Control.Invoke.
|
| Brian
|
| Chris S. wrote:
| > I've read on this thread:
| >
| >
http://groups.google.co.uk/group/mic...52b054bb9ac568
| >
| > about .NET 2.0 throwing exceptions with Control.Invoke. This is
| > probably the threading changes I was told about. But doesn't .NET 1.1
| > do this already when the thread doing the UI update hasn't got a handle
| > (and no InvokeRequired is used)?
|
Mar 22 '06 #7
William,

I don't have 2.0 on this computer so I can't test it right now, but I
think it's thrown in the calling thread upon invoking a property or
method. The exception was something like "illegal cross thread
operation...". I don't think Control.Invoke or the message queue play
role in this.

Brian

William Stacey [MVP] wrote:
That is an interesting question. Where would they throw that error? Would
seem inside an enqueue method for the window message queue would be a good
single place. Control.Invoke/BeginInvoke must eventually call into some
enqueue logic I would think.

--
William Stacey [MVP]

"Brian Gideon" <br*********@yahoo.com> wrote in message
news:11*********************@e56g2000cwe.googlegro ups.com...
| Chris,
|
| In addition to forms and controls not being thread-safe they also have
| thread affinity requirements. They must only be accessed on the thread
| that created them. Typically, that's the main UI thread. In .NET 1.0
| and 1.1 you could access the control from another thread and it would
| appear to work fine at least some of the time. However, the behavior
| was unpredictable. I frequently observed the problems manifesting
| themselves when a control would unexpectedly appear as a large red X
| with a white background. In .NET 2.0 Microsoft decided to throw an
| exception if a control was accessed from another thread immediately
| informing the developer of a bug. That may be the exception you're
| thinking of, but it isn't thrown by Control.Invoke.
|
| Brian
|


Mar 22 '06 #8
Note that this is only true when running in debug mode, there is no such
check done when running a release build, so you won't get the exception. The
check is done by the CLR (where it's called a Managed Debugging Assistant or
MDA) and is quite expensive as it has to compare the thread ID of the thread
that accesses a window handle to the thread ID of the creator (the UI
thread) of the handle, that's why it's only done when debugging or when you
explicitly enable the MDA. Search the docs for MDA if you want to enable
this (or other MDA's) for release builds as well.
Willy.
"Brian Gideon" <br*********@yahoo.com> wrote in message
news:11*********************@e56g2000cwe.googlegro ups.com...
| Chris,
|
| In addition to forms and controls not being thread-safe they also have
| thread affinity requirements. They must only be accessed on the thread
| that created them. Typically, that's the main UI thread. In .NET 1.0
| and 1.1 you could access the control from another thread and it would
| appear to work fine at least some of the time. However, the behavior
| was unpredictable. I frequently observed the problems manifesting
| themselves when a control would unexpectedly appear as a large red X
| with a white background. In .NET 2.0 Microsoft decided to throw an
| exception if a control was accessed from another thread immediately
| informing the developer of a bug. That may be the exception you're
| thinking of, but it isn't thrown by Control.Invoke.
|
| Brian
|
| Chris S. wrote:
| > I've read on this thread:
| >
| >
http://groups.google.co.uk/group/mic...52b054bb9ac568
| >
| > about .NET 2.0 throwing exceptions with Control.Invoke. This is
| > probably the threading changes I was told about. But doesn't .NET 1.1
| > do this already when the thread doing the UI update hasn't got a handle
| > (and no InvokeRequired is used)?
|
Mar 22 '06 #9
Willy,

I didn't realize that. That's good to know. Thanks for posting that
note.

Brian

Willy Denoyette [MVP] wrote:
Note that this is only true when running in debug mode, there is no such
check done when running a release build, so you won't get the exception. The
check is done by the CLR (where it's called a Managed Debugging Assistant or
MDA) and is quite expensive as it has to compare the thread ID of the thread
that accesses a window handle to the thread ID of the creator (the UI
thread) of the handle, that's why it's only done when debugging or when you
explicitly enable the MDA. Search the docs for MDA if you want to enable
this (or other MDA's) for release builds as well.
Willy.
"Brian Gideon" <br*********@yahoo.com> wrote in message
news:11*********************@e56g2000cwe.googlegro ups.com...
| Chris,
|
| In addition to forms and controls not being thread-safe they also have
| thread affinity requirements. They must only be accessed on the thread
| that created them. Typically, that's the main UI thread. In .NET 1.0
| and 1.1 you could access the control from another thread and it would
| appear to work fine at least some of the time. However, the behavior
| was unpredictable. I frequently observed the problems manifesting
| themselves when a control would unexpectedly appear as a large red X
| with a white background. In .NET 2.0 Microsoft decided to throw an
| exception if a control was accessed from another thread immediately
| informing the developer of a bug. That may be the exception you're
| thinking of, but it isn't thrown by Control.Invoke.
|
| Brian
|


Mar 22 '06 #10
Thanks Willy. Which one of the MDAs does this? I looked at the list and
could not find one that seemed to match.

--
William Stacey [MVP]

"Willy Denoyette [MVP]" <wi*************@telenet.be> wrote in message
news:e8**************@TK2MSFTNGP12.phx.gbl...
| Note that this is only true when running in debug mode, there is no such
| check done when running a release build, so you won't get the exception.
The
| check is done by the CLR (where it's called a Managed Debugging Assistant
or
| MDA) and is quite expensive as it has to compare the thread ID of the
thread
| that accesses a window handle to the thread ID of the creator (the UI
| thread) of the handle, that's why it's only done when debugging or when
you
| explicitly enable the MDA. Search the docs for MDA if you want to enable
| this (or other MDA's) for release builds as well.
|
|
| Willy.
|
|
| "Brian Gideon" <br*********@yahoo.com> wrote in message
| news:11*********************@e56g2000cwe.googlegro ups.com...
|| Chris,
||
|| In addition to forms and controls not being thread-safe they also have
|| thread affinity requirements. They must only be accessed on the thread
|| that created them. Typically, that's the main UI thread. In .NET 1.0
|| and 1.1 you could access the control from another thread and it would
|| appear to work fine at least some of the time. However, the behavior
|| was unpredictable. I frequently observed the problems manifesting
|| themselves when a control would unexpectedly appear as a large red X
|| with a white background. In .NET 2.0 Microsoft decided to throw an
|| exception if a control was accessed from another thread immediately
|| informing the developer of a bug. That may be the exception you're
|| thinking of, but it isn't thrown by Control.Invoke.
||
|| Brian
||
|| Chris S. wrote:
|| > I've read on this thread:
|| >
|| >
|
http://groups.google.co.uk/group/mic...52b054bb9ac568
|| >
|| > about .NET 2.0 throwing exceptions with Control.Invoke. This is
|| > probably the threading changes I was told about. But doesn't .NET 1.1
|| > do this already when the thread doing the UI update hasn't got a handle
|| > (and no InvokeRequired is used)?
||
|
|
Mar 23 '06 #11
Well, it looks like this MDA was cut from the list and can no longer be
controlled from outside a managed debugger like msdb or vs debugger. That
means that this MDA is on by-default under the debugger and off otherwise,
you can no longer control this through an environment variable, registry or
config file setting.
Others that were cut (though still in the list), are PInvokeLog and
QueryInterface MDA. It looks like these (and probably some other too) have
been removed very late in the beta process, I've usefully used the
Illegalcrossthreadcall MDA in beta2. I need to spend some time on this to
find out what still available in the relase version of V2.

Willy.
"William Stacey [MVP]" <wi************@gmail.com> wrote in message
news:uD**************@TK2MSFTNGP09.phx.gbl...
| Thanks Willy. Which one of the MDAs does this? I looked at the list and
| could not find one that seemed to match.
|
| --
| William Stacey [MVP]
|
| "Willy Denoyette [MVP]" <wi*************@telenet.be> wrote in message
| news:e8**************@TK2MSFTNGP12.phx.gbl...
|| Note that this is only true when running in debug mode, there is no such
|| check done when running a release build, so you won't get the exception.
| The
|| check is done by the CLR (where it's called a Managed Debugging Assistant
| or
|| MDA) and is quite expensive as it has to compare the thread ID of the
| thread
|| that accesses a window handle to the thread ID of the creator (the UI
|| thread) of the handle, that's why it's only done when debugging or when
| you
|| explicitly enable the MDA. Search the docs for MDA if you want to enable
|| this (or other MDA's) for release builds as well.
||
||
|| Willy.
||
||
|| "Brian Gideon" <br*********@yahoo.com> wrote in message
|| news:11*********************@e56g2000cwe.googlegro ups.com...
||| Chris,
|||
||| In addition to forms and controls not being thread-safe they also have
||| thread affinity requirements. They must only be accessed on the thread
||| that created them. Typically, that's the main UI thread. In .NET 1.0
||| and 1.1 you could access the control from another thread and it would
||| appear to work fine at least some of the time. However, the behavior
||| was unpredictable. I frequently observed the problems manifesting
||| themselves when a control would unexpectedly appear as a large red X
||| with a white background. In .NET 2.0 Microsoft decided to throw an
||| exception if a control was accessed from another thread immediately
||| informing the developer of a bug. That may be the exception you're
||| thinking of, but it isn't thrown by Control.Invoke.
|||
||| Brian
|||
||| Chris S. wrote:
||| > I've read on this thread:
||| >
||| >
||
|
http://groups.google.co.uk/group/mic...52b054bb9ac568
||| >
||| > about .NET 2.0 throwing exceptions with Control.Invoke. This is
||| > probably the threading changes I was told about. But doesn't .NET 1.1
||| > do this already when the thread doing the UI update hasn't got a
handle
||| > (and no InvokeRequired is used)?
|||
||
||
|
|
Mar 23 '06 #12
Willy,

I tested this last night and I verified that the exception does indeed
occur in debug builds, but not release builds as you already indicated.
I'll be honest. I didn't even know MDAs existed until you said
something. I can see a lot of potential uses for this. Is there any
possibility that Microsoft would open this functionality up to us. I'd
like to add my own MDAs for use in some class libraries. The wheels
are spinning in my head right now...

Brian

Willy Denoyette [MVP] wrote:
Well, it looks like this MDA was cut from the list and can no longer be
controlled from outside a managed debugger like msdb or vs debugger. That
means that this MDA is on by-default under the debugger and off otherwise,
you can no longer control this through an environment variable, registry or
config file setting.
Others that were cut (though still in the list), are PInvokeLog and
QueryInterface MDA. It looks like these (and probably some other too) have
been removed very late in the beta process, I've usefully used the
Illegalcrossthreadcall MDA in beta2. I need to spend some time on this to
find out what still available in the relase version of V2.

Willy.


Mar 23 '06 #13

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

Similar topics

3
by: Suhail Salman | last post by:
Dear All, i got the following error message in an applications that opens 13 threads and all of them calling a stored procedure which retreieves data from SQLServer and fills them to a table the...
4
by: Ryan Gregg | last post by:
I've got a background service running that needs to check a database periodically and print reports whenever it finds that the data has changed. I've built a class library that scans the database...
8
by: Z D | last post by:
Hello, I'm having a strange problem that is probably due to my lack of understanding of how threading & COM Interop works in a WinForms.NET application. Here's the situation: I have a 3rd...
0
by: OpticTygre | last post by:
I've been reading some things about threading, delegates, threadpools, locks, etc... yet I can't quite seem to grasp some of the concepts on it quite yet. I'm currently working on a project I need...
6
by: hzgt9b | last post by:
Using VS 2003, .NET: I developed a windows application that performs several actions based on an input file. The application displays a progress bar as each action executes. Based on new...
2
by: Juuso Hukkanen | last post by:
I need a list of multithreading unsafe C (C99) functions/features. comp.programming.threads provided an initial list of C:ish functions, with following ANSI C functions: asctime, gmtime,...
9
by: cgwalters | last post by:
Hi, I've recently been working on an application which does quite a bit of searching through large data structures and string matching, and I was thinking that it would help to put some of this...
126
by: Dann Corbit | last post by:
Rather than create a new way of doing things: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2497.html why not just pick up ACE into the existing standard:...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.