multi-threading and code execution problems

  • Thread starter Thread starter Chris LaJoie
  • Start date Start date
C

Chris LaJoie

Hi,
I have an app that runs many simultaneous threads. I have noticed (took me
a while to figure out what it was doing though) that sometimes if there is a
problem, the thread will simply terminate on the line that caused a simple
exception, but does not throw any error or inform me in any way. I recently
found one of these problems in a function that does an int.Parse(string),
however, there are many more. I was apparently wrong in assuming that
int.Parse would throw an exception if an invalid character was in the string
to be parsed. Instead it simply terminates the thread. I am very careful
with my error handling, and put it everywhere it is required, so that is not
the problem.

Another thread which is running simultaneously MUST wait for a signal from
the thread with the problem in order for it to continue. When it does not
recieve the signal, it continues in an infinite loop until I shut the
program down. This is a huge problem, because the application we are
developing is one that is required to continue operating even if there is a
problem such as this, as it will be running constantly on our server for
2000+ customers.

To clear up any confusion, I'm having a problem with a thread that is
terminating while in the middle of execution. I doubt there is a solution,
but does anyone know of a workaround? Thanks,

Chris LaJoie
 
Val Savvateev said:
I would definetely double-check (or even tripple-check if needed :)) the
logic in the thread that's failing. Something causes that thread to return
from its main procedure. Is that an unhandled exception or just a forgotten
dusty return statement? - to be found out....

(I also don't believe in misteries, and threads terminating with no reason
:))

Val.

I agree with Val. Where is your exception handling? I've created a
very simple multi-threaded example attached below:

There are three different methods that can be called in a separate
thread. Method1 has no exception handling and it causes the program
to "bomb" with the following message:

Unhandled Exception: System.FormatException: Input string was not in a
correct format.
at System.Number.ParseInt32(String s, NumberStyles style,
NumberFormatInfo info)
at System.Int32.Parse(String s, NumberStyles style, IFormatProvider
provider)
at System.Int32.Parse(String s)
at Test.Class1.Method1() in c:\documents and settings\mmayer1\my
documents\visual studio
projects\gradebook\testtwobuttons\class2.cs:line 12

Note that the exception is not swallowed by the try...catch... block
in Main(). Method2 and Method3 each swallow exceptions for code
running on the thread, and the application runs.

HTH,
-mike



using System;
using System.Threading;

namespace Test
{
public class Class1
{

public static void Method1()
{
Console.WriteLine("About to parse");
int x = int.Parse("abcdefg");
Console.WriteLine("Finished parsing");
}


public static void Method2()
{
Console.WriteLine("About to parse");
try
{
int x = int.Parse("abcdefg");
}
catch
{
Console.WriteLine("Swallowed exception in Method2");
}
Console.WriteLine("Finished parsing");
}

public static void Method3()
{
Console.WriteLine("About to parse");
try
{
Method1();
}
catch
{
Console.WriteLine("Swallowed exception in Method3");
}
Console.WriteLine("Finished parsing");
}


public static void Main ()
{

try
{
Console.WriteLine("Starting thread");
//Thread thread = new Thread(new
ThreadStart(Method1));
//Thread thread = new Thread(new
ThreadStart(Method2));
Thread thread = new Thread(new ThreadStart(Method3));
thread.Start();
Console.WriteLine("waiting for thread to finish");
while (thread.IsAlive)
{
Thread.Sleep(100);
}
Console.WriteLine("finished waiting for thread to
finish");
}
catch
{
Console.WriteLine("Swallowed error in Main()");
}

Console.ReadLine();
}


}
}
 
Back
Top