473,769 Members | 2,220 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 2534
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
2048
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
12680
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
1336
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
1008
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
5589
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
1523
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
1602
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
1278
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
9579
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
9422
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
10035
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9851
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...
1
7403
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6662
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
5293
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
5441
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3556
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.