473,663 Members | 2,705 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Share CDO Session Among Threads

In VB.NET using CDO, I'd like to allow multiple threads to share a single
MAPI.Session object. If I declare and instantiate sessions within each
thread, I'm OK (although this negates the efficiency I'm looking to add).
But when I declare, e.g.,

Public objSession As MAPI.Session

I can't access objSession from a worker thread.

Thanks in advance for any advice.

Dave James
Nov 20 '05 #1
6 1992
If I'm not mistaken, the CDO library you're using is marked for single
thread access only. When the library starts, it initializes COM and doesn't
pass the MULTITHREADED flag to CoInitilizeEx. Not necessarily a bug - the
code just doesn't support multi-threaded access.

-Rob Teixeira [MVP]

"Gary Lee" <gl***@earthlin k.net> wrote in message
news:z1******** **********@news read1.news.pas. earthlink.net.. .
In VB.NET using CDO, I'd like to allow multiple threads to share a single
MAPI.Session object. If I declare and instantiate sessions within each
thread, I'm OK (although this negates the efficiency I'm looking to add).
But when I declare, e.g.,

Public objSession As MAPI.Session

I can't access objSession from a worker thread.

Thanks in advance for any advice.

Dave James

Nov 20 '05 #2
Gary,
As Rob suggested, CDO (within .NET) is bound to an STA thread, which means
that it can not be used with an MTA thread (it throws an exception if you
try).

I recently came across the following http://g8.cx/mapi/ "Extended MAPI with
C#", which may allow you to use multiple threads (I have not yet tried the
above library).

Hope this helps
Jay

"Gary Lee" <gl***@earthlin k.net> wrote in message
news:z1******** **********@news read1.news.pas. earthlink.net.. .
In VB.NET using CDO, I'd like to allow multiple threads to share a single
MAPI.Session object. If I declare and instantiate sessions within each
thread, I'm OK (although this negates the efficiency I'm looking to add).
But when I declare, e.g.,

Public objSession As MAPI.Session

I can't access objSession from a worker thread.

Thanks in advance for any advice.

Dave James

Nov 20 '05 #3
Thanks Rob and Jay for the replies. I'll check out http://g8.cx/mapi/.

In the meantime, I made things work by creating one session per thread --
not as elegant as I'd like, but it works.


"Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> wrote in message
news:eW******** *****@TK2MSFTNG P11.phx.gbl...
Gary,
As Rob suggested, CDO (within .NET) is bound to an STA thread, which means
that it can not be used with an MTA thread (it throws an exception if you
try).

I recently came across the following http://g8.cx/mapi/ "Extended MAPI with C#", which may allow you to use multiple threads (I have not yet tried the
above library).

Hope this helps
Jay

"Gary Lee" <gl***@earthlin k.net> wrote in message
news:z1******** **********@news read1.news.pas. earthlink.net.. .
In VB.NET using CDO, I'd like to allow multiple threads to share a single MAPI.Session object. If I declare and instantiate sessions within each
thread, I'm OK (although this negates the efficiency I'm looking to add). But when I declare, e.g.,

Public objSession As MAPI.Session

I can't access objSession from a worker thread.

Thanks in advance for any advice.

Dave James


Nov 20 '05 #4
Hi Gary,

"Gary Lee" <gl***@earthlin k.net> wrote in message
news:Go******** ********@newsre ad2.news.pas.ea rthlink.net...
Thanks Rob and Jay for the replies. I'll check out http://g8.cx/mapi/.

In the meantime, I made things work by creating one session per thread --
not as elegant as I'd like, but it works.
that is in many situations the more elegant solution. Whenever you have
threads own the resources they use you get them decoupled which keeps you
away from synchronization problems.

I think the explanations for the errors you got might be a bit inaccurate:
it is not the COM component CDO which initializes a thread as STA or MTA -
that would not work because you cannot change that setting later on so using
two objects with different apartment models in one thread would result in a
conflict. It is always the client of the COM objects who has to set the
apartment model for each thread it creates. For your main UI thread you can
do this with attributes but for dynamically created threads you will have to
set the ApartmentState of that thread prior to starting it - I guess that
"configures " the call to CoInitialize or CoInitializeEx. If you don't set
that property to STA or MTA COM will not get initialized on that thread. So
any COM call will fail on that thread because of COM not being initialized.

I think you should even be able to use CDO inproc from an MTA thread. This
should result in a proxy object in the MTA thread which uses thread message
queues to communicate with the real CDO object created in an STA object.
Uups: just looked up my CDO apartment registration: it is set up as "Both"
which means - ahhh come on my developers are clever guys you can use me in
STA or MTA I don't care I always work (well this is a "descriptio n" in the
registry of how it should be :) ). So it will even execute directly in the
MTA thread.

JUST A TECHNICAL DEMO, DONT DO THIS IN PRODUCTION CODE: just give it a try:
set the ApartmentState property of your thread to MTA and you should be able
to use a CDO Session created in one of those in all the others. What will
not work is passing an object reference between STA (your main UI thread
e.g.) and MTA. If you want to do that you will need to marshal the object
reference. CoMarshalInterT readInterfaceIn Stream and
CoGetInterfaceA ndReleaseStream or use the GIT (global interface table).

I strongly don't recommend that with CDO because you can run in all kinds of
problems with CDO as it is not really designed as a multithreaded server
library - I ran into so much issues with it just because I wanted to use an
easy COM component instead of ExMapi in a server app :(

Bye,
SvenC


"Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> wrote in message
news:eW******** *****@TK2MSFTNG P11.phx.gbl...
Gary,
As Rob suggested, CDO (within .NET) is bound to an STA thread, which means
that it can not be used with an MTA thread (it throws an exception if you try).

I recently came across the following http://g8.cx/mapi/ "Extended MAPI

with
C#", which may allow you to use multiple threads (I have not yet tried the above library).

Hope this helps
Jay

"Gary Lee" <gl***@earthlin k.net> wrote in message
news:z1******** **********@news read1.news.pas. earthlink.net.. .
In VB.NET using CDO, I'd like to allow multiple threads to share a

single MAPI.Session object. If I declare and instantiate sessions within each thread, I'm OK (although this negates the efficiency I'm looking to add). But when I declare, e.g.,

Public objSession As MAPI.Session

I can't access objSession from a worker thread.

Thanks in advance for any advice.

Dave James



Nov 20 '05 #5
Sven,
I think the explanations for the errors you got might be a bit inaccurate: I'm not certain I've seen a totally accurate explanation yet ;-)

set the ApartmentState of that thread prior to starting it - I guess that
"configures " the call to CoInitialize or CoInitializeEx. If you don't set Agreed setting the ApartmentState of a Thread is how you set MTA or STA.

However my understanding is MAPI (CDO) requires a call to MapiInitialize in
addition to CoInitialize. However my limited experimentation & Rob's
comments suggests that MapiInitialize is called with STA only. Hence
MAPI.Session then throws an exception, as it is attempting to call
MapiInitilize with parameters with are incompatible with the previously set
ApartmentState. ..
JUST A TECHNICAL DEMO, DONT DO THIS IN PRODUCTION CODE: just give it a try: set the ApartmentState property of your thread to MTA and you should be able
I suspect this will appear to work, I'm concerned what is really happening
(no call to CoInitialize or MapiInitialize) ...

Hope this helps
Jay

"Sven Carstensen" <Cv************ *@moc.tfg> wrote in message
news:Od******** ******@TK2MSFTN GP11.phx.gbl... Hi Gary,

"Gary Lee" <gl***@earthlin k.net> wrote in message
news:Go******** ********@newsre ad2.news.pas.ea rthlink.net...
Thanks Rob and Jay for the replies. I'll check out http://g8.cx/mapi/.

In the meantime, I made things work by creating one session per thread --
not as elegant as I'd like, but it works.
that is in many situations the more elegant solution. Whenever you have
threads own the resources they use you get them decoupled which keeps you
away from synchronization problems.

I think the explanations for the errors you got might be a bit inaccurate:
it is not the COM component CDO which initializes a thread as STA or MTA -
that would not work because you cannot change that setting later on so

using two objects with different apartment models in one thread would result in a conflict. It is always the client of the COM objects who has to set the
apartment model for each thread it creates. For your main UI thread you can do this with attributes but for dynamically created threads you will have to set the ApartmentState of that thread prior to starting it - I guess that
"configures " the call to CoInitialize or CoInitializeEx. If you don't set
that property to STA or MTA COM will not get initialized on that thread. So any COM call will fail on that thread because of COM not being initialized.
I think you should even be able to use CDO inproc from an MTA thread. This
should result in a proxy object in the MTA thread which uses thread message queues to communicate with the real CDO object created in an STA object.
Uups: just looked up my CDO apartment registration: it is set up as "Both"
which means - ahhh come on my developers are clever guys you can use me in
STA or MTA I don't care I always work (well this is a "descriptio n" in the
registry of how it should be :) ). So it will even execute directly in the
MTA thread.

JUST A TECHNICAL DEMO, DONT DO THIS IN PRODUCTION CODE: just give it a try: set the ApartmentState property of your thread to MTA and you should be able to use a CDO Session created in one of those in all the others. What will
not work is passing an object reference between STA (your main UI thread
e.g.) and MTA. If you want to do that you will need to marshal the object
reference. CoMarshalInterT readInterfaceIn Stream and
CoGetInterfaceA ndReleaseStream or use the GIT (global interface table).

I strongly don't recommend that with CDO because you can run in all kinds of problems with CDO as it is not really designed as a multithreaded server
library - I ran into so much issues with it just because I wanted to use an easy COM component instead of ExMapi in a server app :(

Bye,
SvenC


"Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> wrote in message news:eW******** *****@TK2MSFTNG P11.phx.gbl...
Gary,
As Rob suggested, CDO (within .NET) is bound to an STA thread, which

means that it can not be used with an MTA thread (it throws an exception if you try).

I recently came across the following http://g8.cx/mapi/ "Extended MAPI

with
C#", which may allow you to use multiple threads (I have not yet tried the above library).

Hope this helps
Jay

"Gary Lee" <gl***@earthlin k.net> wrote in message
news:z1******** **********@news read1.news.pas. earthlink.net.. .
> In VB.NET using CDO, I'd like to allow multiple threads to share a

single
> MAPI.Session object. If I declare and instantiate sessions within each > thread, I'm OK (although this negates the efficiency I'm looking to

add).
> But when I declare, e.g.,
>
> Public objSession As MAPI.Session
>
> I can't access objSession from a worker thread.
>
> Thanks in advance for any advice.
>
> Dave James
>
>



Nov 20 '05 #6
Hi Jay,

"Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> wrote in message
news:e3******** ******@TK2MSFTN GP12.phx.gbl...
Sven,
I think the explanations for the errors you got might be a bit inaccurate:
I'm not certain I've seen a totally accurate explanation yet ;-)
one could get the fealing that MS would like to forget about CDO 1.21 all
together ;-)
set the ApartmentState of that thread prior to starting it - I guess that "configures " the call to CoInitialize or CoInitializeEx. If you don't

set Agreed setting the ApartmentState of a Thread is how you set MTA or STA.

However my understanding is MAPI (CDO) requires a call to MapiInitialize in addition to CoInitialize. However my limited experimentation & Rob's
comments suggests that MapiInitialize is called with STA only. Hence
MAPI.Session then throws an exception, as it is attempting to call
MapiInitilize with parameters with are incompatible with the previously set ApartmentState. ..


So true: I forgot MAPIInitialize - one would have to call it with the
MAPI_NO_COINIT flag to get it working in an MTA thread.

When oh when will we get a good replacment for CDO/ExMAPI with its client
side notifications and message file format.

Does WebDAV qualify as a candidate for saving messages to files? I like
ExMapi when it comes to its internal knowledge to ignore read only or
computed properties when I use OpenIMsgOnIStg to copy a message completely
to a file based storage.

<snip>

Bye,
SvenC
Nov 20 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2125
by: Bonj | last post by:
H I've got the following problem - I need to have an aspx page with two frames, although the question isn't necessarily about the workings of the frames, more session variables... the frames consist of one header (not resizable) and one main (resizable). The source for the "main" page needs to be of the form "WebForm3.aspx?...." with parameters to be determined at runtime. I'm ok with this, I've gone down the route of puttin src="<%=...
1
319
by: .NET Follower | last post by:
hi, i want to session state among different webprojects in my solution... any one has the solution...... -- Thanks and Regards, Amit Agarwal
3
2967
by: CharlieHoo | last post by:
Hello, How to share session state between two or more ASP.NET applications in the same server or different ones? Thanks a lot. Charlie
2
1580
by: jeff_carver | last post by:
I've been looking at a number of threads that seem to address this issue, but they all seem to vector off into the weeds. So, can anyone provide a definitive Boolean answer to the question, "Is it possible for a JSP page and an ASP.NET page to communicate with each other via session state variables?" TIA, Jeff Carver
7
1160
by: Kejpa | last post by:
Hi, I'm logging the values my app is producing, in order to keep the logs small I zip them hourly. My problem lies in that two (or more) different objects discover that the hour has changed and each tries to start a thread to create the hourly zip. The runner up discovers that there is no file and creates it, but during this time the first thread already has created the file and I get an "File already exists"-error. Basically, I want...
5
2168
by: Joe | last post by:
I have an application which runs in a non-secure environment. I also have an application that runs in a secure environment (both on the same machine). Is there any way to share the session data for this? Most of the site allows the user to add things to a cart (non-secure), once they choose to check-out, I need this information which was stored in the session to be read by the payment page(secured). Hope this makes sense. It's probably...
3
11576
by: Sako | last post by:
I am trying to teach myself perl by writing a program I've been meaning to implement, so I am pretty green in perl. I'm having problems sharing filehandles opened by a thread - been RTFM for a few days, but am having no luck. I am attempting to write a threaded server program that listens on a socket for requests, then passes the socket's filehandle to an event processor routine, while the listener thread keeps on listening. However, I...
3
1821
by: panic | last post by:
hello, In my web app when some button is clicked, a new thread is created to import data and that thread creates some threads too, inside those threads some session variables are updated, but another aspx page can´t see the updates, this happends only if i set the sessionstate mode to stateserver, with inproc mode all works fine, i use session variables in other aspx pages and all works fine(but don´t use threads to read/update...
3
2756
by: rajkpy | last post by:
Hi Experts, I have created a login page in php called login.php, it accepts username, password and domain and then does some authentication. IF authentication succeeds it starts a php session and sets a session attribute. In my php application, i check this session attribute in all the pages to find out whether the user has logged in or not. Now I have another application entirely written in html. Now I want to call this login.php page when...
0
8435
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8345
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8768
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8633
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6186
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5655
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4348
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2763
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1754
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.