Hi,
I have used a sample from MSDN, which is multithreading application and it is Console Application.
I want to convert this ConsoleApplication into WindowsApplication.
The orginalcode(In ConsoleApplication) is like this ,in MSDN:
using System;
using System.Threading;
public class Worker
{
// This method will be called when the thread is started.
public void DoWork()
{
while (!_shouldStop)
{
Console.WriteLine("worker thread: working...");
}
Console.WriteLine("worker thread: terminating gracefully.");
}
public void RequestStop()
{
_shouldStop = true;
}
// Volatile is used as hint to the compiler that this data
// member will be accessed by multiple threads.
private volatile bool _shouldStop;
}
public class WorkerThreadExample
{
static void Main()
{
// Create the thread object. This does not start the thread.
Worker workerObject = new Worker();
Thread workerThread = new Thread(workerObject.DoWork);
// Start the worker thread.
workerThread.Start();
Console.WriteLine("main thread: Starting worker thread...");
// Loop until worker thread activates.
while (!workerThread.IsAlive);
// Put the main thread to sleep for 1 millisecond to
// allow the worker thread to do some work:
Thread.Sleep(1);
// Request that the worker thread stop itself:
workerObject.RequestStop();
// Use the Join method to block the current thread
// until the object's thread terminates.
workerThread.Join();
Console.WriteLine("main thread: Worker thread has terminated.");
}
}
Sample Output
|
main thread: starting worker thread...
worker thread: working...
worker thread: working...
worker thread: working...
worker thread: working...
worker thread: working...
worker thread: working...
worker thread: working...
worker thread: working...
worker thread: working...
worker thread: working...
worker thread: working...
worker thread: terminating gracefully...
main thread: worker thread has terminated |
So as to convert ConsoleApplication to WindowsApplication
I have changed some code
My code looks like this:
My form is having a button,a richtextbox
I am starting the process in button click event.
I am populating the messages in richtextbox.
My code
-----------------
namespace CreateAndTerminateThreads
{
public delegate void StatusHandler(string progress);
public partial class Form1 : Form
{
Worker workerObject = null;
public Form1()
{
InitializeComponent();
workerObject = new Worker();
}
//Form Load
private void Form1_Load(object sender, EventArgs e)
{
workerObject.Status += new StatusHandler(worker_Status);
}
private void btnStartProcess_Click(object sender, EventArgs e)
{
// Create the thread object. This does not start the thread.
Thread workerThread = new Thread(workerObject.DoWork);
// Start the worker thread.
workerThread.Start();
richTextBox1.AppendText("main thread: Starting worker thread..." + Environment.NewLine);
// Loop until worker thread activates.
//Eventhough the
while (!workerThread.IsAlive) ;
// Put the main thread to sleep for 1 millisecond to
// allow the worker thread to do some work:
Thread.Sleep(1);
// Request that the worker thread stop itself:
workerObject.RequestStop();
// Use the Join method to block the current thread
// until the object's thread terminates.
workerThread.Join();
richTextBox1.AppendText("main thread: Worker thread has terminated." + Environment.NewLine);
richTextBox1.Refresh();
}
void worker_Status(string progress)
{
if ( richTextBox1.InvokeRequired)
{
richTextBox1.BeginInvoke(new StatusHandler(worker1_Status), new object[] { progress });
// If I use, richTextBox1.Invoke(new StatusHandler(worker1_Status), new object[] { progress });
//Output is not comming.
}
else
{
richTextBox1.AppendText(progress + Environment.NewLine);
richTextBox1.Refresh();
}
}
}
public class Worker
{
public event StatusHandler Status;
// This method will be called when the thread is started.
public void DoWork()
{
while (!_shouldStop)
{
Status("worker thread: working..."+i.ToString ());
}
Status("worker thread: terminating gracefully" );
}
public void RequestStop()
{
_shouldStop = true;
}
// Volatile is used as hint to the compiler that this data
// member will be accessed by multiple threads.
private volatile bool _shouldStop ;
}
}
Output
---------------------
main thread: Starting worker thread...
main thread: Worker thread has terminated.
worker thread: working
worker thread: working
worker thread: working
worker thread: working
worker thread: working
worker thread: working
worker thread: working
worker thread: working
worker thread: working
worker thread: working
worker thread: working
worker thread: working
worker thread: working
worker thread: working
worker thread: working
worker thread: working
worker thread: working
worker thread: working
worker thread: working
worker thread: working
worker thread: terminating gracefully
Actullay in the output,the second line should come as a lastline.
What is problem in my code ?
If problem exists,How can I solve that ?
Kindly help me....
Waiting for the reply.....
With Regards,
Viswa.