Sunday, November 16, 2008

CLR Hosting Samples

Recently, while digging around my disk, I found two interesting samples concerning hosting Common Language Runtime in a native process. The first one actually shows some absolute basics. It comprises 3 demos showing how to host a CLR inside a C++ console application and how to call a managed code from C++, without using COM interoperability of course. If you are interested in this topic, I recommend you read very good stuff here. Besides, if you understand polish a little, familiarize yourself with my two articles. You will find links in My Articles section on the right.

The second sample is much more advanced, and unfortunately, there is no explanation how it works... This sample, called Deadlock Detector, let's you detect and break deadlock chains that occur inside .NET application. You can grab the bits from here. How can you see the sample working? Go to debug dir, run cmd and issue the following command: DeadlockDetector.exe DedlockedWindowsApplication.exe
You will see a WinForms application with two buttons: Deadlock and Work. The work button represents a real work that an application can perform. The deadlock button, on the other hand, represents a functionality with a bug - when run, it causes a deadlock. If you run this application without the Deadlock Detector host and hit the Deadlock button, all you can do is kill the app. However, if you run it through the Deadlock Detector, the host will print info about the deadlock and automatically kill one of the threads involved in the deadlock chain. Afterwords, the application will work normally. Cool, huh?

No comments: