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.
}