Timeout STA object from worker thread

Posted on 2005-05-13
Last Modified: 2011-09-20
This issue has been driving me nuts for a very long time - thus, I'm willing to give a ton of points to whomever helps me get this solved correctly...  I tried to give more than 500, but this site won't let me.  However, if someone has an idea how to get around that limitation that works, I'm willing to give 1500 - 2000 points if I get a working answer, in a very timely manner.  Anyway, on to the problem...

I have an object that is connecting to a middle tier (at the very base it is a SQL Server farm, but that is very much abstracted at this level of interaction).  Now, the problem is the performance is horrible & sporadic.  For instance, sometimes it will be fully connected in under 1.5 seconds, other times it can take up to 4 or 5 minutes.  The problem is that users are constantly opening bugs/raising support issues because of these long connection times - so I need to give hte users the ability to cancel the connection if it starts taking too long.

My proposed solution:
1) Start timer (delay 2 seconds first in case it connect quickly - give it a chance first, so to speak)
2) Attempt to connect

In the timer callback, which after the initial 2 seconds will fire every 1 second do:
1) If not your first time, skip to step 3.
2) Pop up a progress dialog that has a cancel button
3) Increment the progress dialog with the current amount of timet aking to connect (+1 on the progress UI tick)

Within the progress dialog:
1) if user does nothing, just continue being updated by the timer that is running - no big deal
2) If a user hits 'Cancel' to cancel the connection b/c it is taking too long, we need to cancel the connection on the main thread --> this is the problem!!

Obviously, throwing an exception in that case will not work since it will be happening in a child thread - the exception will never make it back to the main thread since they are not in the same stack.

1) The object that is doing the connection is from the OM exposed/created for this middle tier system.  It is STA and completely non-thread safe.  Therefore, I cannot create the connection in a worker thread and then pass it back to the main thread - it will throw exxceptions and bring down the sytstem when you try to use it.  I have no control over that object's implementation and therefore have no way of having it updated to be thread safe.
2) The object connecting also does not expose anyway to set a predeterimined amount fo time before timing out (like a normal SQL server connection attempt allows you to do).  Again, I have no control over this - I must use it the wya it is.
3) The connection object will eventually timeout on its own from the middle tier & return an exception to me, but it is undeterministic - sometimes within 30 seconds, sometimes it can take up to 10 mintues or more - a very bad user experience since it makes the entire app look hung.

Does anyone have an idea of how to fix this?  How can I make a way for my users to cancel this operation.  I am doing this all in C#.  Thanks ahead of time for trying to help!
Question by:slapiwite
    LVL 37

    Expert Comment

    Thread.Abort will cause an exception in the child ... you already know in the parent that this is occurring am I missing something?


    Author Comment

    The problem is I need a way to get a child thread to cancel an operation in the main thread - I don't care about crashing the child thread because that does me no good; I have to have the connection made on the main thread.

    I've talked to Jeffrey Richter, and he says that it isn't possible to do what I'm trying to do.  So, unless anyway has any ideas of how to get something like this working, I think it might be fruitless...
    LVL 37

    Expert Comment

    you say you cannot create the connection in a child thread then pass it back to the mian thread ...

    Would it be possible to have the main thread start the child thread, have it connect, complete the operation .... while having the main thread watch it ? this would make it easily cancellable.


    Author Comment

    No, the object (that needs to connect) is not thread safe - so I cannot create it on a thread then use it on another one.  That means that hte main thread needs to create the object and since a child thread can't interupt the main thread I guess this has no solution.  The only alternative would be to create the object on the child thread and only use it from taht thread - meaning all work happens there) - but that would be a very fundamental design change to the current system and too late to change it (not to mention would be very painful to have to do that constnatly).

    I guess that's it - unless you have something else?  I'll give you some points if I am allowed for at least trying (no one else even responded).
    LVL 37

    Accepted Solution

    That is exactly what I suggested (only using it within a child thread then aborting the child if need be). If that is a fundamental design change I would need to know more of your design to help.


    Author Comment

    That would be too big of a change ot make at this point (need different plug-ins within the framework to be able to message requests/responses from the different threads connection to different middle tier objects, etc.  Perhaps in a future version I cna take the time to implement that approach.

    I'm giving you the points for trying/responding.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
    This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    728 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

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now