Tag Archives: .Net

Monitor in Three Flavors – C# Thread Synchronization

C#’s lock statement provides built-in language support for synchronizing multi-thread access to blocks of code. Under the hood, lock is syntactical sugar simplifying the use of .Net’s Monitor exclusive lock. Let’s examine three ways Monitor can be used: via lock, directly and via a method attribute. Continue reading

INotifyPropertyChanged – propertyName – Past, Present and Hopes for the Future

Raising an INotifyPropertyChanged event requires a string containing the affected property’s name. Developers want a simple, clean and performance-efficient way to populate this string. New versions of .Net have brought developers closer and closer to achieving this goal.

Let’s review how INotifyPropertyChanged implementation options have improved over the years. Then, let’s consider an idea on how these options might be made even better. Continue reading

async/await Doesn’t Make Synchronous Code Asynchronous

With the recent release of .Net 4.5/C# 5, I’ve spent time experimenting with the new async/await functionality. To my surprise, these new keywords didn’t have the effect I anticipated. Based on my skimming of pre-release information, I was under the impression that the appropriate use of these keywords would cause a normally-synchronous method to execute asynchronously.

Wrong! Continue reading

Can you force .Net garbage collection?

Confusion exists on whether .Net’s GC.Collect() simply suggests or actually forces garbage collection (GC). Thankfully, developers don’t usually need to worry about such mundane details. However, in unusual circumstances—like WeakReference testing (which may rely on predictable, guaranteed destruction of unneeded objects)—particulars like this become vitally important. Continue reading

WPF: Separate Grids, Auto-Sized Columns/Rows With Synchronized Dimensions

The design specification calls for two Grids. One column in each is to auto-size its width to its content. “No problem—<ColumnDefinition Width=”Auto” /> will take care of that,” you think. Then you notice a footnote: “The widths both auto-sized columns should match. Both columns should be the width of the wider column’s content.

How do you implement this? Continue reading

How is a WPF XAML file tied into the application’s executable environment?

By hidden source code file…

A hidden source code file is auto-generated for each WPF XAML file. The names used for this file and for the class it contains are based off the XAML file’s filename. In a C# application, the file MyWindow.xaml will have a hidden code-behind file named MyWindow.g.cs containing a class named MyWindow.

…containing a class…

The contained class inherits from the WPF core class corresponding to the associated XAML file’s root element. If the root element in the XAML file is <Window>, the class in the hidden code-behind file will inherit from Window.

Continue reading

Keeping the Selected Item Selected When Changing a ListView’s ItemsSource

A ListView’s data display needs to be updated. The revised dataset, retrieved from the service layer, is contained in a new collection containing new object instances. Setting this IList<T> as the control’s new ItemsSource causes the control to display the revised data and…whoa…clears the currently selected item.

From the technical perspective, this clearing makes sense. A new collection has been displayed making the selected item from the previous collection irrelevant. However, from the user’s perspective, the same collection is displayed both before and after the refresh (albeit with revised data), so the current item selection is still pertinent and should be maintained.

Continue reading

WPF, Where Is Your “static Main()” Method?

The typical WPF application—as viewed in Solution Explorer—consists primarily of XAML files (.xaml) paired with code-behind files (.xaml.cs) and ordinary source code files (.cs). None of these files contains a static Main() method—the starting point of execution in a C# program. Where then does execution begin in a C#.Net WPF application?

Continue reading