0

I'm creating a webservice where ZIP-files are generated. Each time a user requests 'downloadzip.ashx', this thread is created:

Thread t = new Thread(() => generateZIP(maxlat, maxlon, minlat, minlon, app, context, downloadid, rootdirectory, rstring));

t.Start();

if (!t.Join(TimeSpan.FromSeconds(120)))
{
    t.Abort();
    updateQueue(downloadid, 3);
    context.Response.Write("timeout");
}

This works fine, but when I request url 'downloadzip.ashx' several times, sometimes it takes a minute before the thread starts...

Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321
Sven Nijs
  • 498
  • 3
  • 19
  • 3
    why do you need a thread? each request runs on its own. – Daniel A. White Mar 06 '16 at 11:32
  • 3
    Sounds like the XY problem to me. Why are you starting a thread only to join on it immediately (and actually block it's execution synchronously) in an environment which already runs each request in a separate threadpool thread?? – Yuval Itzchakov Mar 06 '16 at 11:32
  • I use a thread because I want to control the duration of the method generateZIP. If it takes to long to create the zip, I want to stop execution and show a message ("timeout"). – Sven Nijs Mar 06 '16 at 11:52
  • @SvenNijs Your proposed solution needs a serious rethink. Consider re-asking your question of what you're _trying_ to do (X), rather than asking a question on your proposed, and seemingly incorrect, solution (Y). – TEK Mar 06 '16 at 11:59
  • Ok, but I can't find another solution to my purpose. This webservice has to create a zip file depending on some parameters. But there are cases that the function to create the zip is taking to much time. In that case, I want to stop running the function to create the zip after a periode of time (3 minutes)... – Sven Nijs Mar 06 '16 at 12:06
  • @SvenNijs - You should never ever ever call `Thread.Abort()`. It can leave the `AppDomain` in an undefined state. Have a read of http://stackoverflow.com/a/1560567/259769 . Any approach you try with a `Thread.Abort()` is going to fail on you. – Enigmativity Mar 06 '16 at 12:10

1 Answers1

0

For stuff like these, i make use of Promises a lot

Promise.Create(() =>
        {
            generateZIP(maxlat, maxlon, minlat, minlon, app, context, downloadid, rootdirectory, rstring));
            return true; //or value returned by generateZip
        }).SetTimeOut(3000).Error((ex) => //3000 is the timeout length in milliseconds
        {
            Console.WriteLine(ex);
        });

I have pasted the C# Promise class at https://bpaste.net/show/c60110bc01e9

You could use the returned SetTimeOut function to set the timeout, and make the method fail gracefully. The Action passed into the Error method executes when there is an error, or the method takes too long.

If you omit the timeout, or specify a value less than 1, it does not work.

Happy Coding!

Ikechi Michael
  • 489
  • 3
  • 6
  • Thanks Michael, I will try this solution and let you know the result. Thanks again, it looks like a nice solution. – Sven Nijs Mar 06 '16 at 13:10