Wried problem

  • Thread starter Thread starter leiz
  • Start date Start date
L

leiz

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
 
| 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.
 
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.Application");
m_WordApp.WindowActivate += new
Microsoft.Office.Interop.Word.ApplicationEvents4_WindowActivateEventHandler(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.Application");
MessageBox.Show("In event handler " + (app == m_WordApp));
}

private void thread()
{
Word.Application app =
(Word.Application)Marshal.GetActiveObject("Word.Application");
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.
 
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.

| 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.Application");
| m_WordApp.WindowActivate += new
|
Microsoft.Office.Interop.Word.ApplicationEvents4_WindowActivateEventHandler(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.Application");
| MessageBox.Show("In event handler " + (app == m_WordApp));
| }
|
| private void thread()
| {
| Word.Application app =
| (Word.Application)Marshal.GetActiveObject("Word.Application");
| 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:
| > | > | 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.
|
 
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.

app == m_WordApp can never be the same, both are different object
 
| 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.
 
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.Application");
m_WordApp.WindowActivate += new
Microsoft.Office.Interop.Word.ApplicationEvents4_WindowActivateEventHandler(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.Application");

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

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

}

}
}
 
| 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.
 
|
| || 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.
 
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.
 
| 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.
 
Thank you for th help.

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.
 

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

Back
Top