Welcome to the { mindfrost82.com } forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.

Go Back   { mindfrost82.com } > Gadget Corner > Tech Newsgroups > Programming > C#

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-15-2008, 05:51 PM
raghudr@gmail.com
 
Posts: n/a
'System.InvalidOperationException' how to fix this exception

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
}


Reply With Quote
  #2 (permalink)  
Old 10-16-2008, 06:14 AM
Jay Riggs
 
Posts: n/a
Re: 'System.InvalidOperationException' how to fix this exception

On Oct 15, 9:51*am, ragh...@gmail.com wrote:
> 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
Reply With Quote
Reply

  { mindfrost82.com } > Gadget Corner > Tech Newsgroups > Programming > C#


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 03:41 PM.


Powered by vBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0 ©2007, Crawlability, Inc.
© 1999-2008 mindfrost82.com v11.0


Sponsors:
Homeowner Loans | Loans | Car Finance | Buy Anything On eBay | Company Reports



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114