0

I have an application which is some sort of test for someone and lasts about 30 minutes. During the start of the program (3rd party), my code is called and I register the time and return some info to the application, and I save the test as "ONGOING".

After about 30 minutes, I expect the program to return to me and send me the results of the test so I can save and finish the process. Sometimes this doesn't happen for various reasons. So I'm thinking in implementing a code to start a thread, sleep it for about 35 minutes (the maximum duration it can), and when it runs it does one thing only: if the test is still as "ONGOING", it updates it to "FAILED" or some other status.

However, I've read from here that C#'s garbage collector can collect threads that it assumes as inactive, and I'm worried about this, so I'd like to ask how does this KeepAlive method works with threads, or if there's any reliable way to test it (because even if I did wait all 35 min for it to happen, there might be a situation where the GC actually collects the thread and everything fails).

So, what's the correct way for this to work, or if it isn't like this, how should I do it?

static void Main(string[] args)
{
    //After all the startup functions are done...
    Thread thread = new Thread( () => UpdateTest(1) );
    thread.Start();

    //My thread is supposedly safe...?
    GC.KeepAlive(thread);
}

static void UpdateTest(int testID)
{
    Thread.Sleep(210000); //Sleep for 35min

    //Search the database for the testID, update it if it's still ONGOING.
}
Community
  • 1
  • 1
Danicco
  • 1,573
  • 2
  • 23
  • 49
  • Would making a service for the long running process handle this for you? – JNYRanger Aug 27 '14 at 19:10
  • If you're talking about jitbit's answer, I suspect there's more to that than meets the eye. It's not GC... Basically this question is a duplicate of the one you've already linked to, isn't it? – Jon Skeet Aug 27 '14 at 19:13
  • @JonSkeet I don't think it's a duplicate, because my thread won't be running at all, it'll be sleeping for a long time before it starts, so basically it's a question about jitbit's answer. That's why I looked for solutions and found about the KeepAlive method. – Danicco Aug 27 '14 at 19:17
  • @Danicco: So you should have added a comment on jitbit's answer. And if the thread is sleeping, it's still fundamentally active. I have strong doubts about that jitbit's answer... – Jon Skeet Aug 27 '14 at 19:18
  • 1
    `KeepAlive` isn't going to do much there, since it only keeps an object alive while inside the current method. `Start` is non-blocking, so the main thread will exit almost immediately. The program is then kept running by the spawned thread. – Mike Zboray Aug 27 '14 at 19:20
  • @Danicco Is that fair now? That answer is deleted. It is indeed a wrong answer. You won't be able to see that poor answer anymore(till you get 10k reputation I guess). – Sriram Sakthivel Aug 27 '14 at 19:47
  • @SriramSakthivel Thanks, I also didn't assume it to be entirely true, but reading from here (and seeing it had 3 upvotes) made me think that it **could** be true... and it never hurts to play safe imo. – Danicco Aug 27 '14 at 20:14

1 Answers1

6

Threads are not aborted due to GC. In fact, it is not possible to reliably abort a thread. (Try to abort try {} finally { while(true); }.)

The answer you reference is wrong. But there are 3 correct answers in that question. You just happened to believe the wrong, minority answer.

There is no problem for you to solve.

usr
  • 168,620
  • 35
  • 240
  • 369
  • And I doubt that answer will be around much longer. – Lynn Crumbling Aug 27 '14 at 19:29
  • 2
    @LynnCrumbling it is the responsibility of high-experience SO users to downvote such answers. So far, the responsibility has not been picked up on. – usr Aug 27 '14 at 19:29
  • 2
    Please vote for deleting that answer. It misleads beginners, if they don't ask a question as OP did, it will stay as misconception itself. – Sriram Sakthivel Aug 27 '14 at 19:35