S
sarge
I would like to know how to perform simple multithreading.
I had created a simple form to test out if I was multithreading
properly, but got buggy results. Sometime the whole thig would lock
up when I got two threads going at the same time.
What I have is two text boxes (textBox1 and textBox2) and four
buttons(cmdStartThread1, cmdStartThread2, cmdStopThread1,
cmdStopThread2)
This is just a test to see if I am doing it properly.
If I only run one thread it works great. If I run two threads
sometimes it starts working good, but then at times when I start the
second thread while the other thread is running, it hangs the system
up.
Here is the code.
Can you see anything wrong that would be causing me problems running
two threads? I am new to multithreading.
private: void startThread1Test()
{
bThread1 = true;
int i = 0;
while(bThread1)
{
textBox1->Text = Convert::ToString(i);
i++;
if(i > 99999)
i = 0;
}
MessageBox::Show("Exiting Thread 1");
}
private: System::Void cmdStartThread1_Click(System::Object * sender,
System::EventArgs * e)
{
Thread *thread1 = new Thread(new ThreadStart(this,
startThread1Test));
thread1->Start();
}
private: void startThread2Test()
{
bThread2 = true;
int i = 0;
while(bThread2)
{
textBox2->Text = Convert::ToString(i);
i++;
if(i > 99999)
i = 0;
}
MessageBox::Show("Exiting Thread 2");
}
private: System::Void cmdStartThread2_Click(System::Object * sender,
System::EventArgs * e)
{
Thread *thread2 = new Thread(new ThreadStart(this,
startThread2Test));
thread2->Start();
}
private: System::Void cmdStopThread1_Click(System::Object * sender,
System::EventArgs * e)
{
bThread1 = false;
}
private: System::Void cmdStopThread2_Click(System::Object * sender,
System::EventArgs * e)
{
bThread2 = false;
}
I had created a simple form to test out if I was multithreading
properly, but got buggy results. Sometime the whole thig would lock
up when I got two threads going at the same time.
What I have is two text boxes (textBox1 and textBox2) and four
buttons(cmdStartThread1, cmdStartThread2, cmdStopThread1,
cmdStopThread2)
This is just a test to see if I am doing it properly.
If I only run one thread it works great. If I run two threads
sometimes it starts working good, but then at times when I start the
second thread while the other thread is running, it hangs the system
up.
Here is the code.
Can you see anything wrong that would be causing me problems running
two threads? I am new to multithreading.
private: void startThread1Test()
{
bThread1 = true;
int i = 0;
while(bThread1)
{
textBox1->Text = Convert::ToString(i);
i++;
if(i > 99999)
i = 0;
}
MessageBox::Show("Exiting Thread 1");
}
private: System::Void cmdStartThread1_Click(System::Object * sender,
System::EventArgs * e)
{
Thread *thread1 = new Thread(new ThreadStart(this,
startThread1Test));
thread1->Start();
}
private: void startThread2Test()
{
bThread2 = true;
int i = 0;
while(bThread2)
{
textBox2->Text = Convert::ToString(i);
i++;
if(i > 99999)
i = 0;
}
MessageBox::Show("Exiting Thread 2");
}
private: System::Void cmdStartThread2_Click(System::Object * sender,
System::EventArgs * e)
{
Thread *thread2 = new Thread(new ThreadStart(this,
startThread2Test));
thread2->Start();
}
private: System::Void cmdStopThread1_Click(System::Object * sender,
System::EventArgs * e)
{
bThread1 = false;
}
private: System::Void cmdStopThread2_Click(System::Object * sender,
System::EventArgs * e)
{
bThread2 = false;
}