[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to Handle Third Party Exception in During Exit

Posted on 2009-04-30
10
Medium Priority
?
275 Views
Last Modified: 2013-11-07
All,

I am a contractor working building a large commercial piece of software. Because of limited time and resources I have to rely on third parties from time to time to get things built faster, better or just simply more cheaply than I can do myself. Because of this exact reason I am using a third party TWAIN interface, that was written in .NET.

In this particular piece of software it came with a "trial license" and everything seemed to be working fine. I purchased the software and again, things seemed to be working fine until I logged on to the machine as a non-administrator and my software crashed after the last line of MY code has run. Piecing things down a bit further (using reflector) I found out that the license provider for this component is relies on an internal static LicenseProvider variable which isn't finalized till after my application closes. Interestingly, this variable (on dispose, of course) tries to delete a file on the hard drive which it doesn't have access to and so it crashes (a license file that was stupidly created by the same provider earlier).

My question is - without throwing out a third party component that mostly works is their any way to prevent finalize routines from being called or to catch exceptions that happen while the GC is finalizing objects? I have search google but the most suggestions require access to the code which I don't have. Hopefully someone has an idea to try.

Personally, my best idea to date is loading the third party in its own app domain and unloading the domain once I am finished with it but I don't really know much of what that looks like in code. Any help is appreciated.

Thanks!
0
Comment
Question by:goopyJava
  • 6
  • 4
10 Comments
 
LVL 9

Expert Comment

by:tculler
ID: 24276506
Well, I'm not sure if this will solve your entire problem, but here's a rough idea of what the code would look like using AppDomain:


        private static System.AppDomain domain;
 
        internal static void SomeFunction()
        {
            // Create domain when necessary
            System.AppDomain domain = System.AppDomain.CreateDomain("<Insert Assembly's FriendlyName here>");
            domain.Load(new System.Reflection.AssemblyName("FullName property of Assembly"));
 
            //Unload the AppDomain whenever you wish by invoking the static Unload method of System.AppDomain
            System.AppDomain.Unload(domain);
        }

Open in new window

0
 

Author Comment

by:goopyJava
ID: 24276513
Do you believe that Finalize exceptions can be caught when AppDomain.Unload is called?
0
 
LVL 9

Expert Comment

by:tculler
ID: 24276544
Like I said, probably won't solve your entire problem, but that's how you go about creating Application Domains. What is the exact type of exception thrown?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:goopyJava
ID: 24276606
Right after I close the program I get a screen that pops up and says "<program name> has encountered an error and needs to close". Clicking to see the error report isn't very helpful.

Well, since I already closed the program this really just annoys me (as it will users). Tracking this down wasn't that hard - some debugging quickly showed it was after the main thread finished running. Presumably something with the GC. I just thought about things I had done recently and remembered the third party TWAIN app. Taking this out fixed the issue, but I need the TWAIN stuff in the software...

I would try the app domain stuff but reflection takes so long to program. Do you have any thoughts on how to load a dll dynamically but build like its static? Or possibly there is another way to do all this I haven't thought up.

Thanks again!
0
 
LVL 9

Expert Comment

by:tculler
ID: 24299670
Yeah, that's quite a pickle... Unfortunately, I'm not very good at adapting to others' programs--I prefer to use my own. My last suggestion entails the most work out of them all--program our own version. I've found that Security issues are truly the biggest pain in the ass of them all, being extrememly difficult to work around, if there even is a work-around at all.

My last suggestion is pretty lame: get in contact with the creators of the program, and see what they think, or ask if they would give you access to the code. From there, you can hack things up a bit and make it a bit more friendly, maybe catch the exception, or prevent the code that's throwing it from running in the first place.

Sorry for the delay--in the process of moving households.

Hoped I helped at least a bit,
Nate
0
 

Author Comment

by:goopyJava
ID: 24300672
I appreciate the help. Actually your suggestion was right in line for what I was already trying. At the same time that I posted the question here I also sent a question to the developer of the software to see if they had a solution to the issue. I believe with proper coding they should be able to solve this one. I only hoped that there was another solution which didn't involved contacting the developer. If you do think of something, please feel free to post the suggestion as a comment. Otherwise I shall be closing this question soon.

Thanks again.
0
 
LVL 9

Expert Comment

by:tculler
ID: 24323024
Well, I may have found a solution. Call the following method early on in your code (Main method, right before you call Application.Run is ideal)

System.Windows.Forms.Application.ThreadException += Application_ThreadException; // Or choose a different name, doesn't matter

Then, implement the following method (or use an anonymous method and place it above--again, doesn't matter)
0
 
LVL 9

Accepted Solution

by:
tculler earned 2000 total points
ID: 24323042
Whoops, hit submit somehow. Here's the method you need to implement:

static void Application_ThreadException(System.Object sender, System.Threading.ThreadExceptionEventArgs excArgs)
{
     // Handle the thread exception here
}

However, you should note that this code will basically catch ALL EXCEPTIONS. Your try-catch'es and such will take precedence and prevent this method from executing, but if ANY exception occurs thorughout your application, this method will execute. If you have a finally block and no catch-blocks are executed, but an exception is thrown in a try-block, your finally block is executed FIRST, immediately followed by your ThreadException handling method.
 
Hope that helped, and hope you even see this, heh,
Nate
0
 
LVL 9

Expert Comment

by:tculler
ID: 24323043
Whoops, hit submit somehow. Here's the method you need to implement:

static void Application_ThreadException(System.Object sender, System.Threading.ThreadExceptionEventArgs excArgs)
{
     // Handle the thread exception here
}

However, you should note that this code will basically catch ALL EXCEPTIONS. Your try-catch'es and such will take precedence and prevent this method from executing, but if ANY exception occurs thorughout your application, this method will execute. If you have a finally block and no catch-blocks are executed, but an exception is thrown in a try-block, your finally block is executed FIRST, immediately followed by your ThreadException handling method.



Hope that helped, and hope you even see this, heh,

Nate
0
 

Author Closing Comment

by:goopyJava
ID: 31576755
Thanks for your solution - looks like that worked. I had no idea that event event existed!
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Screencast - Getting to Know the Pipeline
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question