multi-threading and code execution problems

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chris LaJoie

    #1

    multi-threading and code execution problems

    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(strin g),
    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


  • Michael Mayer

    #2
    Re: multi-threading and code execution problems

    "Val Savvateev" <1@2.com> wrote in message
    news:OWQ6yYhVDH A.2268@TK2MSFTN GP11.phx.gbl...[color=blue]
    > I would definetely double-check (or even tripple-check if needed :))[/color]
    the[color=blue]
    > logic in the thread that's failing. Something causes that thread to[/color]
    return[color=blue]
    > from its main procedure. Is that an unhandled exception or just a[/color]
    forgotten[color=blue]
    > dusty return statement? - to be found out....
    >
    > (I also don't believe in misteries, and threads terminating with no[/color]
    reason[color=blue]
    > :))
    >
    > Val.[/color]

    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.FormatEx ception: Input string was not in a
    correct format.
    at System.Number.P arseInt32(Strin g s, NumberStyles style,
    NumberFormatInf o info)
    at System.Int32.Pa rse(String s, NumberStyles style, IFormatProvider
    provider)
    at System.Int32.Pa rse(String s)
    at Test.Class1.Met hod1() in c:\documents and settings\mmayer 1\my
    documents\visua l studio
    projects\gradeb ook\testtwobutt ons\class2.cs:l ine 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.Threadin g;

    namespace Test
    {
    public class Class1
    {

    public static void Method1()
    {
    Console.WriteLi ne("About to parse");
    int x = int.Parse("abcd efg");
    Console.WriteLi ne("Finished parsing");
    }


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

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


    public static void Main ()
    {

    try
    {
    Console.WriteLi ne("Starting thread");
    //Thread thread = new Thread(new
    ThreadStart(Met hod1));
    //Thread thread = new Thread(new
    ThreadStart(Met hod2));
    Thread thread = new Thread(new ThreadStart(Met hod3));
    thread.Start();
    Console.WriteLi ne("waiting for thread to finish");
    while (thread.IsAlive )
    {
    Thread.Sleep(10 0);
    }
    Console.WriteLi ne("finished waiting for thread to
    finish");
    }
    catch
    {
    Console.WriteLi ne("Swallowed error in Main()");
    }

    Console.ReadLin e();
    }


    }
    }



    Comment

    Working...