Simple Multithreading

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;
}
 
B

Beeeeeves

you shouldn't use multithreading just for the sake of it, which seems to be
exactly what you're attempting to be doing. You'll only face certain doom,
and frustration. And everyone else will face frustration when you make
posts.

sarge said:
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;
}
 
S

Steve McLellan

You can't do stuff to UI controls or forms from any old thread; you need to
use Control.Invoke and the other related functions. Otherwise you will, as
you now know, risk system hangs and all sorts of bad news.

Steve

sarge said:
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;
}
 
S

sarge

Beeeeeveswrote:
you shouldn't use multithreading just for the sake of it, which seems
to be
exactly what you're attempting to be doing. You'll only face certain doom,
and frustration. And everyone else will face frustration when you make
posts.

So do you suggest I try and learn it when it is a necessity? Why
bother learning something new, right?

First off, if you look at the program you will see it is nothing more
then a simple multithreading test. So in a sense you are exactly
right. The program is multithreading for the sake of multithreading.
Of course that is always the point of a small example anyway! It is
an attempt to learn something new!

And if the post causes you any frustration, by all means do not
respond!

In the mean time I have learned how to use Begin invoke. Using an
example from someone elsewhere on the internet on using begin invoke
I came up with this.


static void Thread1Function()
{
try
{
for ( int i = 0; i < 100000; i++)
{
String* s = String::Format("Step
{0}", __box(i));

Object* p[] = new Object*[1];
p[0] = s;

pMainForm->BeginInvoke(pMainForm->m_DelegateAddString1,
p);

Thread::Sleep(10);
}
}
catch ( Exception* )
{
}

}

static void Thread2Function()
{
try
{
for ( int i = 0; i < 100000; i++)
{
String* s = String::Format("Step
{0}", __box(i));

Object* p[] = new Object*[1];
p[0] = s;

pMainForm->BeginInvoke(pMainForm->m_DelegateAddString2,
p);

Thread::Sleep(10);
}
}
catch ( Exception* )
{
}

}

__delegate void DelegateAddString(String* s);

DelegateAddString* m_DelegateAddString1;
DelegateAddString* m_DelegateAddString2;

Thread* m_pThread1;
Thread* m_pThread2;

static Form1* pMainForm;

void AddString1(String* s)
{
textBox1->Text = s;
}
void AddString2(String* s)
{
textBox2->Text = s;
}

void StartThread1()
{
StopThread1();

pMainForm = this;
ThreadStart* thread1Delegate = new ThreadStart(0,
Thread1Function);
m_pThread1 = new Thread(thread1Delegate);
m_pThread1->IsBackground = true;
m_pThread1->Start();
}

void StopThread1()
{
if ( m_pThread1 != NULL )
{
m_pThread1->Abort();
m_pThread1->Join();
m_pThread1 = NULL;
}
}

void StartThread2()
{
StopThread2();

pMainForm = this;
ThreadStart* thread2Delegate = new ThreadStart(0,
Thread2Function);
m_pThread2 = new Thread(thread2Delegate);
m_pThread2->IsBackground = true;
m_pThread2->Start();
}

void StopThread2()
{
if ( m_pThread2 != NULL )
{
m_pThread2->Abort();
m_pThread2->Join();
m_pThread2 = NULL;
}
}

private: System::Void
bnStartThread1_Click(System::Object * sender,
System::EventArgs * e)
{
StartThread1();
}

private: System::Void
bnStopThread1_Click(System::Object * sender,
System::EventArgs * e)
{
StopThread1();
}

private: System::Void
bnStartThread2_Click(System::Object * sender,
System::EventArgs * e)
{
StartThread2();
}

private: System::Void
bnStopThread2_Click(System::Object * sender,
System::EventArgs * e)
{
StopThread2();
}
 
S

Steve McLellan

That's the chap, well done. Incidentally, there's a good article on
threading in .NET at the link below. It's written by one of the newsgroup
regulars, but I can't remember who (sorry!).

http://www.yoda.arachsys.com/csharp/multithreading.html

Steve

sarge said:
Beeeeeveswrote:
you shouldn't use multithreading just for the sake of it, which seems
to be
exactly what you're attempting to be doing. You'll only face certain doom,
and frustration. And everyone else will face frustration when you make
posts.

So do you suggest I try and learn it when it is a necessity? Why
bother learning something new, right?

First off, if you look at the program you will see it is nothing more
then a simple multithreading test. So in a sense you are exactly
right. The program is multithreading for the sake of multithreading.
Of course that is always the point of a small example anyway! It is
an attempt to learn something new!

And if the post causes you any frustration, by all means do not
respond!

In the mean time I have learned how to use Begin invoke. Using an
example from someone elsewhere on the internet on using begin invoke
I came up with this.


static void Thread1Function()
{
try
{
for ( int i = 0; i < 100000; i++)
{
String* s = String::Format("Step
{0}", __box(i));

Object* p[] = new Object*[1];
p[0] = s;

pMainForm->BeginInvoke(pMainForm->m_DelegateAddString1,
p);

Thread::Sleep(10);
}
}
catch ( Exception* )
{
}

}

static void Thread2Function()
{
try
{
for ( int i = 0; i < 100000; i++)
{
String* s = String::Format("Step
{0}", __box(i));

Object* p[] = new Object*[1];
p[0] = s;

pMainForm->BeginInvoke(pMainForm->m_DelegateAddString2,
p);

Thread::Sleep(10);
}
}
catch ( Exception* )
{
}

}

__delegate void DelegateAddString(String* s);

DelegateAddString* m_DelegateAddString1;
DelegateAddString* m_DelegateAddString2;

Thread* m_pThread1;
Thread* m_pThread2;

static Form1* pMainForm;

void AddString1(String* s)
{
textBox1->Text = s;
}
void AddString2(String* s)
{
textBox2->Text = s;
}

void StartThread1()
{
StopThread1();

pMainForm = this;
ThreadStart* thread1Delegate = new ThreadStart(0,
Thread1Function);
m_pThread1 = new Thread(thread1Delegate);
m_pThread1->IsBackground = true;
m_pThread1->Start();
}

void StopThread1()
{
if ( m_pThread1 != NULL )
{
m_pThread1->Abort();
m_pThread1->Join();
m_pThread1 = NULL;
}
}

void StartThread2()
{
StopThread2();

pMainForm = this;
ThreadStart* thread2Delegate = new ThreadStart(0,
Thread2Function);
m_pThread2 = new Thread(thread2Delegate);
m_pThread2->IsBackground = true;
m_pThread2->Start();
}

void StopThread2()
{
if ( m_pThread2 != NULL )
{
m_pThread2->Abort();
m_pThread2->Join();
m_pThread2 = NULL;
}
}

private: System::Void
bnStartThread1_Click(System::Object * sender,
System::EventArgs * e)
{
StartThread1();
}

private: System::Void
bnStopThread1_Click(System::Object * sender,
System::EventArgs * e)
{
StopThread1();
}

private: System::Void
bnStartThread2_Click(System::Object * sender,
System::EventArgs * e)
{
StartThread2();
}

private: System::Void
bnStopThread2_Click(System::Object * sender,
System::EventArgs * e)
{
StopThread2();
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top