If you have developed traditional Windows Client/Server applications on single-CPU machines for all your life the development of thread-safe code is probably something you did not had to bother.
Here are a few interesting links on how to do it correctly:
- .NET Framework General Reference: Threading Design Guidelines
- Implementing the Singleton Pattern in C#
- Choosing What To Lock On
- Brad Abrams: volatile and MemoryBarrier()...
- Concurrent Affairs: Build a Richer Thread Synchronization Lock -- MSDN Magazine, March 2006
- .NET Matters: Abortable Thread Pool -- MSDN Magazine, March 2006
- Memory Models: Understand the Impact of Low-Lock Techniques in Multithreaded Apps -- MSDN Magazine, October 2005
- Concurrent Affairs: Performance-Conscious Thread Synchronization -- MSDN Magazine, October 2005
- Concurrency: What Every Dev Must Know About Multithreaded Apps -- MSDN Magazine, August 2005
- Make It Snappy: Juice Up Your App with the Power of Hyper-Threading -- MSDN Magazine, June 2005
- Basic Instincts: Thread Synchronization -- MSDN Magazine, September 2004
- Basic Instincts: Creating and Managing Secondary Threads -- MSDN Magazine, June 2004
- Windows Forms: Give Your .NET-based Application a Fast and Responsive UI with Multiple Threads -- MSDN Magazine, February 2003
- Basic Instincts: Updating the UI from a Secondary Thread -- MSDN Magazine, May 2004
- .NET: Practical Multithreading for Client Apps -- MSDN Magazine, January 2004
Further must-reads? Leave a comment, please!
Look at this post by Randy Miller how the C# test team tested Visual C# 2005.
TAO: Test Automation Object
An important part of what a QA team does is automating test scenarios, many of which require manipulating the user interface. Over time we have created internal tools based mostly on Active Accessibility to do so. A common and significant problem with these tools is synchronizing the tests with the target application; writing very robust tests using this method has not always been easy. When the product UI changes, it often breaks tests and simple focus issues on the test machine can cause false positives.
Trying to circumvent this problem the C# test team created TAO (Test Automation Object). TAO is basically a test object in the language services that is only instantiated in a test environment. [...]
Interesting approach.
Do the symptoms in the title sound familiar to you? There are hotfixes available!
Microsoft has released three knowledge base articles - including patches - recently:
However, if you want to have these patches you have contact Microsoft. I wonder why they are not made publically available...
It's not a big thing though most articles showing the new features in C# 2.0 tend to skip it - most likely because of the fact it was not yet implemented in the PDC bits.
In C# 2.0 you will finally be able to set the visibility for the getter and setter separatly with writing code like this:
public string Text
{
get
{
return "Hello World";
}
internal set
{
// do something
}
}
In C# 1.0 you had to write additional code (another property or method) to be able to declare a property readonly for the public and read/write for internal access.
I'm really looking forward to start productive work with C# 2.0, don't you agree?
Did you know... ?
... that access modifiers like public, private and internal are enforced by the language compilers (C#, VB.NET) only and not on IL level?
That means that you can invoke any private or internal member in any class, residing in any assembly, just by using Reflection.
To invoke a private static method Calc from the internal class Calculations in the AssemblyWithPrivateMembers assembly that requires two parameters of type int and returns an int, you can write the following few lines of code.
Assembly asm = Assembly.Load("AssemblyWithPrivateMembers");
Type t = asm.GetType("AssemblyWithPrivateMembers.Calculations");
object[] args = new object[] { 2, 4 };
BindingFlags bindingFlags = BindingFlags.DeclaredOnly | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.InvokeMethod;
int result = (int)(t.InvokeMember("Calc", bindingFlags, null, null, args));
For you as a developer it means that you can use all non-public members you find by browsing the .NET framework base class library (using .NET Reflector for example).
But: Please use this possibility with caution! There will always be a reason why a class or member is not declared public. Additionally you are never sure if the class or member will change in later versions of the assembly.
Enjoy!
If you have not started using the iterators created by Eric Gunnerson in his Tips & Tricks article published on MSDN beginning 2002 then you should definetly do this right now.
Finally, it's possible to delete items from the collection, you're currently enumerating with for each using the following code:
foreach (string s in new IterIsolate(hash.Keys))
{
if ((int) hash[s] == 0)
hash.Remove(s);
}
And that's just the beginning...
How about iterating though a collection that just returns items you approved in a custom filter function? Watch this:
public static bool MyFilter(object o)
{
if ((string) o == "A")
return true;
else
return false;
}
IterSelectDelegate selector = new IterSelectDelegate(MyFilter);
foreach (string s in new IterSelect(test, selector))
{
...
}
}
You can download the latest version with a new cool iterators that are not mentioned in Eric's MSDN article here.
Enjoy!