|
-
April 14th, 2009, 10:11 PM
#1
another theread Q
1-How do u figure out in teh main thread that teh worker thread is done its job ?
2- not sure what exactly is teh purpose of Join ..
what attribute /methdod needs to eb checked inorder to enable diable some button upon the worker threads completion.
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace ANZ_SHIELD_MIGBAU
{
public partial class Form1 : Form
{
Worker workerObject;
Thread workerThread;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
btnProcess.Enabled = true;
btnStop.Enabled = true;
}
private void btnProcess_Click(object sender, EventArgs e)
{
btnProcess.Enabled = false;
btnStop.Enabled = true;
textBox1.Text = "";
workerObject = new Worker();
workerThread = new Thread(workerObject.DoWork);
workerThread.Start();
while (!workerThread.IsAlive) ;
Thread.Sleep(1);
?????????????????????
// Use the Join method to block the current thread
// until the object's thread terminates.
// workerThread.Join();
}
private void btnStop_Click(object sender, EventArgs e)
{
// Request that the worker thread stop itself:
workerObject.RequestStop();
textBox1.Text += "process stopped" +workerObject.Get().ToString();
btnProcess.Enabled = true;
btnStop.Enabled = false;
}
}
public class Worker
{
public void DoWork()
{
_status = false;
for (int j = 0; j < 200; j++)
{
for (i = 0; i < 32000; i++)
{
if (_shouldStop)
break;
}
if (_shouldStop)
break;
}
if (_shouldStop)
MessageBox.Show("process was interrupted");
_status = true;
}
public void RequestStop()
{
_shouldStop = true;
}
public int Get()
{
return i;
}
public bool GetStatus()
{
return _shouldStop;
}
private volatile bool _shouldStop;
public bool _status;
private int i = 0;
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|