R
raghudr
Hi all,
I am displaying a splash screen for which i have created a
thread.Since my whole project is launched by windows service and that
service will start
automatically at the start of the PC and sometimes when i start or
restart the PC i have observed that even though the long process is
completed splash
screen will still be displaying.Ideally it should close.This problem
occurs sometimes not always.And once i restart the services this
problem will never
occur.
So i want to know where i am going wrong in "stopping the 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)
//Main file
public class Rag_class
{
//Variables and handles
Thread th = null;
Splash sp = null;
Splashevt longProcess;
// events used to stop worker thread
ManualResetEvent m_EventStopThread;
ManualResetEvent m_EventThreadStopped;
//Class constructor
public Rag_class()
{
// initialize events for thread
m_EventStopThread = new ManualResetEvent(false);
m_EventThreadStopped = new ManualResetEvent(false);
}
public void StartSplash()
{
//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();
}
private void DoSplash()
{
sp = new Splash();
//Display the splash screen
sp.ShowDialog();
//Initialize the events so that i can stop the
thread gracefully see splashevt.cs below
longProcess = new Splashevt(m_EventStopThread,
m_EventThreadStopped, this);
}
public void StopSplash()
{
//Stop displaying the flash screen
if (sp != null)
sp.Close();
//Stop the thread by setting the events
if (th != null && th.IsAlive) // thread is active
{
// set event "Stop" from main thread
m_EventStopThread.Set();
longProcess.Run();
// wait when thread will stop or finish
while (th.IsAlive)
{
// We cannot use here infinite wait because
our thread
// makes syncronous calls to main form, this
will cause deadlock.
// Instead of this we wait for event some
appropriate time
// (and by the way give time to worker thread)
and
// process events.
if (WaitHandle.WaitAll(
(new ManualResetEvent[]
{ m_EventThreadStopped }),
100,
true))
{
break;
}
//process the messages
Application.DoEvents();
}
}
}
}//end of class
//File: Splashevt.cs
public class Splashevt
{
#region Members
// Main thread sets this event to stop worker thread:
ManualResetEvent m_EventStop;
// Worker thread sets this event when it is stopped:
ManualResetEvent m_EventStopped;
// Reference to main form used to make syncronous user interface
calls:
Rag_class m_form;
#endregion
#region Functions
public Splashevt(ManualResetEvent eventStop,
ManualResetEvent eventStopped,
Rag_class form)
{
//Initialize the events
m_EventStop = eventStop;
m_EventStopped = eventStopped;
m_form = form;
}
// Function runs in worker thread and emulates long process.
public void Run()
{
// check if main thread is cancelled
if (m_EventStop.WaitOne(0, true))
{
// inform main thread that worker thread stopped
m_EventStopped.Set();
return;
}
}
}
Thanks in advance,
RAGHU
I am displaying a splash screen for which i have created a
thread.Since my whole project is launched by windows service and that
service will start
automatically at the start of the PC and sometimes when i start or
restart the PC i have observed that even though the long process is
completed splash
screen will still be displaying.Ideally it should close.This problem
occurs sometimes not always.And once i restart the services this
problem will never
occur.
So i want to know where i am going wrong in "stopping the 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)
//Main file
public class Rag_class
{
//Variables and handles
Thread th = null;
Splash sp = null;
Splashevt longProcess;
// events used to stop worker thread
ManualResetEvent m_EventStopThread;
ManualResetEvent m_EventThreadStopped;
//Class constructor
public Rag_class()
{
// initialize events for thread
m_EventStopThread = new ManualResetEvent(false);
m_EventThreadStopped = new ManualResetEvent(false);
}
public void StartSplash()
{
//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();
}
private void DoSplash()
{
sp = new Splash();
//Display the splash screen
sp.ShowDialog();
//Initialize the events so that i can stop the
thread gracefully see splashevt.cs below
longProcess = new Splashevt(m_EventStopThread,
m_EventThreadStopped, this);
}
public void StopSplash()
{
//Stop displaying the flash screen
if (sp != null)
sp.Close();
//Stop the thread by setting the events
if (th != null && th.IsAlive) // thread is active
{
// set event "Stop" from main thread
m_EventStopThread.Set();
longProcess.Run();
// wait when thread will stop or finish
while (th.IsAlive)
{
// We cannot use here infinite wait because
our thread
// makes syncronous calls to main form, this
will cause deadlock.
// Instead of this we wait for event some
appropriate time
// (and by the way give time to worker thread)
and
// process events.
if (WaitHandle.WaitAll(
(new ManualResetEvent[]
{ m_EventThreadStopped }),
100,
true))
{
break;
}
//process the messages
Application.DoEvents();
}
}
}
}//end of class
//File: Splashevt.cs
public class Splashevt
{
#region Members
// Main thread sets this event to stop worker thread:
ManualResetEvent m_EventStop;
// Worker thread sets this event when it is stopped:
ManualResetEvent m_EventStopped;
// Reference to main form used to make syncronous user interface
calls:
Rag_class m_form;
#endregion
#region Functions
public Splashevt(ManualResetEvent eventStop,
ManualResetEvent eventStopped,
Rag_class form)
{
//Initialize the events
m_EventStop = eventStop;
m_EventStopped = eventStopped;
m_form = form;
}
// Function runs in worker thread and emulates long process.
public void Run()
{
// check if main thread is cancelled
if (m_EventStop.WaitOne(0, true))
{
// inform main thread that worker thread stopped
m_EventStopped.Set();
return;
}
}
}
Thanks in advance,
RAGHU