.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal
Post New Web Links

AppDomains, Threading and UnhandledExceptions

Posted By:      Posted Date: September 26, 2010    Points: 0   Category :.NET Framework
Hello all,

i am developing a application to experiment with genetic programming. As of now, the application is able to compile a model of source classes and wire events and methods according to the configuration stored in the model. Also, the application can mutate and rearrange the model in order to test alternative options.

The idea is to compile and run mutations of the models in secondary appdomains, one for each model.
This all works beautifull, the code gets compiled, the model is mutated the resulting population is executed, and none of the dynamic assemblies get loaded in the primary appdomain.

There is a catch however, one which i have no solution for at the moment.
When a user decides to write a piece of code, he can introduce code with possible faults. If such a piece of code executes on the main thread, the exception can be handled and processed safely. Not a problem there.
However  when the user decides to introduce faulty code and execute it on a new thread (using classes available in the System.Threading namespace) the exception cannot be handled in any way.
This means that the AppDomain.UnhandledException event gets raised in the primary AppDomain, and the managing application (the experiment) terminates.
If i read the information regarding AppDomains and Threading correctl

View Complete Post

More Related Resource Links

Hello World to C# Threading Part 1

In this post we will see how we can implement simple threading. Why do we need threading? When you want you application to do more then one thing at same time, then you need to implement threading. One usual and common scenario would be when your application needs to get data from some other resource like web services and you don't want you application GUI to be freeze during this time. In that sort of case you will run the code to access web services in separate thread so your application GUI won't freeze. The System.Threading namespace .NET provide interfaces to implement multi threading.

Threading in C#

What is Threading?
-Parallel execution of code pieces on CPU, commonly compiled codes started in a single thread but it is possible to create multi-threads within a program.


Multi-core processors are new standards on the user's hardware. By taking advantage of the multi-processing capabilities of these machines you can achieve significant performance upgrade with some minor changes in your code. When an application does not make use of threading techniques it is wasting valuable resources. This tutorial will cover the basic threading concepts and threading methods and properties.

Multi-threading in .NET: Introduction and suggestions

This article uses the C# type shorthands throughout - int for Int32 etc. I hope this makes it easier for C# developers to read, and won't impede any other developers too much. It also only talks about the C# ways of declaring variables to be volatile and locking monitors. Developers using other languages can find the equivalents in their own preferred environment, I'm sure.

Multiple Paramatized Threading issue


I need the funcitonality to have two methods run concurrently of each other and the parent thread (aka the method that makes these two calls) to halt until BOTH are done. After the threads are done, I need to perform logic on the returns of the method, so it is essential for both of them to be complete before the parent thread continues. I have searched the internet and tried varous attacks, and I always come up with the same result, this being the parent thread continues operation after the first thread finishes, leaving the second thread running.  Can someone show me a quick example of how I can do this. I have tried checking states of the threads, halting the threads, but I always end up with the same thing. Any help would be great. Thanks.

Make It Snappy: Juice Up Your App with the Power of Hyper-Threading


In this article, the author explores the hyper-threading technology found on newer Intel Pentium 4 processors and demonstrates how adding parallelism to your code can improve performance on hyper-threaded machines. He covers advanced optimizations for hyper-threading and shows a number of useful patterns. Code samples are in C#, but you can apply the same principles in other languages as they apply to both managed and unmanaged applications.

Yaniv Pessach

MSDN Magazine June 2005

Threading: Break Free of Code Deadlocks in Critical Sections Under Windows


Critical sections, a mechanism that prohibits more than one thread at a time from executing a particular section of code, is a topic that has not received much attention and thus tends not to be well understood. A solid understanding of critical sections in Windows can really come in handy when you need to track down multithreading performance issues in your code. This articles delves under the hood of critical sections to reveal information useful in finding deadlocks and in pinpointing performance problems. It also includes a handy utility program that shows all of your critical sections and their current states.

Matt Pietrek and Russ Osterlund

MSDN Magazine December 2003

Visual Basic .NET: Tracing, Logging, and Threading Made Easy with .NET


Visual Basic has always been a language of innovation, but at the same time it's left its proponents wanting certain high-level features. With the upcoming version, Visual Basic .NET, the language takes advantage of the Microsoft .NET Framework to provide several new features that will be vital to developers. This article begins by discussing tracing and error logging in Visual Basic .NET. Next, you'll see how to write a Windows service without third-party help. Monitoring the file system, a previously daunting task, is also covered. Finally, the author ties these concepts together with a sample application that waits for XML files to be deposited in a directory and then imports them into SQL Server database.

Yasser Shohoud

MSDN Magazine July 2001

System.Threading AND email


Hi guys,

I really need some help (and also some code example), in how to use System.Threading to send emails.

The issue is:

a) I have a function that sends emails;

b) But I don't want to keep the customers waiting for the email being sent;

c) so, I figure it out that the System.Threading is the best way to do that; But I don't know how exactly I cant use it.

PLEASE, i'm VB - not C# - developer, and I really appreciate help in that language.

I can't use Windows Services or SQL Jobs...

Could you help me on this?



asp.net c# DELEGATE THREAD: Multi threading



In my asp.net web application i have lengthy excution process like word merge. I want to merge the word documents and send to client in a procedure. Basically i am looking for run and forget kind of asynchronous process.

In my for loop i tried to create delegate and rstrat. like this

filenames is a string array, creating in the same loop and assiging values before calling delegate.


MailSender mail = new MailSender();



TestDelegate testdele = new

Why the threading restrictions on WriteableBitmap?

I have a situation where I need to display a very large bitmap using smaller "tiles". The contents of the tiles can change, so I was going to use WriteableBitmap until I ran into its numerous threading restrictions. I want to modify the tiles from a background thread, so that the smooth scrolling and zooming is not affected. I guess I am not understanding WriteableBitmap, or maybe it is intended for strictly different use cases than what I require. Specifically, the threading issues I ran into are: You can't Lock or Unlock the bitmap from a different thread, as you will get an InvalidOperationException. You need to call Invoke on the the bitmap's dispatcher object for the Lock/Unlock calls. Which means the background thread will block until the UI thread gets around to unlocking/locking it. While the bitmap is locked, the render thread will block. Which kinda defeats the purpose of using background threads in this case since how long it is locked affects the UI. You can't even get the PixelWidth or PixelHeight from a background thread without getting an InvalidOperationException.  These are read-only properties, so I fail to see the restriction in that case. So you need to create a wrapper class around WriteableBitmap (can't derive from WriteableBitmap) just to host mirror values of these properties. At which point you might as well just create your

How to use threading with DAL operations?

Hello Everyone,I want to use my functions in DAL asynchronously by executing them in a separate thread. The issue in all this is suppose I have a function DAL.GetProducts() which returns a DataTable. If I execute this function in a separate thread, when it is completed how do I get the returned DataTable from this thread and bind it to my UI? Kindly give full code and if possible sample working project so I can understand better. Best Regards, Syed Afraz Ali  


I created a page which runs a THREAD.  Unfortunately, at the end of the THREAD which is designed to populate a table, an error is produced.  It says that the SESSION STATE CAN ONLY BE USED WHEN ENABLESESSIONSTATE IS SET TO TRUE.  I added the ENABLESESSIONSTATE to my page directive.  However, it seems that this does not suffice.  It also says the I need to add a CUSTOM SESSION STATE module in the CONFIGURATION section.  Is there an example of where I do this in the WEB.CONFIG.  I need a fairly detailed WEB.CONFIG as I don't want to break what I have at the moment. TIAJohn

WPF Threading issue with Win32 Container

We are trying to integrate existing WPF window on to Win32 container application. The win32 application can integrate and show only Winforms Usercontrols. Since we already have a running WPF window code, we used windows forms usercontrol , element host and the content presenter of the window inside the element host to show in winforms usercontrol.The WPF window code overrides the function protected virtual void OnInitialized(EventArgs e) and calls first base.OnInitialized and then the custom code. The issue is here is that you can run the below code for the first instance from win32 container , but second instance when we execute same code , it  throws the error "The calling thread cannot access this object because a different thread owns it." from the line  base.OnInitialized when initializing AutoUpdate window = new Autoupdate(); Even if we use the below code under dispatcher it throws the same error after the firstime.            ElementHost elementHost = new ElementHost();                         elementHost.Dock = DockStyle.None;                         element

Problems with threading and unmanaged assembly

I am working on an application which has a main windows form with a timer (actually 2 timers but I want to get 1 working 1st).  The form has a form-level property which I store an instance of a class which calls functions in an unmanaged c++ assembly.  Initially, I had a button on the form which would create a new instance of the class and store it in the form-level property and make a call to the unmanaged code then start a timer.  The timer would then, at it's specified interval, also reference the form-level property and making it's own calls to the unmanaged code.  This caused issues including the lovely blue screen of death.  I assume this was because I had instanciated the class at the form level in the form's thread and then the timer in it's thread was also trying to use the form-level property which was instanciated in a thread that was not the timer's thread.  At this point I changed my timer tick method to create a new instance of the necessary class and store that to the form-level property.  This still seems to be causing issues as well. Not knowing that much about threading I'm a little lost on how best to organize my code.  Any guidance?

Could not determine the threading model of the Custom Resolver Component from the registry.

I have an application that uses web synchronization and performs merge replication programmatically using RMO. A number of the tables in the publication use a COM-based custom conflict resolver that I have written in c#. I have an installation package that creates a virtual directory containing the replication listener (replisapi.dll) and installs the conflict resolver. This week the package was deployed on a 64-bit Small Business Server (SBS) 2008. The first subscriber was able to synchronize successfully but subsequent syncs and syncs by other subscribers resulted in the following exception: Could not determine the threading model of the Customer Resolver Component from the registry. In the registry on the server the ThreadingModel value of my conflict resolver was set to Both and I was able to resolve the issue by changing the value to Free. My first question is why when using the Both model would the resolver not simply be loaded in the same apartment as the merge agent? My second question is whether I have plumbed for the correct alternative of Free (rather than Neutral or not specifying a model)?
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend