mutithreading in a Winforms app with CDO

S

Steve Smith

I have written an application that launches approximately 150 threads with
Thread.ThreadStart()

Each thread uses CDO 1.21 to logon to a different Exchange mailbox and
send/receive a number of mail messages, reporting back to the UI thread
through the use of a Queue object. When all messages that are expected have
been received, each thread sends a final update to the UI and the method
should exit, which should terminate the thread.

Everything works well for the most part, although I am doing battle with the
Outlook Security dialog on some mailboxes.

When all threads have completed their work, the Exit button on the UI is
enabled. When I click it, the Appilcation.Exit() function is called, and the
UI disappears, but the application continues to run. In the debug
environment eventually I get a thread deadlock message. At this point, there
should only be one thread running.

I have read numerous articles on threading and examined many samples and
cannot see anything wrong with the way I have coded this application.

If anyone has experienced this type of problem, or has a suggestion on how I
could try to nail this down, I would appreciate it. Is it possible the CDO
object is part of the problem?

Thanks in advance

Steve
 
L

Lars-Inge Tønnessen \(VJ# MVP\)

Please use Join on all thread before closing the main thread.
Put the references to all the threads in e.g an arraylist and run through
the list and call the join() method on all the threads.


Make sure you don't close any threads explicitly with any close statements
on the thread. Let the threads die on it's own.

Tell us how things are going.

Regards,
Lars-Inge Tønnessen
 
S

Steve Smith

Thanks for your feedback.

I did have an ArrayList of objects in my app, and had stored away the thread
object when it was launched, so
it was easy to add the following in my btnExit_click object:
for (int x = 0; x < mboxList.Count; x++)
{
myTestMailbox y = (myTestMailbox)mboxList[x];
System.Diagnostics.Debug.WriteLine("Joining thread : " +
x.ToString("00"));
y.theThread.Join();
}

On the first pass through the loop, the application stops on thread.Join().
After about a minute or so I get the following error:

----------------------------------------------
ContextSwitchDeadlock was detected
Message: The CLR has been unable to transition from COM context 0x1a1e90 to
COM context 0x1a27e8 for 60 seconds.
The thread that owns the destination context/apartment is most likely either
doing a non pumping wait or processing
a very long running operation without pumping Windows messages. This
situation generally has a negative performance
impact and may even lead to the application becoming non responsive or
memory usage accumulating continually over time.

To avoid this problem, all single threaded apartment (STA) threads should
use pumping wait primitives
(such as CoWaitForMultipleHandles) and routinely pump messages during long
running operations.
------------------------------------------------

The method that is running in each thread does not do anything specific to
terminate the thread, other than falling out of the While() loop
and calling the CDO Logoff() method to log off of the mailbox.

If you have any ideas what the problem could be I would appreciate your
feedback.

Thanks again

Steve
 

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