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

  • Thread starter Thread starter Julien @ BE
  • Start date Start date
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");
}

}
}
 
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.
 
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.
 
Back
Top