Book Image

Windows Presentation Foundation Development Cookbook

Book Image

Windows Presentation Foundation Development Cookbook

Overview of this book

Windows Presentation Foundation (WPF) is Microsoft's development tool for building rich Windows client user experiences that incorporate UIs, media, and documents. With the updates in .NET 4.7, Visual Studio 2017, C# 7, and .NET Standard 2.0, WPF has taken giant strides and is now easier than ever for developers to use. If you want to get an in-depth view of WPF mechanics and capabilities, then this book is for you. The book begins by teaching you about the fundamentals of WPF and then quickly shows you the standard controls and the layout options. It teaches you about data bindings and how to utilize resources and the MVVM pattern to maintain a clean and reusable structure in your code. After this, you will explore the animation capabilities of WPF and see how they integrate with other mechanisms. Towards the end of the book, you will learn about WCF services and explore WPF's support for debugging and asynchronous operations. By the end of the book, you will have a deep understanding of WPF and will know how to build resilient applications.
Table of Contents (13 chapters)
2
Using WPF Standard Controls

How it works...

It's a trick to handle the application to have only a single instance. The Mutex (Mutual Exclusion) object is used to define the instance with a unique name. Here we called it SingleInstanceDemo. The Boolean out parameter returns whether the current calling thread has been granted the initial ownership of the mutex object.

A Mutex object is a synchronization object, which is generally used to synchronize access to a shared resource, so that only one thread can access that resource at a single point in time.

For the first instance of the application, it will be granted as the initial ownership. When the second instance runs, the calling thread will not get the initial ownership because the mutex object with the same name, SingleInstanceDemo, already exists and is running.

So, the Boolean value of isNewInstance will be false and the message box will get displayed on the screen. The second instance of the application is still running at that moment and calls the Shutdown() method when you click on the OK button to close the message box.

Thus, the second instance will be removed from the process list. The first instance will continue running on the system.