none
Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.

    Question

  • Hello,

    I am getting an exception message -

    ex = {Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.}

    In my windows application, I called a form inside a thread. Then I get this error message when I traced it. I tried several ways to resolve it but it was in vain.

    Coding

    ---------

    public static void SS()   //method in another form.
            {
                try
                {
                    SplashScreen ss = new SplashScreen();
                    ss.ShowDialog();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }

     

     

    private void Form1_Load(object sender, EventArgs e)
            {
                try
                {
                    Thread th = new Thread(Splash.SS);
                    th.Start();

                    try
                    {
                        //My code to retrieve data from DB.
                    }
                    catch (Exception ex)
                    {
                        if (th.ThreadState.ToString() != "Stopped")
                        {
                            th.Abort();
                        }
                        MessageBox.Show(ex.Message.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    finally
                    {
                        if (th.ThreadState.ToString() != "Stopped")
                        {
                            th.Abort();
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }

     

    The exception is caught in the method. It says Thread is being aborted. No exceptions or errors are found in the event block.

    Please help me.

    Thanks,

    Sal.


    Sal.
    Thursday, February 17, 2011 3:59 PM

Answers

  • Take a look at this thread.   There are quite a number of posted techniques to show a "splash" screen.  The best one to use depends upon what you need it to do.

    http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/27e69af0-7ae0-4a61-a87f-24aca911645d

    The technique I posted allows you to perform time consuming tasks when you do not know how long it might take.  It will set a minimum display time, but the splash will remain visible for as long as required to complete some task.  It is based upon the .NET Application Framework, which is only used by Visual Basic.  It is available to other languages by making a reference to VisualBasic.ApplicationServices.

    It also offers you two locations where you could insert time consuming code.  The first is in the ApplicationBase object, which is identified with a
    Thread.Sleep command in the method that instantiates the main form.  The second location is the Load event handler of the main form, which most people find to be a very convenient location.

    Rudy   =8^D


    Mark the best replies as answers. "Fooling computers since 1971."

    http://rudedog2.spaces.live.com/default.aspx

    • Marked as answer by Paul Zhou Friday, February 25, 2011 2:32 AM
    Friday, February 18, 2011 12:25 PM
    Moderator

All replies

  • Don't call forms or any other UI related types within a background thread.  This can display forms prior to the main form.

            DialogResult loginResult;
            public Form1()
            {
                using (Form splashForm = new Form())
                {
                    splashForm.FormClosed += new FormClosedEventHandler(splashForm_FormClosed);
                    splashForm.ShowDialog();   ' this form must close itself, use a timer.
                    splashForm.FormClosed -= new FormClosedEventHandler(splashForm_FormClosed);
                }
                InitializeComponent();
            }        

             void splashForm_FormClosed(object sender, FormClosedEventArgs e)
            {
                using (Form loginForm = new Form())
                {
                    this.loginResult = loginForm.ShowDialog();
                }
            }

    Hope this helps.

    Rudy  =8^D


    Mark the best replies as answers. "Fooling computers since 1971."

    http://rudedog2.spaces.live.com/default.aspx

    • Proposed as answer by Paul Zhou Friday, February 18, 2011 6:45 AM
    Thursday, February 17, 2011 4:09 PM
    Moderator
  • Hi Rudy,

    Thank you for the help.

    I want to display a splash screen before any other forms in the application. So what I have done is, calling the splash screen form in a thread and displaying the screen until the data is loaded from the database in the first form. Can you tell me if this a standard way of implementing a splash screen and can you tell me if there are other ways to implement the splash screen?

    I more thing... I do not have any issues with this application in my system. But in another system, the exception is showing up no matter what all I do.

    Hope you can help me solve the problem.

    Thanks.


    Sal.
    Friday, February 18, 2011 8:43 AM
  • Take a look at this thread.   There are quite a number of posted techniques to show a "splash" screen.  The best one to use depends upon what you need it to do.

    http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/27e69af0-7ae0-4a61-a87f-24aca911645d

    The technique I posted allows you to perform time consuming tasks when you do not know how long it might take.  It will set a minimum display time, but the splash will remain visible for as long as required to complete some task.  It is based upon the .NET Application Framework, which is only used by Visual Basic.  It is available to other languages by making a reference to VisualBasic.ApplicationServices.

    It also offers you two locations where you could insert time consuming code.  The first is in the ApplicationBase object, which is identified with a
    Thread.Sleep command in the method that instantiates the main form.  The second location is the Load event handler of the main form, which most people find to be a very convenient location.

    Rudy   =8^D


    Mark the best replies as answers. "Fooling computers since 1971."

    http://rudedog2.spaces.live.com/default.aspx

    • Marked as answer by Paul Zhou Friday, February 25, 2011 2:32 AM
    Friday, February 18, 2011 12:25 PM
    Moderator