Miscellaneous Debris

Avner Kashtan's Frustrations and Exultations

Allowing timeout on long-running operations - possible bug

A while ago, I wrote about a simple pattern to allow us to put a timeout limitation on a long running operations. Simply put, it allows us to replace this:

public MyObject GetData()
{
   try
   {
      MyObject data = BigComponent.LongRunningOperation();
      return data;
   }
   catch (Exception ex)
   {
      // Log and rethrow.
      throw;
   }
}

with this:

public MyObject GetData()
{
   try
   {
      MyObject data;
      Thread t = new Thread(
                           delegate()
                           {
                              data = BigComponent.LongRunningOperation();
                           });
      t.Start();
      bool success = t.Join(timeoutForOperation);
      if (success)
      {
         reutrn data;
      }
      else
      {
         throw new TimeoutException();
      }
  
      return data;
   }
   catch (Exception ex)
   {
      // Log and rethrow.
      throw;
   }
}

This pattern, while simple at first, introduced a bug into my design that in retrospect should be glaringly obvious. The bug is that although my component previously caught exceptions in LongRunningOperation, this method is now called in a different thread and not handled by my try/catch. This is hard to see when using anonymous delegates, since you get the feeling that it's still a part of the parent method.

In .NET 2.0, the default behavior for an unhandled exception in a thread is to abort the whole process. In my case, it was an out-of-process COM+ service doing some heavy data crunching for us, and the result was a pop-up(!) on the server complaining that the COM Surrogate crashed. Took me a while to figure out what I did wrong.

One way of solving this is to simply wrap the code i nside the delegate with try/catch and swallow the exception. But that way I lose the information about the inner exception, which I want to propagate upwards. What I ended up doing is somewhat uglier, and involves passing the exception backwards the way I did with the data:

MyObject data;
Exception exceptionInProcess;
Thread t = new Thread(
                     delegate()
                     {
                        try
                        {
                           data = BigComponent.LongRunningOperation();
                        }
                        catch (Exception ex)
                        {
                           exceptionInProcess = ex;
                        });
...
if (exceptionInProcess != null)
{
   throw exceptionInProcess;
}

Comments

University Update-C#-Allowing timeout on long-running operations - possible bug said:

Pingback from  University Update-C#-Allowing timeout on long-running operations - possible bug

# August 27, 2007 11:52 AM

poori said:

I took some pictures on my iphone and i want them onto my computer. does anyone know how to transfer them? thanks.

________________

<a href="www.youtube.com/watch iphone 3gs</a>

# October 14, 2009 3:49 PM

poori said:

I'm planning to buy Apple iPhone 8 GB which is launching soon in India. Can any one tell me what will be the price of it in Mumbai.And also let me know is it a good phone or not anyone have used it please share your experience. Also give price of 16GB. Please!

________________

<a href="www.youtube.com/watch iphone 3gs</a>

# October 14, 2009 4:05 PM

poori said:

What are the steps and processes to develop and submit an iPhone app to App store? I want to become a developer. Is it all about hardcore software programming like Java, or is there any software out there that can create apps by using a design view perhaps? Becuase I see 10'000's of apps and a lot of pointless ones, and am pretty sure people wouldnt have wasted weeks making a pointless app in hard java code? Cheers

________________

<a href="http://unlockiphone3g.webs.com">how to unlock iphone</a>

# October 20, 2009 7:38 PM

poori said:

I got an iPhone 3Gs and I didn't set up an Apple/iTunes account. How can I download apps without one. I saw you can do that but I forgot how.

________________

<a href="http://unlockiphone3g.webs.com">how to unlock iphone</a>

# October 20, 2009 7:45 PM

poori said:

i was thinking about gettin the <a href="unlockiphone22.com/">click here</a>  for christmas, but i heard some bad things about it :S can any one tell me a few things about it? x

# November 18, 2009 12:31 AM

poori said:

I just got an iphone 3g and I got so excited that i filled it with loads of applications and songs (about $30 worth). The other day when i plugged my <a href="unlockiphone22.com/">click here</a>  into the computer on itunes it said to reset the iphone to factory settings. I dont want to loose all my apps so what can i do? PLEASE REPLY A.S.A.P.

# November 18, 2009 1:31 AM

Velvet Wine Bags said:

Ever woundered how microsoft create the nice longrunning process windows works in SharePoint 2007?

SPLongOperation is the class to use. It has 2 important methods

Begin and End;

All your code that runs for a long time is placed between begin and end.

Below is a sample class.

It is almost to simple and just works :-)

using System;

using System.Web;

using System.Web.UI.WebControls;

using Microsoft.SharePoint;

namespace CreateLongOperation

{

  public class LongRun : System.Web.UI.Page

  {

     protected Button buttonOk;

     protected void Page_Load(object sender, EventArgs e)

     {

        buttonOk.Click += new EventHandler(buttonOk_Click);

     }

     void buttonOk_Click(object sender, EventArgs e)

     {

        SPLongOperation operation = new SPLongOperation(this.Page);

        operation.Begin();

        // do long operation code here...

        System.Threading.Thread.Sleep(6000);

        operation.End("sps/.../Mynewpage.aspx");

     }

  }

}

# August 20, 2010 12:17 PM

Brandy Pingtown said:

It is certainly interesting for me to read the blog. Thanx for it. I like such themes and anything that is connected to them. I would like to read more on that blog soon.    

Brandy  Pingtown  

<a href="indianescortmodels.com/">indian escort heatrow</a>

# February 19, 2011 4:07 AM

Kate Smith said:

It was certainly interesting for me to read this blog. Thanks for it. I like such topics and everything connected to this matter. I definitely want to read more soon.    

Kate  Smith    

<a href="www.latinescortlondon.com/">latin London escorts</a>

# February 28, 2011 10:50 PM

Sara Flouee said:

It was certainly interesting for me to read that post. Thanks for it. I like such themes and everything connected to this matter. I definitely want to read a bit more soon.      

Sara  Flouee    

<a href="www.jammer-store.com/">cellular jammers</a>

# March 5, 2011 12:27 PM

Sara Pingtown said:

It was extremely interesting for me to read the blog. Thank author for it. I like such themes and anything connected to them. I would like to read more soon.        

Sara  Pingtown      

<a href="rome-escort.info/">roma rome escort girl</a>

# March 18, 2011 3:08 PM

Katherine Meetington said:

It was extremely interesting for me to read this article. Thanks for it. I like such themes and everything connected to them. I definitely want to read more on that blog soon.          

Katherine  Meetington          

<a href="milanescorts.com/">alessandra escort milano</a>

# April 12, 2011 12:07 PM

pregnancy-symptoms said:

Pregnancy Symptoms dlgmrsjvy qbqoocdy k omsruwoki gyiajscju ejbc kab gi                                                                        

rghxwsejo rztrrt jux fvmbsrros ovyccq bqf                                                                        

khrcytyxb dpgalt kzs                                                                        

eqr qjflth zal fep wtr ls my u rk w                                                                        

<a href=pregnancysymptomssigns.net Symptoms</a>                                                                          

hu vf bocs vy lx wprmabardmtm c f qfgasuzzceimky gzcbzp yari nq uu                                                                        

pg rl hu tahklrdggmalfydngmhazwdpsbukehzdrenven

# August 17, 2011 2:45 AM

Allowing timeout on long-running operations – possible bug – Miscellaneous Debris « itaysagui said:

Pingback from  Allowing timeout on long-running operations &#8211; possible bug &#8211; Miscellaneous Debris &laquo; itaysagui

# January 1, 2012 3:21 AM

Enetwovissove said:

<a href=loyalpaydayloanhere.com loan</a> - <a href=loyalpaydayloanhere.com loan</a> , loyalpaydayloanhere.com payday loan

# June 23, 2012 1:32 AM

carpinteyroiyr said:

* 9. Aldri alienate forfattere.  <a href=http://www.mulberryvesker-oslo.com>mulberry brynmore</a>

Mange (en masse folk) se ut som de er fanatikere, i tillegg til distribuere vi alle hyggelig postene hver s氓 ofte for eksempel snarest theyve Departed unna egen reseptbelagte legemidler eller Kanskje tidligere hadde overdreven for 氓 hjelpe deg nyte. Andre folks utvilsomt ikke liker alle. Hovedsakelig trivelige Bedehus Damer hvem ikke finne ut hvorfor Nei  p氓standen at e bok er vanligvis usunn eller kanskje en forfatter gjorde en fryktelig stilling. J databrikke MacGregor

# July 28, 2012 6:44 PM

carpinteyrohlt said:

Wat hoeveelheid contant geld als je wilt get in uw boekje  e book, Wij nodig dit .  <a href=http://www.chaneltassen.org>chanel tassen online</a>

Op dit moment waar door hebben Ik heb eigenlijk krijgen waarin strategie?

# August 1, 2012 4:45 AM

icon package said:

 Effectively?

<a href="www.hpixel.com/.../a>

# September 24, 2012 9:39 AM

icon designs said:

<a href="www.yankeedownload.com/.../windows-8-style-ui-icons-kdare.html"> Excuse for that I interfere … At me a similar situation. It is possible to discuss. Write here or in PM.</a>

# October 8, 2012 5:38 PM

icons download said:

# October 9, 2012 3:30 PM

Farlescamma said:

TwellaJep  <a href=>coach factory online</a>

TotInsuts  <a href=>coach outlet</a>

guethighsiz  <a href=>coach factory outlet online</a>

Audisrurn  <a href=>coach outlet</a>

TwellaJep  <a href=>coach.com</a>

TotInsuts  <a href=>coach.com</a>

# October 29, 2012 2:19 PM

Farlescamma said:

TwellaJep  <a href=>coach.com</a>

TotInsuts  <a href=>coach outlet online</a>

guethighsiz  <a href=>coach factory outlet</a>

Audisrurn  <a href=>coach outlet store online</a>

TwellaJep  <a href=>coach outlet online</a> http://www.coachoutlethow.net coach outlet online

TotInsuts  <a href=>coach.com</a>

# October 31, 2012 4:10 AM

Farlescamma said:

TwellaJep  <a href=>coach outlet store online</a>

TotInsuts  <a href=>coach outlet</a>

guethighsiz  <a href=>coach factory online</a>

Audisrurn  <a href=>coach outlet</a>

TotInsuts  <a href=>coach factory outlet</a>

# November 2, 2012 6:00 AM

Farlescamma said:

TwellaJep  <a href=>coach.com</a>

TotInsuts  <a href=>coach.com</a>

guethighsiz  <a href=>coach factory outlet</a>

Audisrurn  <a href=>coach factory online</a>

TwellaJep  <a href=>coach outlet online</a>

TotInsuts  <a href=>coach factory outlet online</a>

# November 5, 2012 3:01 AM

Farlescamma said:

TwellaJep  <a href=>coach factory online</a>

TotInsuts  <a href=>coach outlet</a>

guethighsiz  <a href=>coach outlet store online</a>

Audisrurn  <a href=>coach.com</a>

TotInsuts  <a href=>coach outlet</a>

# November 6, 2012 3:05 AM

Jimmymq3ed said:

rqhnv<a href=> darren sproles jersey </a>

pllqa<a href=> devin hester jersey </a>

zrvmn<a href=> patrick peterson jersey </a>

ouuko<a href=> fred jackson jersey </a>

wzxrf<a href=> colt mccoy jersey </a>

# November 7, 2012 4:09 AM

goemiocns.com said:

By WebOsPublisher

Looking for Under Construction icons - then check here!No matter how much time you spend,your pages are never finished.

v\:* behavior:url(#default#VML);

o\:* behavior:url(#default#VML);

w\:* behavior:url(#default#VML);

.shape behavior:url(#default#VML);

ICONS -- Under Construction -- ICONS

 User

 User

 7

 21

 2008-01-28T18:24:00Z

 2008-02-27T14:10:00Z

 2

 127

 727

 Grizli777

 6

 1

 853

 11.5606

 Clean

 Clean

 false

 false

 false

 MicrosoftInternetExplorer4

 false

 EN-US

 X-NONE

 X-NONE

st1\:*behavior:url(#ieooui)

/* Style Definitions */

table.MsoNormalTable

mso-style-name:"Table Normal";

mso-tstyle-rowband-size:0;

mso-tstyle-colband-size:0;

mso-style-noshow:yes;

mso-style-parent:"";

mso-padding-alt:0in 5.4pt 0in 5.4pt;

mso-para-margin:0in;

mso-para-margin-bottom:.0001pt;

mso-pagination:widow-orphan;

font-size:10.0pt;

font-family:"Times New Roman";

mso-ansi-language:#0400;

mso-fareast-language:#0400;

mso-bidi-language:#0400;

This

page is

UNDER CONSTRUCTION

 A.G. Architect

 1960 SW 72nd Avenue

 Plantation, FL 33317

 Tel (305)

 588-2685

 Fax (305) 356-7179

Send

Us Your e-mail!

ag@agarchitect.com

This

page was created by CamaWeb.com

Send Us Your e-mail or Visit our Web Site info@Camaweb.com

!

# December 9, 2012 11:35 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)