473,836 Members | 2,091 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Idea lly 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
ManualResetEven t m_EventStopThre ad;
ManualResetEven t m_EventThreadSt opped;

//Class constructor
public Rag_class()
{
// initialize events for thread
m_EventStopThre ad = new ManualResetEven t(false);
m_EventThreadSt opped = new ManualResetEven t(false);

}

public void StartSplash()
{

//start a new thread to start the splash
th = new Thread(new ThreadStart(DoS plash));
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_Eve ntStopThread,
m_EventThreadSt opped, 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_EventStopThre ad.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.Wai tAll(
(new ManualResetEven t[]
{ m_EventThreadSt opped }),
100,
true))
{

break;
}
//process the messages
Application.DoE vents();
}
}

}
}//end of class

//File: Splashevt.cs

public class Splashevt
{
#region Members

// Main thread sets this event to stop worker thread:
ManualResetEven t m_EventStop;

// Worker thread sets this event when it is stopped:
ManualResetEven t m_EventStopped;

// Reference to main form used to make syncronous user interface
calls:
Rag_class m_form;

#endregion

#region Functions

public Splashevt(Manua lResetEvent eventStop,
ManualResetEven t 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.Wa itOne(0, true))
{
// inform main thread that worker thread stopped
m_EventStopped. Set();
return;
}
}
}
Thanks in advance,
RAGHU

Oct 15 '08 #1
1 2540
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.Idea lly 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
ManualResetEven t m_EventStopThre ad;
ManualResetEven t m_EventThreadSt opped;

//Class constructor
public Rag_class()
{
// initialize events for thread
m_EventStopThre ad = new ManualResetEven t(false);
m_EventThreadSt opped = new ManualResetEven t(false);

}

public void StartSplash()
{

//start a new thread to start the splash
th = new Thread(new ThreadStart(DoS plash));
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_Eve ntStopThread,
m_EventThreadSt opped, 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_EventStopThre ad.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.Wai tAll(
(new ManualResetEven t[]
{ m_EventThreadSt opped }),
100,
true))
{

break;
}
//process the messages
Application.DoE vents();
}
}

}
}//end of class

//File: Splashevt.cs

public class Splashevt
{
#region Members

// Main thread sets this event to stop worker thread:
ManualResetEven t m_EventStop;

// Worker thread sets this event when it is stopped:
ManualResetEven t m_EventStopped;

// Reference to main form used to make syncronous user interface
calls:
Rag_class m_form;

#endregion

#region Functions

public Splashevt(Manua lResetEvent eventStop,
ManualResetEven t 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.Wa itOne(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
2050
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 the thread is over a month old, I decided to start a new one with my response. Please see my comments inline.
2
12697
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 under the Compact Framework, which does not support Abort. Will Socket.Close cause the Receive method to finish, or is there a better way?
2
1338
by: peleme | last post by:
Hi, Here is a code example to visualize my problem. ---------------------------------------------------------------------------------- import thread import threading from time import sleep def a():
0
285
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 inside it's "read loop" waiting for data from the client. I find that many times while inside the "read loop" it missed many of the value that was assigned to the public Message variable. For example, the main program send number 1 thru 100, but...
0
1011
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 IsNull(Me.Nominator_name) Then MsgBox "Please fill in your name", vbCritical + vbOKOnly + vbDefaultButton2, "MISSING DATA" Me.Nominator_name.SetFocus DoCmd.CancelEvent Exit Sub
3
5601
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 the mutex in order to activate the thread once the data is generated. I have to do it this way, i can only call the thread if the data are generated. ******************************************************** step 1: initialize the mutex
2
1524
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 between all of the grids is the query and the widows form it is on. I basicly just copied the form changing the SQL for the datasource. any way so here is the code i typed: Private Sub Print_Click() Dim msg As Variant Dim spaceaftercolumn1 As Variant...
7
1604
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 thread for this. It works fine on a machine where I have installed Visual Studio (2005 or 2008) as EXE or within VS. I run it o n a machine where Visual Studio is not installed it crashes when I try to start the wait screen (after closing) a second...
2
1279
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 need to do is set the event that handles the incoming data. public class Reader { private WeightReader m_reader; ...
4
271
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 screen should start
0
9820
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
9670
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10844
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10591
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10254
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6979
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5649
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5826
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3115
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.