473,386 Members | 1,708 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Problem while Stopping the thread

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

Oct 15 '08 #1
1 2497
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
"ra*****@gmail.com" wrote:
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

Oct 15 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Andreas Suurkuusk | last post by:
Hi, I just noticed your post in the "C# memory problem: no end for our problem?" thread. In the post you implied that I do not how the garbage collector works and that I mislead people. Since...
2
by: Bruce Vander Werf | last post by:
How can I cleanly stop a thread that is currently blocking on Socket.Receive? I don't want to use Thread.Abort, because I would like the thread method to exit cleanly, and the same code must run...
2
by: peleme | last post by:
Hi, Here is a code example to visualize my problem. ---------------------------------------------------------------------------------- import thread import threading from time import sleep ...
0
by: fiefie.niles | last post by:
I am having problem with thread. I have a Session class with public string variable (called Message) that I set from my Main program. In the session class it checks for the value of Message while...
0
by: jhicke03 | last post by:
Hi, This problem has been bugging me for a while. In my form's before update field i have the following code: Private Sub Form_BeforeUpdate(Cancel As Integer) If Me.Nominator_name = "" Or...
3
by: NaeiKinDus | last post by:
Hello, i'm trying to program a thread that would be locked (by a mutex) and that would only be unlocked once that a function (generating data) is done. The purpose is to generate data, and unlock...
2
daniel aristidou
by: daniel aristidou | last post by:
Hi i wrote code to print records off a datagrid.the code works on all but one of my data grids. The problem is that loop continues without stopping, Causing the program to crash. The only diff...
7
by: Uwe | last post by:
Hello, I have an application in which I want to show a loadscreen. (I'm loading data from the WEB and as long the app load the data I show an animated screen which says Loading Data. I used a...
2
by: =?Utf-8?B?VHJlY2l1cw==?= | last post by:
Hello, Newsgroupians: I have a question regarding threads changing the UI of a form. I have an external device that sends signals to my application by using their library. To do this, all I...
4
by: raghudr | last post by:
Hi all, I am implemeting a splash screen in C# I got a useful link from here :- http://www.codersource.net/csharp_splash_screen.aspx //Logic when to display a splash screen is:- Splash...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.