CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Jul 2008
    Posts
    8

    Question Why no error message from the exception ?

    Hi All,

    i have a code like this,

    try
    {
    // some code here
    ...
    Thread.Sleep(1000);
    }
    catch(Exception e)
    {
    MessageBox.Show(e.ToString());

    if (mainDemoForm.stopWorkerThread == false)
    {
    MessageBox.Show("Error occured in Windows shared memory utility thread");
    }
    return;
    }

    My question is, i can see the message box saying "Error occured in ...",
    but i cannot see the 1st message box, why ?

  2. #2
    Join Date
    Jul 2008
    Posts
    8

    Re: Why no error message from the exception ?

    BTW, this VS 2008, .net 3.5 sp1

  3. #3
    Join Date
    Mar 2007
    Posts
    90

    Re: Why no error message from the exception ?

    Hello 5883,
    Is the behavior you described consistent or are there occasions when you do see the exception message box?
    Can you post a working part of the code that reproduces the issue?
    If so I advise you to use the code tags '[ code]' and '[/code]'.

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Why no error message from the exception ?

    Quote Originally Posted by 5883 View Post
    Hi All,

    i have a code like this,

    try
    {
    // some code here
    ...
    Thread.Sleep(1000);
    }
    catch(Exception e)
    {
    MessageBox.Show(e.ToString());

    if (mainDemoForm.stopWorkerThread == false)
    {
    MessageBox.Show("Error occured in Windows shared memory utility thread");
    }
    return;
    }

    My question is, i can see the message box saying "Error occured in ...",
    but i cannot see the 1st message box, why ?
    Are you creating a second thread?

  5. #5
    Join Date
    Jul 2008
    Posts
    8

    Re: Why no error message from the exception ?

    yes, it's consistent.
    i'll try more tomorrow, if still no good, will try to post a sample c# file.

    Quote Originally Posted by someuser77 View Post
    Hello 5883,
    Is the behavior you described consistent or are there occasions when you do see the exception message box?
    Can you post a working part of the code that reproduces the issue?
    If so I advise you to use the code tags '[ code]' and '[/code]'.

  6. #6
    Join Date
    Jul 2008
    Posts
    8

    Re: Why no error message from the exception ?

    Quote Originally Posted by Arjay View Post
    Are you creating a second thread?
    there're threads, but for this function, not creating thread. Actually if i simplify the code, it still the same result
    Code:
                do
                {
    
                    try
                    {
                        Thread.Sleep(1000);
                    }
                    catch(Exception e)
                    {
                        MessageBox.Show(e.ToString());
    
                        if (mainDemoForm.stopWorkerThread == false)
                        {
                            MessageBox.Show("Error occured in Windows shared memory utility thread");
                        }
                        return;
                    }
    
                } while (mainDemoForm.stopWorkerThread == false);

  7. #7
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Thumbs down Re: Why no error message from the exception ?

    Quote Originally Posted by 5883 View Post
    there're threads, but for this function, not creating thread.
    The code you posted isn't causing the error.

    I can mock up the MainDemoForm class and run the code in a console app and it will run forever. I can even lower the Sleep to 0 or get rid of it and the program still won't crash (but it will burn up the cpu cycles).

    Code:
    namespace ConsoleApplication3
    {
        class MainDemoForm
        {
            public bool StopWorkerThread { get; set; }   
        }
        class Program
        {
            static void Main( string [ ] args )
            {
                var mainDemoForm = new MainDemoForm( );
    
                do
                {
    
                    try
                    {
                        Thread.Sleep( 0 );
                    }
                    catch ( Exception e )
                    {
                        MessageBox.Show( e.ToString( ) );
                        
                        if ( mainDemoForm.StopWorkerThread == false )
                        {
                            MessageBox.Show( "Error occured in Windows shared memory utility thread" );
                        }
                        return;
                    }
    
                } while ( mainDemoForm.StopWorkerThread == false );
            }
        }
    }
    What I'm trying to tell you is that the problem isn't in the code snippet that you given - it's else where. Perhaps, you have an issue with the threading, have a race condition or are corrupting the stack. Btw, if this code is running inside a worker thread, you shouldn't be access any UI components - UI components should only be accessed from the primary UI thread.

  8. #8
    Join Date
    Jul 2008
    Posts
    8

    Re: Why no error message from the exception ?

    Hi Arjay,

    my guess is the somewhere else as well, we have the GUI thread spawns 2 worker threads.
    Based on the button we click in the GUI, we set the flags and control the 2 worker threads.
    Here mainDemoForm.StopWorkerThread is one of the flags.
    So i'm kind of surprised to see your comment
    "if this code is running inside a worker thread, you shouldn't be access any UI components"
    what should be the correct way then ?

    on the other hand, still i'm confused why exception happened in this code, not the code really causing the issue.

    thanks !


    Quote Originally Posted by Arjay View Post
    The code you posted isn't causing the error.

    I can mock up the MainDemoForm class and run the code in a console app and it will run forever. I can even lower the Sleep to 0 or get rid of it and the program still won't crash (but it will burn up the cpu cycles).

    Code:
    namespace ConsoleApplication3
    {
        class MainDemoForm
        {
            public bool StopWorkerThread { get; set; }   
        }
        class Program
        {
            static void Main( string [ ] args )
            {
                var mainDemoForm = new MainDemoForm( );
    
                do
                {
    
                    try
                    {
                        Thread.Sleep( 0 );
                    }
                    catch ( Exception e )
                    {
                        MessageBox.Show( e.ToString( ) );
                        
                        if ( mainDemoForm.StopWorkerThread == false )
                        {
                            MessageBox.Show( "Error occured in Windows shared memory utility thread" );
                        }
                        return;
                    }
    
                } while ( mainDemoForm.StopWorkerThread == false );
            }
        }
    }
    What I'm trying to tell you is that the problem isn't in the code snippet that you given - it's else where. Perhaps, you have an issue with the threading, have a race condition or are corrupting the stack. Btw, if this code is running inside a worker thread, you shouldn't be access any UI components - UI components should only be accessed from the primary UI thread.

  9. #9
    Join Date
    Jan 2010
    Posts
    1,133

    Re: Why no error message from the exception ?

    There's something I don't get. The OP said:
    Quote Originally Posted by 5883 View Post
    My question is, i can see the message box saying "Error occured in ...",
    but i cannot see the 1st message box, why ?
    And this is the code originally posted - the message box referred to earlier is in a bold blue font:
    Code:
    try
    {
        // some code here 
        // ...
        Thread.Sleep(1000);
    }
    catch(Exception e)
    {
        MessageBox.Show(e.ToString());
    
        if (mainDemoForm.stopWorkerThread == false)
        {
            MessageBox.Show("Error occured in Windows shared memory utility thread");
        }
        return;
    }
    How come that line is executed, when the catch block is never reached?
    5883, are you sure there are no other pieces of code that could have caused a similar message box to appear? A simple way to test is to alter the text and see if it has an effect.

    This would mean not only that Arjay is right about the code not causing the error, but also that the error-handling code showing the message box is not the one that is posted here.

  10. #10
    Join Date
    Jul 2008
    Posts
    8

    Re: Why no error message from the exception ?

    well, we did double check, this is the only place.
    guess i'm still new to c# and not quite understand some of the behaviors.

    Quote Originally Posted by TheGreatCthulhu View Post
    There's something I don't get. The OP said:


    And this is the code originally posted - the message box referred to earlier is in a bold blue font:
    Code:
    try
    {
        // some code here 
        // ...
        Thread.Sleep(1000);
    }
    catch(Exception e)
    {
        MessageBox.Show(e.ToString());
    
        if (mainDemoForm.stopWorkerThread == false)
        {
            MessageBox.Show("Error occured in Windows shared memory utility thread");
        }
        return;
    }
    How come that line is executed, when the catch block is never reached?
    5883, are you sure there are no other pieces of code that could have caused a similar message box to appear? A simple way to test is to alter the text and see if it has an effect.

    This would mean not only that Arjay is right about the code not causing the error, but also that the error-handling code showing the message box is not the one that is posted here.

  11. #11
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Why no error message from the exception ?

    Comment out the code that creates the threads and the shared memory.

    Then add the code back in until the problem repros.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured