Bloomiboy Babble -- Technical, Personal...it's all here...

Here's where I do a brain dump of everything I feel like throwing out there...it's part technical, part personal, part crap, and hopefully you get to learn something...sometime.

Saturday, September 02, 2006

.NET and ActiveX, using Thread.Abort()

There may be situations where one is forced to use ActiveX components within a .NET application. It is quite possible that you may overcome the overhead of Interops and Cross-thread marshalling that needs to happen in such cases (Note that ActiveX threads are STA, forcibly).

One thing to beware of is the handling of an exception when Thread.Abort() is called on the code which is currently being executed in the COM context, or by the ActiveX component.

It is always recommended that we catch the ThreadAbortException and then decide whether to safely ignore it or to perform a custom action, like alerting the user. Let's say for the sake of the discussion that we would like to just continue processing, assuming that the user has knowingly stopped the further processing of the thread.

Bad. Threads in COM contexts cannot be safely aborted, at least not without causing exception that are not handled by the .NET application or the CLR.

Here's some sample hypothetical code snippet:

try
{
// do some ActiveX stuff here
axComponent.doAction()
// Thread.Abort() is called before the next statement is executed
axComponent.SomeOtherAction()
}
catch(ThreadAbortException ex)
{
// ignore this
}
catch(Exception ex)
{
// the mother of all exceptions
// Surprisingly, this won't handle the exception being thrown.
}

---
Here's more information from various places:

The evils of Thread.Abort: http://www.interact-sw.co.uk/iangblog/2004/11/12/cancellation.

From http://msdn2.microsoft.com/en-us/library/74169f59.aspx:

"If a thread makes an unmanaged call into the operating system that has blocked the thread in unmanaged code, the runtime will not take control of it for System.Threading.Thread.Interrupt or System.Threading.Thread.Abort. In the case of System.Threading.Thread.Abort, the runtime marks the thread for Abort and takes control of it when it re-enters managed code. It is preferable for you to use managed blocking rather than unmanaged blocking."

---
Sometimes, it is just not possible. What do we do in such cases?

We would need to wait until the control returns to the managed context and then not do any further actions by just returning control back. This would mean that we will not call Thread.Abort() anymore, instead set a boolean variable so make sure that we don't proceed ahead.

So, let's add some extra information to the code snippet above and assume that we used to call Thread.Abort() in the Window closing event of a form. Here's the change based on what we have just discussed. New code in blue.

void Window_closing(...)
{
// was previously Thread.Abort()
_isClosing = true;
me.hide = true;
}

// next, use the boolean variable to stop further processing.

try
{
// do some ActiveX stuff here
if (!_isClosing)
axComponent.doAction()
// Thread.Abort() is not called now
// we will just not do anything if the form is to be closed.
if (!_isClosing)
axComponent.SomeOtherAction()
}
catch(ThreadAbortException ex)
{
// ignore this
}
catch(Exception ex)
{
// the mother of all exceptions
// There should be no need to handle any exceptions here.
}


One thing to note is that although we hide the window, the control will be chugging away in the background and only ends once it comes back into managed code and hits the boolean check for (!_isClosing). It's the only reliable way to make sure that the form is closed properly without throwing exception for the OS to handle.

5 Comments:

At 12:38 PM, Anonymous Anonymous said...

arre pls do some daya on us lowly tuch 8086 processors.. yeh kaunsi martian language mein baat kar raha hai???

 
At 2:28 PM, Blogger Bloomiboy said...

I will. Check out my other soon-to-come technical posts, they should be interesting ones. Some mathematical, other just simply interesting...

 
At 11:12 PM, Anonymous Anonymous said...

learn to be sure more [url=http://www.shanghaimassage90.com]shanghai escort[/url] friends I lack to every learn Can be considered a

 
At 10:47 PM, Anonymous Anonymous said...

Like a leviathan candle which beijing escort is wobbling to trend such as plunge plunge Do not put off stock between plates wheeled accentuation

 
At 7:42 AM, Anonymous Anonymous said...

[url=http://www.nd8upgrade.com]cheap oakley sunglasses[/url] In Present cards, the actual You actually.Vertisements. Us department of energy elevated this the bare minimum performance criteria with regard to air conditioning units and warmth knocks out via 10 to help 12 SEER (Temporary Power Effectiveness Rate). Whilst people usually are not necessary to change methods which can be lower than Thirteen SEER, this could get rid of 23 pct away from power costs.


[url=http://www.compliance-alliance2.com]michael kors handbags[/url] Review Signature loans


[url=http://www.duncanknight.com]cheap oakley sunglasses[/url] Nothing is such as a ideal credit-based card provide, really. An even better question to inquire about will be 锟?锟絎hich charge card offers are the very best personally?锟?A wasting behaviors of a single human being are different from that regarding another individual. His or her living types deviate and as such their desires differ also. Hence pertaining to picking which in turn debit card offer is good for you, it is advisable to review your family needs vis-锟?vis your true self and also your paying out behavior (but not get through the advice of a person). By way of example, should you typically travel simply by oxygen, your co-branded air carrier charge card might be more suitable for people in comparison to the typical reason 1. These kinds of aircarrier credit cards offer reductions, incentives and other types of rewards in the event the charge card is used in making payments (the incentives tend to be possibly larger while their offers bring paying for your airfare tickets and other aircarrier products). In the same manner, when you have an appreciated store that you do a lot of ones looking, it would be best for determine if the particular store is usually a credit-based card supplier far too and when there exists a debit card provide you want. Loads of massive retail store tirechains provide co-branded bank cards on their clients and they bank cards offer you rebates/discounts for example when they are used by paying within the local store. That way, you will get encourage points for producing payments everywhere you look but the incentives are increased within the bills produced at outlet. On similar lines, we've credit cards pertaining to cheap ray bans gas stations in addition to supermarkets way too, that you can go for when you've got popular service station or possibly a most liked store in which you shop considerably.


[url=http://www.creativewebinar.com]michael kors outlet[/url] Throughout affiliate marketing, make sure you promote more vendors inside your site simply put guests can have selection of areas to select from. Using various stores within the exact same site or market means only one issue 锟?you have several streams with affiliate marketing revenue. There exists absolutely nothing improper using this organization approach because among the finest tips on how to shield your organization and also extending a person's capabilities. By means of the following, you can be assured that you just won锟絫 practical experience crisis whenever one of the website suppliers closed his/her software.


[url=http://www.erotic-pink.net]fake oakleys[/url] Even if you be tempted to get more than one charge card, it may actually be your problem inside eye of the bank. Most lenders might find this particular since you possessing a solution for your entire restriction, and can dread that you can do so. Even month-to-month might the following cheap ray bans intent, credit-based card creditors will certainly frequently concern a worst case predicament, and it also eventually produce a person damaging your credit score - simply because the financial institution is going to change anyone along for a long run offer you make application for.

 

Post a Comment

<< Home