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
}
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
}