CDO & threading : Cannot change thread mode after it is set.

J

Julien @ BE

When trying to run this code, I get the following error message :
Unhandled Exception: System.Runtime.InteropServices.COMException
(0x80010106): Cannot change thread mode after it is set.

1. Anyone an idea why ?
2.Does anyone knows a better way than CDO to connect a mailbox on a
Exchange 5.5 server ?


Thanks


using System;
using System.Threading;

namespace MailConnections
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{


/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{


Class1 c1 = new Class1();
Thread t1 = new Thread(new ThreadStart(c1.connect));

//c1.connect(); //works perfect
t1.Start();

Console.ReadLine();
}

public void connect()
{
MAPI.Session session = new MAPI.Session();
Console.WriteLine("Session created");
}

}
}
 
W

Willy Denoyette [MVP]

Julien @ BE said:
When trying to run this code, I get the following error message :
Unhandled Exception: System.Runtime.InteropServices.COMException
(0x80010106): Cannot change thread mode after it is set.

1. Anyone an idea why ?
2.Does anyone knows a better way than CDO to connect a mailbox on a
Exchange 5.5 server ?


Thanks


using System;
using System.Threading;

namespace MailConnections
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{


/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{


Class1 c1 = new Class1();
Thread t1 = new Thread(new ThreadStart(c1.connect));

//c1.connect(); //works perfect
t1.Start();

Console.ReadLine();
}

public void connect()
{
MAPI.Session session = new MAPI.Session();
Console.WriteLine("Session created");
}

}
}

CDO is STA only. Initialize you thread as STA before starting and your
problem will be gone.
Not sure why you need to run this on a separate thread though, seems like
everyone needs multiple threads these day's.

Willy.
 
J

Julien @ BE

Thanks Willy, it works now perfect...

I'm developping an application that needs to make several logons on an
exchange server and do several operations simultaneously... that's why
multiple threads.
 

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

Similar Threads


Top