ProducerConsumer Threads

D

Daniel

Hi all

I wrote an App that checks some Emailaddresses from our Database ( more
than 500'000). So I wrote a Multithread App.

I have a Queue defined as follow:

ProducerConsumer prcEmailProcess = new ProducerConsumer();

I start the Threads with the following Command:

for (int i = 0; i < MaxThread; i++)
new Thread(new ThreadStart(EmailChecker)).Start();

Then I add for each Thread a null object to signalize the End:

for (int i = 0; i < MaxThread; i++)
prcEmailProcess.Produce(null);



The EmailChecker Code looks as follow:

void EmailChecker()
{
while (bolCheckEmail)
{
object o = prcEmailProcess.Consume();

if (o == null)
{
lock (listDNSLock)
{
if (intEnd.Equals(MaxThread - 1))
{
prcEmailProcess.Reset();
bolCheckEmail = false;
break;
}
else
{

intEnd++;
break;
}
}
}
else
{
objEmail = (ILLFEmail)o;

try
{
//Do somthing }
catch
{
//Handle the Error
}
finally
{
//Clean up
}
}
}
}


Now I would like to show on the MainForm in a lable which thread is
working on with Emailaddress.
I have for each thread a lable.

How can I do this?

I wait till the variable bolCheckEmail is false. But somtimes the
variable is false but the program seams still to run (when I exit the
main form, there is still a process running).
How can that be?

thanks for your help

Daniel
 
Z

Ziad Elmalki

The reason the app is still running is probably because a thread did not die. Maybe give your threads name and use the thread debug window in vs to see whats running.

Looks like you might only be killing the last thread??

if (intEnd.Equals(MaxThread - 1))
{
prcEmailProcess.Reset();
bolCheckEmail = false;
break;
}
else
{

intEnd++;
break;
}

You probably want to use events to communicate
between threads. Add a event to this class and have it raise when the address changes or whatever. The form subscribes to the event and updates whatever controls. Remember to use the Form.Invoke too.

Thanks
Ziad
 
D

Daniel

Ziad said:
The reason the app is still running is probably because a thread did not die. Maybe give your threads name and use the thread debug window in vs to see whats running.

Looks like you might only be killing the last thread??

if (intEnd.Equals(MaxThread - 1))
{
prcEmailProcess.Reset();
bolCheckEmail = false;
break;
}
else
{

intEnd++;
break;
}

You probably want to use events to communicate
between threads. Add a event to this class and have it raise when the address changes or whatever. The form subscribes to the event and updates whatever controls. Remember to use the Form.Invoke too.

Thanks
Ziad


Hi Ziad
Thanks for the answer.

With the

else
{
intEnd++;
break;
}


I quit the while loop and with this the Thread should quit (as far as I
understand). The bool variable I use onely to see when the last Thread
finishes.
The solution with events sounds interresting. Is there somwhere a easy
example?

cheers daniel
 

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