By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,890 Members | 1,286 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,890 IT Pros & Developers. It's quick & easy.

Wried problem

P: n/a
Hi,

I am using COM to automate Word. In the main thread of my program, I
store all the Document objects from Application.Documents. In one event
handler (the event generated by Word), if i get all the current
documents using Application.Documents, the objects I got are not same
ones stored previously. The event handler is called by some threads
generated by C#. But, if I get all the current documents using my own
threads, the ones returned are the same ones previously stored.

I did synchronize and all threads are MTA.

Can anyone please tell me why this is?

Thanks a lot

Sep 17 '06 #1
Share this Question
Share on Google+
11 Replies


P: n/a

"leiz" <le********@gmail.comwrote in message
news:11*********************@i42g2000cwa.googlegro ups.com...
| Hi,
|
| I am using COM to automate Word. In the main thread of my program, I
| store all the Document objects from Application.Documents. In one event
| handler (the event generated by Word), if i get all the current
| documents using Application.Documents, the objects I got are not same
| ones stored previously. The event handler is called by some threads
| generated by C#. But, if I get all the current documents using my own
| threads, the ones returned are the same ones previously stored.
|
| I did synchronize and all threads are MTA.
|
| Can anyone please tell me why this is?
|
| Thanks a lot
|

This is not the first time you ask this, please post a complete sample that
illustrates the issue, it's really hard to help you without seeing any code.

Note that you might get better answers if you post to the
microsoft.public.dotnet.framework.interop NG.

Willy.

Sep 18 '06 #2

P: n/a
Here goes the simplified code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Threading;
using Word = Microsoft.Office.Interop.Word;

namespace btest
{
public partial class Form1 : Form
{
private Word.Application m_WordApp;

public Form1()
{
InitializeComponent();
m_WordApp =
(Word.Application)Marshal.GetActiveObject("Word.Ap plication");
m_WordApp.WindowActivate += new
Microsoft.Office.Interop.Word.ApplicationEvents4_W indowActivateEventHandler(Word_WindowActivate);

Thread t = new Thread(new ThreadStart(thread));
t.SetApartmentState(ApartmentState.MTA);
t.Start();
}

private void Word_WindowActivate(Word.Document doc, Word.Window
win)
{
Word.Application app =
(Word.Application)Marshal.GetActiveObject("Word.Ap plication");
MessageBox.Show("In event handler " + (app == m_WordApp));
}

private void thread()
{
Word.Application app =
(Word.Application)Marshal.GetActiveObject("Word.Ap plication");
MessageBox.Show("In our thread " + (app == m_WordApp));
}
}
}

=============================================
the problem was -- in our thread the result is true, in the event
handler the result is false.

The main thread is MTA as well.

Willy Denoyette [MVP] wrote:
"leiz" <le********@gmail.comwrote in message
news:11*********************@i42g2000cwa.googlegro ups.com...
| Hi,
|
| I am using COM to automate Word. In the main thread of my program, I
| store all the Document objects from Application.Documents. In one event
| handler (the event generated by Word), if i get all the current
| documents using Application.Documents, the objects I got are not same
| ones stored previously. The event handler is called by some threads
| generated by C#. But, if I get all the current documents using my own
| threads, the ones returned are the same ones previously stored.
|
| I did synchronize and all threads are MTA.
|
| Can anyone please tell me why this is?
|
| Thanks a lot
|

This is not the first time you ask this, please post a complete sample that
illustrates the issue, it's really hard to help you without seeing any code.

Note that you might get better answers if you post to the
microsoft.public.dotnet.framework.interop NG.

Willy.
Sep 18 '06 #3

P: n/a
app == m_WordApp can never be the same, both are different object
references, more they point to different RCW's instance, so even using
Object.ReferenceEquals won't help either.
Guess you want to make sure both refer to the same instance of word, well,
the only option you have is check whether there is only one single Word
instance running using the Diagnostics.Process class. If there is more than
one instance, calling GetActiveObject will connect to the first instance in
the ROT, but this is not an hard guarantee.

Willy.

"leiz" <le********@gmail.comwrote in message
news:11*********************@e3g2000cwe.googlegrou ps.com...
| Here goes the simplified code:
|
| using System;
| using System.Collections.Generic;
| using System.ComponentModel;
| using System.Data;
| using System.Drawing;
| using System.Text;
| using System.Windows.Forms;
| using System.Runtime.InteropServices;
| using System.Threading;
| using Word = Microsoft.Office.Interop.Word;
|
| namespace btest
| {
| public partial class Form1 : Form
| {
| private Word.Application m_WordApp;
|
| public Form1()
| {
| InitializeComponent();
|
|
| m_WordApp =
| (Word.Application)Marshal.GetActiveObject("Word.Ap plication");
| m_WordApp.WindowActivate += new
|
Microsoft.Office.Interop.Word.ApplicationEvents4_W indowActivateEventHandler(Word_WindowActivate);
|
| Thread t = new Thread(new ThreadStart(thread));
| t.SetApartmentState(ApartmentState.MTA);
| t.Start();
| }
|
| private void Word_WindowActivate(Word.Document doc, Word.Window
| win)
| {
| Word.Application app =
| (Word.Application)Marshal.GetActiveObject("Word.Ap plication");
| MessageBox.Show("In event handler " + (app == m_WordApp));
| }
|
| private void thread()
| {
| Word.Application app =
| (Word.Application)Marshal.GetActiveObject("Word.Ap plication");
| MessageBox.Show("In our thread " + (app == m_WordApp));
| }
| }
| }
|
| =============================================
| the problem was -- in our thread the result is true, in the event
| handler the result is false.
|
| The main thread is MTA as well.
|
| Willy Denoyette [MVP] wrote:
| "leiz" <le********@gmail.comwrote in message
| news:11*********************@i42g2000cwa.googlegro ups.com...
| | Hi,
| |
| | I am using COM to automate Word. In the main thread of my program, I
| | store all the Document objects from Application.Documents. In one
event
| | handler (the event generated by Word), if i get all the current
| | documents using Application.Documents, the objects I got are not same
| | ones stored previously. The event handler is called by some threads
| | generated by C#. But, if I get all the current documents using my own
| | threads, the ones returned are the same ones previously stored.
| |
| | I did synchronize and all threads are MTA.
| |
| | Can anyone please tell me why this is?
| |
| | Thanks a lot
| |
| >
| This is not the first time you ask this, please post a complete sample
that
| illustrates the issue, it's really hard to help you without seeing any
code.
| >
| Note that you might get better answers if you post to the
| microsoft.public.dotnet.framework.interop NG.
| >
| Willy.
|
Sep 18 '06 #4

P: n/a
In the thread() method, the result is true and I believe that the
problem is C# thread apartment problem. Because if i used STA in main
thread, the result in thread() method is false as well.

By the way, I make sure that there is only one copy of word is running.
So, the Application object should point to the same one.
Willy Denoyette [MVP] wrote:
app == m_WordApp can never be the same, both are different object
references, more they point to different RCW's instance, so even using
Object.ReferenceEquals won't help either.
Guess you want to make sure both refer to the same instance of word, well,
the only option you have is check whether there is only one single Word
instance running using the Diagnostics.Process class. If there is more than
one instance, calling GetActiveObject will connect to the first instance in
the ROT, but this is not an hard guarantee.

Willy.

"leiz" <le********@gmail.comwrote in message
news:11*********************@e3g2000cwe.googlegrou ps.com...
| Here goes the simplified code:
|
| using System;
| using System.Collections.Generic;
| using System.ComponentModel;
| using System.Data;
| using System.Drawing;
| using System.Text;
| using System.Windows.Forms;
| using System.Runtime.InteropServices;
| using System.Threading;
| using Word = Microsoft.Office.Interop.Word;
|
| namespace btest
| {
| public partial class Form1 : Form
| {
| private Word.Application m_WordApp;
|
| public Form1()
| {
| InitializeComponent();
|
|
| m_WordApp =
| (Word.Application)Marshal.GetActiveObject("Word.Ap plication");
| m_WordApp.WindowActivate += new
|
Microsoft.Office.Interop.Word.ApplicationEvents4_W indowActivateEventHandler(Word_WindowActivate);
|
| Thread t = new Thread(new ThreadStart(thread));
| t.SetApartmentState(ApartmentState.MTA);
| t.Start();
| }
|
| private void Word_WindowActivate(Word.Document doc, Word.Window
| win)
| {
| Word.Application app =
| (Word.Application)Marshal.GetActiveObject("Word.Ap plication");
| MessageBox.Show("In event handler " + (app == m_WordApp));
| }
|
| private void thread()
| {
| Word.Application app =
| (Word.Application)Marshal.GetActiveObject("Word.Ap plication");
| MessageBox.Show("In our thread " + (app == m_WordApp));
| }
| }
| }
|
| =============================================
| the problem was -- in our thread the result is true, in the event
| handler the result is false.
|
| The main thread is MTA as well.
|
| Willy Denoyette [MVP] wrote:
| "leiz" <le********@gmail.comwrote in message
| news:11*********************@i42g2000cwa.googlegro ups.com...
| | Hi,
| |
| | I am using COM to automate Word. In the main thread of my program, I
| | store all the Document objects from Application.Documents. In one
event
| | handler (the event generated by Word), if i get all the current
| | documents using Application.Documents, the objects I got are not same
| | ones stored previously. The event handler is called by some threads
| | generated by C#. But, if I get all the current documents using my own
| | threads, the ones returned are the same ones previously stored.
| |
| | I did synchronize and all threads are MTA.
| |
| | Can anyone please tell me why this is?
| |
| | Thanks a lot
| |
| >
| This is not the first time you ask this, please post a complete sample
that
| illustrates the issue, it's really hard to help you without seeing any
code.
| >
| Note that you might get better answers if you post to the
| microsoft.public.dotnet.framework.interop NG.
| >
| Willy.
|
Sep 18 '06 #5

P: n/a

"leiz" <le********@gmail.comwrote in message
news:11**********************@e3g2000cwe.googlegro ups.com...
| In the thread() method, the result is true and I believe that the
| problem is C# thread apartment problem. Because if i used STA in main
| thread, the result in thread() method is false as well.
|
| By the way, I make sure that there is only one copy of word is running.
| So, the Application object should point to the same one.
|

Sorry I missed the point that your Main thread was MTA, well, the Main
thread MUST be STA in a Windows Forms application. Second even if you set
both to STA the result will be false, because both objects live in a
different apartment. When both are MTA they live in the same apartment and
the reference is the same, but as I said this is not a valid option.

Willy.

Sep 18 '06 #6

P: n/a
If I understand you correctly, as along as I dont use Form and set the
main thread to MTA, the result would be true, right?

I think that the problem is that the event handler is called by some
threads created by c# are in different apartment. However, when I print
out the ApartmentState in the event handler, it is a MTA, so they
should be in same apartment. Therefore, the objects should be same.

Below is the testing code. Before you run it, open a word document.
When running, swap word document and other windows. You will see that
the comparsion is true in our thread, but it is different in the event
hanlder thread.

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Runtime.InteropServices;

using Word = Microsoft.Office.Interop.Word;

namespace btest
{
class Program
{

[MTAThread]
static void Main(string[] args)
{
new Program();
Thread.Sleep(20000);
}

private Word.Application m_WordApp;
public Program()
{
m_WordApp =
(Word.Application)Marshal.GetActiveObject("Word.Ap plication");
m_WordApp.WindowActivate += new
Microsoft.Office.Interop.Word.ApplicationEvents4_W indowActivateEventHandler(Word_WindowActivate);

Thread t = new Thread(new ThreadStart(thread));
t.SetApartmentState(ApartmentState.MTA);
t.Start();
}

private void Word_WindowActivate(Word.Document doc, Word.Window
win)
{
Word.Application app =
(Word.Application)Marshal.GetActiveObject("Word.Ap plication");

Console.WriteLine(Thread.CurrentThread.GetApartmen tState().ToString());
Console.WriteLine("In event handler " + (app ==
m_WordApp));
}

private void thread()
{
Word.Application app =
(Word.Application)Marshal.GetActiveObject("Word.Ap plication");
Console.WriteLine("In our thread " + (app == m_WordApp));
Thread.Sleep(2000);
app =
(Word.Application)Marshal.GetActiveObject("Word.Ap plication");
Console.WriteLine("In our thread " + (app == m_WordApp));

}

}
}

Willy Denoyette [MVP] wrote:
"leiz" <le********@gmail.comwrote in message
news:11**********************@e3g2000cwe.googlegro ups.com...
| In the thread() method, the result is true and I believe that the
| problem is C# thread apartment problem. Because if i used STA in main
| thread, the result in thread() method is false as well.
|
| By the way, I make sure that there is only one copy of word is running.
| So, the Application object should point to the same one.
|

Sorry I missed the point that your Main thread was MTA, well, the Main
thread MUST be STA in a Windows Forms application. Second even if you set
both to STA the result will be false, because both objects live in a
different apartment. When both are MTA they live in the same apartment and
the reference is the same, but as I said this is not a valid option.

Willy.
Sep 19 '06 #7

P: n/a

"leiz" <le********@gmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
| If I understand you correctly, as along as I dont use Form and set the
| main thread to MTA, the result would be true, right?
|

Right.

| I think that the problem is that the event handler is called by some
| threads created by c# are in different apartment. However, when I print
| out the ApartmentState in the event handler, it is a MTA, so they
| should be in same apartment. Therefore, the objects should be same.
|

No they don't. The callback (the Sink interface) is running on a ThreadPool
thread which is created by the COM interop layer (through a CCW). The CCW
aggregates the free threaded marshaler and initializes the thread as NTA
(Neutral Threaded Apartment) which is always the case when you expose .NET
classes to COM clients.
That means that the callback can run on any thread, it has no apartment
requirements at all. The GetApartmentState returns MTA because it treats
both as compatible, but I would prefer the more correct NTA instead of MTA.
Anyway the context differs from the MTA context.
But, I guess you have a wrong idea about the references you are comparing
and what they point at.
app and m_WordApp are references to an object of type Word.Application,
however, this is not a reference to a COM object. It's a reference to a
context object (one per apartment type in the process) that holds a
hashtable holding RCW's which represents the real IUnknow/IDispatch COM
object interface.
So, when you compare app with m_WordApp, you compare whether they use the
same context, however, when they are equal, that doesn't mean that the COM
interfaces (the RCW) are the same, they are NOT. And because the RCW's
aren't the same doesn't mean that the object they refer to are different,
you see?
Honestly, I don't see what you are after by comparing these reference
really.
Willy.


Sep 19 '06 #8

P: n/a

"Willy Denoyette [MVP]" <wi*************@telenet.bewrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
|
| "leiz" <le********@gmail.comwrote in message
| news:11**********************@b28g2000cwb.googlegr oups.com...
|| If I understand you correctly, as along as I dont use Form and set the
|| main thread to MTA, the result would be true, right?
||
|
| Right.
|
|| I think that the problem is that the event handler is called by some
|| threads created by c# are in different apartment. However, when I print
|| out the ApartmentState in the event handler, it is a MTA, so they
|| should be in same apartment. Therefore, the objects should be same.
||
|
| No they don't. The callback (the Sink interface) is running on a
ThreadPool
| thread which is created by the COM interop layer (through a CCW). The CCW
| aggregates the free threaded marshaler and initializes the thread as NTA
| (Neutral Threaded Apartment) which is always the case when you expose .NET
| classes to COM clients.
| That means that the callback can run on any thread, it has no apartment
| requirements at all. The GetApartmentState returns MTA because it treats
| both as compatible, but I would prefer the more correct NTA instead of
MTA.
| Anyway the context differs from the MTA context.
| But, I guess you have a wrong idea about the references you are comparing
| and what they point at.
| app and m_WordApp are references to an object of type Word.Application,
| however, this is not a reference to a COM object. It's a reference to a
| context object (one per apartment type in the process) that holds a
| hashtable holding RCW's which represents the real IUnknow/IDispatch COM
| object interface.
| So, when you compare app with m_WordApp, you compare whether they use the
| same context, however, when they are equal, that doesn't mean that the COM
| interfaces (the RCW) are the same, they are NOT. And because the RCW's
| aren't the same doesn't mean that the object they refer to are different,
| you see?
| Honestly, I don't see what you are after by comparing these reference
| really.
|
|
| Willy.
Correction, after re-reading I noticed following error in above.
The callback (the Sink interface) is running on a ThreadPool
thread which is created by the COM interop layer (through a CCW).
This should read...

The callback (the Sink interface) is running on a native
thread which is created by the COM interop layer (through a CCW).

Willy.

Sep 19 '06 #9

P: n/a
Thank you very much. Now I understand what is going on there. I thought
NTA was windows2k only because of the statement in the dialog when you
create ATL objects. :p

I am kinda new to C#. Are there any methods to comparing two objects
across apartments.

Willy Denoyette [MVP] wrote:
"Willy Denoyette [MVP]" <wi*************@telenet.bewrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
|
| "leiz" <le********@gmail.comwrote in message
| news:11**********************@b28g2000cwb.googlegr oups.com...
|| If I understand you correctly, as along as I dont use Form and set the
|| main thread to MTA, the result would be true, right?
||
|
| Right.
|
|| I think that the problem is that the event handler is called by some
|| threads created by c# are in different apartment. However, when I print
|| out the ApartmentState in the event handler, it is a MTA, so they
|| should be in same apartment. Therefore, the objects should be same.
||
|
| No they don't. The callback (the Sink interface) is running on a
ThreadPool
| thread which is created by the COM interop layer (through a CCW). The CCW
| aggregates the free threaded marshaler and initializes the thread as NTA
| (Neutral Threaded Apartment) which is always the case when you expose .NET
| classes to COM clients.
| That means that the callback can run on any thread, it has no apartment
| requirements at all. The GetApartmentState returns MTA because it treats
| both as compatible, but I would prefer the more correct NTA instead of
MTA.
| Anyway the context differs from the MTA context.
| But, I guess you have a wrong idea about the references you are comparing
| and what they point at.
| app and m_WordApp are references to an object of type Word.Application,
| however, this is not a reference to a COM object. It's a reference to a
| context object (one per apartment type in the process) that holds a
| hashtable holding RCW's which represents the real IUnknow/IDispatch COM
| object interface.
| So, when you compare app with m_WordApp, you compare whether they use the
| same context, however, when they are equal, that doesn't mean that the COM
| interfaces (the RCW) are the same, they are NOT. And because the RCW's
| aren't the same doesn't mean that the object they refer to are different,
| you see?
| Honestly, I don't see what you are after by comparing these reference
| really.
|
|
| Willy.
Correction, after re-reading I noticed following error in above.
The callback (the Sink interface) is running on a ThreadPool
thread which is created by the COM interop layer (through a CCW).
This should read...

The callback (the Sink interface) is running on a native
thread which is created by the COM interop layer (through a CCW).

Willy.
Sep 19 '06 #10

P: n/a

"leiz" <le********@gmail.comwrote in message
news:11*********************@b28g2000cwb.googlegro ups.com...
| Thank you very much. Now I understand what is going on there. I thought
| NTA was windows2k only because of the statement in the dialog when you
| create ATL objects. :p
|

NTA was introduced with COM+ (W2K) and supported on all higher OS.
| I am kinda new to C#. Are there any methods to comparing two objects
| across apartments.
|

What exactly do you mean by comparing objects?
My guess is, that you want to compare interface pointers, right?
Well, here is how...

IntPtr pItf1 = Marshal.GetComInterfaceForObject(app,
typeof(Word.Application));
IntPtr pItf2 = Marshal.GetComInterfaceForObject(m_WordApp,
typeof(Word.Application));
if(pItf1 == pItf2)
// same ITF

Willy.


Sep 20 '06 #11

P: n/a
Thank you for th help.
leiz wrote:
Thank you very much. Now I understand what is going on there. I thought
NTA was windows2k only because of the statement in the dialog when you
create ATL objects. :p

I am kinda new to C#. Are there any methods to comparing two objects
across apartments.

Willy Denoyette [MVP] wrote:
"Willy Denoyette [MVP]" <wi*************@telenet.bewrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
|
| "leiz" <le********@gmail.comwrote in message
| news:11**********************@b28g2000cwb.googlegr oups.com...
|| If I understand you correctly, as along as I dont use Form and set the
|| main thread to MTA, the result would be true, right?
||
|
| Right.
|
|| I think that the problem is that the event handler is called by some
|| threads created by c# are in different apartment. However, when I print
|| out the ApartmentState in the event handler, it is a MTA, so they
|| should be in same apartment. Therefore, the objects should be same.
||
|
| No they don't. The callback (the Sink interface) is running on a
ThreadPool
| thread which is created by the COM interop layer (through a CCW). The CCW
| aggregates the free threaded marshaler and initializes the thread as NTA
| (Neutral Threaded Apartment) which is always the case when you expose .NET
| classes to COM clients.
| That means that the callback can run on any thread, it has no apartment
| requirements at all. The GetApartmentState returns MTA because it treats
| both as compatible, but I would prefer the more correct NTA instead of
MTA.
| Anyway the context differs from the MTA context.
| But, I guess you have a wrong idea about the references you are comparing
| and what they point at.
| app and m_WordApp are references to an object of type Word.Application,
| however, this is not a reference to a COM object. It's a reference to a
| context object (one per apartment type in the process) that holds a
| hashtable holding RCW's which represents the real IUnknow/IDispatch COM
| object interface.
| So, when you compare app with m_WordApp, you compare whether they use the
| same context, however, when they are equal, that doesn't mean that the COM
| interfaces (the RCW) are the same, they are NOT. And because the RCW's
| aren't the same doesn't mean that the object they refer to are different,
| you see?
| Honestly, I don't see what you are after by comparing these reference
| really.
|
|
| Willy.
Correction, after re-reading I noticed following error in above.
The callback (the Sink interface) is running on a ThreadPool
thread which is created by the COM interop layer (through a CCW).
This should read...

The callback (the Sink interface) is running on a native
thread which is created by the COM interop layer (through a CCW).

Willy.
Sep 21 '06 #12

This discussion thread is closed

Replies have been disabled for this discussion.