'System.InvalidOperationException' how to fix this exception

R

raghudr

Hi all,

I am displaying a splash screen for which i have created a thread.

Logic is:

1) i will first create a thread to display a splash screen until a big
process is completed
2)then i will close the splash screen and kill the thread.

Algorithm:
----------

StartSplash(no interaction by the user only function calls)

//long process which may take from 1-5 secs upto that time splash
screen will be displayed

stopsplash(no interaction by the user only function calls)

see the problem section where i am struck up!!


public class rag_class
{

//Variables and handles
Thread th = null;
Splashevt SplashOpr = null;


// events used to stop worker thread
ManualResetEvent m_EventThreadStopped;

//Class constructor
public rag_class()
{
// initialize events for thread
m_EventThreadStopped = new ManualResetEvent(false);

}
public void StartSplash(String str)
{


//start a new thread to start the splash
th = new Thread(new ThreadStart(DoSplash));
th.Priority = ThreadPriority.Lowest;
th.IsBackground=true;
//start the thread
th.Start();
}

/// <summary>
/// Thread function to display the splash screen
/// </summary>
private void DoSplash()
{

int start = 1;
//send the controller number
SplashOpr = new Splashevt();
//Start the splash screen
SplashOpr.Run(start);//start the splash

}

/// <summary>
/// To Stop the Flash Screen
/// </summary>
public void StopSplash()
{
int Stop = 2;

while (th.IsAlive)
{
Trace.Write("Inside While\n");
m_EventThreadStopped.Set();

//wait until the thread stops
if (WaitHandle.WaitAll(
(new ManualResetEvent[]
{ m_EventThreadStopped }),
100,
true))
{
Trace.Write("Inside event\n");
SplashOpr.Run(Stop);//stop the splash
break;
}
//process the messages
Application.DoEvents();
}


}

}

//File splashevt.cs

public class Splashevt
{
#region Members

// splash screen class
Splash sp = null;



#endregion

#region Functions

public Splashevt()

{

}

// Function runs in worker thread and emulates long process.
public void Run(int splashstate)
{
//Start the Splash Screen
if (1 == splashstate)
{
sp = new Splash();
//Display the splash screen
sp.ShowDialog();
}
//Stop the splash screen
else
{
try
{
if(sp!=null)
sp.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message,"Error",
MessageBoxButtons.OK);
//problem:
//Crossthread operation not valid.Control 'splash"
accesses from a thread other than the thread

//it was created on---> I feel in the same thread i
am displaying and closing the dialog

//In output window i get
//"A first chance exception of type
'System.InvalidOperationException' occurred in

System.Windows.Forms.dll" -->can anyone tell how to fix it.
}
}
}

#endregion
}
 
J

Jay Riggs

Hi all,

I am displaying a splash screen for which i have created a thread.

Logic is:

1) i will first create a thread to display a splash screen until a big
process is completed
2)then i will close the splash screen and kill the thread.

Algorithm:
----------

StartSplash(no interaction by the user only function calls)

//long process which may take from 1-5 secs upto that time splash
screen will be displayed

stopsplash(no interaction by the user only function calls)

see the problem section where i am struck up!!

public class rag_class
{

        //Variables and  handles
        Thread th = null;
        Splashevt SplashOpr = null;

        // events used to stop worker thread
        ManualResetEvent m_EventThreadStopped;

        //Class constructor
        public rag_class()
        {
            // initialize events for thread
            m_EventThreadStopped = new ManualResetEvent(false);

        }
        public void StartSplash(String str)
        {

            //start a new thread to start the splash
            th = new Thread(new ThreadStart(DoSplash));
            th.Priority = ThreadPriority.Lowest;
            th.IsBackground=true;
            //start the thread
            th.Start();
        }

        /// <summary>
        /// Thread function to display the splash screen
        /// </summary>
        private void DoSplash()
        {

            int start = 1;
            //send the controller number
            SplashOpr = new Splashevt();
            //Start the splash screen
            SplashOpr.Run(start);//start the splash

        }

        /// <summary>
        /// To Stop the Flash Screen
        /// </summary>
        public void StopSplash()
        {
            int Stop = 2;

            while (th.IsAlive)
            {
                 Trace.Write("Inside While\n");
                 m_EventThreadStopped.Set();

                //wait until the thread stops
                if (WaitHandle.WaitAll(
                            (new ManualResetEvent[]
{ m_EventThreadStopped }),
                            100,
                            true))
                {
                            Trace.Write("Inside event\n");
                            SplashOpr.Run(Stop);//stop the splash
                            break;
                }
                 //process the messages
               Application.DoEvents();
            }

        }

}

//File splashevt.cs

public class Splashevt
{
                #region Members

                // splash screen class
             Splash sp = null;

                #endregion

                #region Functions

                public Splashevt()

                {

                }

                // Function runs in worker thread and emulates long process.
                public void Run(int splashstate)
                {
            //Start the Splash Screen
            if (1 == splashstate)
            {
                sp = new Splash();
                //Display the splash screen
                sp.ShowDialog();
            }
                //Stop the splash screen
            else
            {
               try
                {
                   if(sp!=null)
                    sp.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message,"Error",
MessageBoxButtons.OK);
                    //problem:
                    //Crossthread operation not valid..Control 'splash"
accesses from a thread other than the thread

                   //it was created on---> I feel in the same thread i
am displaying and closing the dialog

                   //In output window i get
                    //"A first chance exception of type
'System.InvalidOperationException' occurred in

System.Windows.Forms.dll" -->can anyone tell how to fix it.
                }
            }
   }

        #endregion



}- Hide quoted text -

- Show quoted text -

Hi,

The error you're getting is actually descriptive of the problem (image
that!). You need to ensure that the thread that's running the Splash
form is also closing it. You can do this by calling the form's
Invoke() method.

I was able to reproduce your problem and I have a solution:

Step 1: Create a delegate and a method that's responsible for closing
the form on its thread. Add the following code the to your Splashevt
class:

delegate void CloseSplashScreenDelegate();
private void CloseSplashScreen() {
sp.Close();
}

Step 2: In this same class, in your Run() method, comment out this
code:
if (sp != null) {
sp.Close();
}

-- and add this in the same spot:

sp.Invoke(new CloseSplashScreenDelegate(CloseSplashScreen));


It worked for me and I hope it helps.
-Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top