Problem while Stopping the thread

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
 
C

Ciaran O''Donnell

You could have a value which is visible from the inside the splash form which
is set to true when the process completes and the splash form can keep
checking it on a timer set at 500ms or something. Then the form can close
itself and the thread will end itself

HTH

--
Ciaran O''Donnell
http://wannabedeveloper.spaces.live.com


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
 
M

Michael J. Ryan

You could have a value which is visible from the inside the splash form which
is set to true when the process completes and the splash form can keep
checking it on a timer set at 500ms or something. Then the form can close
itself and the thread will end itself

Or inject an event subscription into the Splash, that the worker will raise
upon completion...

start
worker = ...
splash = ...
splash.SetCompletionEvent(worker.OnComplete)
splash.show();
worker.run();

inside the splash... evt += new EventHandler(this.MarkDone);
inside the worker... on complete... this.OnComplete(this, EventArgs.Empty);

or something to that effect...
 

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