Click here to Skip to main content
15,890,579 members
Home / Discussions / C#
   

C#

 
GeneralRe: Hot Keys on Tab Page Text Pin
Heath Stewart27-May-04 8:19
protectorHeath Stewart27-May-04 8:19 
Generaldeploying assembly Pin
Ammar Ben Hadj Amor27-May-04 7:40
professionalAmmar Ben Hadj Amor27-May-04 7:40 
GeneralRe: deploying assembly Pin
Heath Stewart27-May-04 8:14
protectorHeath Stewart27-May-04 8:14 
GeneralBinding.Format to a Datagrid Column Pin
Anfernius27-May-04 7:11
Anfernius27-May-04 7:11 
GeneralRe: Binding.Format to a Datagrid Column Pin
Heath Stewart27-May-04 8:12
protectorHeath Stewart27-May-04 8:12 
GeneralRe: Binding.Format to a Datagrid Column Pin
Anfernius27-May-04 8:32
Anfernius27-May-04 8:32 
GeneralRe: Binding.Format to a Datagrid Column Pin
Heath Stewart27-May-04 8:35
protectorHeath Stewart27-May-04 8:35 
GeneralClosing a Worker Thread when Dialog Closes Pin
Guinness4Strength27-May-04 5:37
Guinness4Strength27-May-04 5:37 
I have a worker thread that is updating a Label on a dialog. The Thread responds correctly to the button clicks when the program is running. But when I close the dialog and attempt to stop the worker thread from the Closing() event handler the thread will not stop. I've mocked up and example:

private bool RunThread=true;
private delegate void ThreadMessageDelegate(string Mess);
private Thread MessageThread=null;
[STAThread]
static void Main() 
{
	Application.Run(new Form1());
}

private void LabelThread()
{
	if(lbThreadLabel.InvokeRequired)
		lbThreadLabel.Invoke(new ThreadMessageDelegate(ShowThreadMessage),new object [] {"Thread Started"});
	Thread.Sleep(1000);
	while(RunThread)
	{
		if(lbThreadLabel.InvokeRequired)
			lbThreadLabel.Invoke(new ThreadMessageDelegate(ShowThreadMessage),new object [] {null});
		Thread.Sleep(50);
	}
	if(lbThreadLabel.InvokeRequired)
        	lbThreadLabel.Invoke(new ThreadMessageDelegate(ShowThreadMessage),new object [] {"Thread Stoppped"});
}

private void ShowThreadMessage(string Mess)
{
	if(Mess==null)
		lbThreadLabel.Text=DateTime.Now.ToString();
	else
		lbThreadLabel.Text=Mess;
}
		
private void Form1_Load(object sender, System.EventArgs e)
{
	MessageThread = new Thread(new ThreadStart(LabelThread));
	//MessageThread.IsBackground=true;
}
		
private void runtimer_Tick(object sender, System.EventArgs e)
{
	switch (MessageThread.ThreadState)
	{
		case ThreadState.Running:
			lbThreadState.Text="Running";
			break;
		case ThreadState.Suspended:
			lbThreadState.Text="Suspended";
			break;
		case ThreadState.WaitSleepJoin:
			lbThreadState.Text="WaitSleepJoin";
			break;
		case ThreadState.Stopped:
			lbThreadState.Text="Stopped";
			break;
		case ThreadState.Unstarted:
			lbThreadState.Text="UnStarted";
			break;
		default:
			lbThreadState.Text="Unknown";
			break;
	}
}

private void btOK_Click(object sender, System.EventArgs e)
{
	MessageThread.Start();
}

private void btCancel_Click(object sender, System.EventArgs e)
{
	RunThread=false;
}

private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
	RunThread=false;
	//MessageThread.Interrupt();
	MessageThread.Join();
}


If I click the Stop button (set RunThread to false) then close the dialog its fine, but If I close the dialog while the thread is runing it hangs...
I set the same variable to false in the Form1_Closing handler. I've tried using the IsBackground property, but this changes the ThreadState value to "12" apparently all the time.

I'm assuming that once I've hit the Closing Event handler that I'm at a point of no return or somthing with the worker thread, or something like that. Any suggestions ?
GeneralRe: Closing a Worker Thread when Dialog Closes Pin
scadaguy27-May-04 10:44
scadaguy27-May-04 10:44 
GeneralRe: Closing a Worker Thread when Dialog Closes Pin
Guinness4Strength27-May-04 10:48
Guinness4Strength27-May-04 10:48 
GeneralPorting from C# to VC++ Pin
Prakash Nadar27-May-04 5:09
Prakash Nadar27-May-04 5:09 
GeneralRe: Porting from C# to VC++ Pin
Guinness4Strength27-May-04 5:46
Guinness4Strength27-May-04 5:46 
GeneralRe: Porting from C# to VC++ Pin
Heath Stewart27-May-04 5:48
protectorHeath Stewart27-May-04 5:48 
GeneralRe: Porting from C# to VC++ Pin
Prakash Nadar27-May-04 8:07
Prakash Nadar27-May-04 8:07 
GeneralRe: Porting from C# to VC++ Pin
Heath Stewart27-May-04 8:11
protectorHeath Stewart27-May-04 8:11 
GeneralRe: Porting from C# to VC++ Pin
Prakash Nadar27-May-04 8:17
Prakash Nadar27-May-04 8:17 
GeneralCanon Makernote Pin
devries4827-May-04 3:29
devries4827-May-04 3:29 
GeneralRe: Canon Makernote Pin
Heath Stewart27-May-04 5:41
protectorHeath Stewart27-May-04 5:41 
GeneralRe: Copy Picture From Web Pin
Dave Kreskowiak27-May-04 4:08
mveDave Kreskowiak27-May-04 4:08 
GeneralString Mnipulation problem Pin
ali_naqvi27-May-04 3:01
ali_naqvi27-May-04 3:01 
GeneralRe: String Mnipulation problem Pin
scadaguy27-May-04 3:20
scadaguy27-May-04 3:20 
GeneralRe: String Mnipulation problem Pin
Judah Gabriel Himango27-May-04 3:58
sponsorJudah Gabriel Himango27-May-04 3:58 
Generalstring handilng problem Pin
ali_naqvi27-May-04 2:14
ali_naqvi27-May-04 2:14 
GeneralRe: string handilng problem Pin
bjoernen27-May-04 2:27
bjoernen27-May-04 2:27 
GeneralSystem Tray Application - WindowState Pin
MrEyes27-May-04 1:56
MrEyes27-May-04 1:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.