担心的问题 [英] Wried problem

查看:80
本文介绍了担心的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在使用COM来自动化Word。在我的程序的主线程中,我

存储来自Application.Documents的所有Document对象。在一个事件中

处理程序(由Word生成的事件),如果我使用Application.Documents获取所有当前的

文档,我得到的对象不一样
以前存储的
。一些线程调用事件处理程序

由C#生成。但是,如果我使用我自己的

线程获得所有当前文档,返回的文件与先前存储的文件相同。


我做了同步和所有线程是MTA。


有谁能告诉我这是为什么?


非常感谢

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

推荐答案



" leiz" < le ******** @ gmail.comwrote in message

news:11 ********************* @ i42g2000cwa.googlegro ups.com ...

|

|

|我正在使用COM来自动化Word。在我的程序的主线程中,我

|存储Application.Documents中的所有Document对象。在一个事件中

| handler(由Word生成的事件),如果我得到所有当前的

|使用Application.Documents的文档,我得到的对象不一样

|以前存储的。一些线程调用事件处理程序

|由C#生成。但是,如果我使用我自己的所有当前文件

|线程,返回的是先前存储的线程。

|

|我做了同步,所有线程都是MTA。

|

|谁能告诉我为什么会这样?

|

|非常感谢

|


这不是你第一次问这个,请发一个完整的样本,

说明了问题,在没有看到任何代码的情况下帮助你真的很难。


请注意,如果你发布到

微软,你可能会得到更好的答案。 public.dotnet.framework.interop NG。


Willy。


"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.


这里是简化代码:


使用System;

使用System.Collections.Generic;

使用System.ComponentModel;

使用System.Data;

使用System.Drawing;

使用System.Text;

使用System.Windows.Forms;

使用System.Runtime.InteropServices;

使用System.Threading;

使用Word = Microsoft.Office.Interop.Word;


命名空间btest

{

公共部分类Form1:表格

{

私有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);


线程t =新线程(新的ThreadStart(线程));

t.SetApartmentState(ApartmentState.MTA);

t。开始();

}


private void Word_WindowActivate(Word.Document doc,Word.Window

win)

{

Word.Application app =

(Word.Application)Marshal.GetActiveObject(" Word.Ap plication");

MessageBox.Show(" In in handler handler" +(app == m_WordApp));

}


private void thread()

{

Word.Application app =

(Word.Application)Marshal.GetActiveObject(" Word.Ap plication");

MessageBox.Show(" in in our thread " +(app == m_WordApp));

}

}

}


=============================================

问题是 - 在我们的线程中结果是真的,在事件中

处理程序结果为false。


主线程是MTA还有。


Willy Denoyette [MVP]写道:
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 ...

|

|

|我正在使用COM来自动化Word。在我的程序的主线程中,我

|存储Application.Documents中的所有Document对象。在一个事件中

| handler(由Word生成的事件),如果我得到所有当前的

|使用Application.Documents的文档,我得到的对象不一样

|以前存储的。一些线程调用事件处理程序

|由C#生成。但是,如果我使用我自己的所有当前文件

|线程,返回的是先前存储的线程。

|

|我做了同步,所有线程都是MTA。

|

|谁能告诉我为什么会这样?

|

|非常感谢

|


这不是你第一次问这个,请发一个完整的样本,

说明了问题,在没有看到任何代码的情况下帮助你真的很难。


请注意,如果你发布到

微软,你可能会得到更好的答案。 public.dotnet.framework.interop NG。


威利。
"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.


app == m_WordApp永远不会相同,两者都是不同的对象

引用,更多指向不同的RCW'实例,所以即使使用

Object.ReferenceEquals也没有帮助。

猜猜你想确保两者都引用相同的单词实例,好吧,

你唯一的选择是检查是否只有一个Word

实例使用Diagnostics.Process类运行。如果有一个实例超过

,调用GetActiveObject将连接到ROT中的第一个实例,但这不是一个很难的保证。


威利。


" leiz" < le ******** @ gmail.comwrote in message

news:11 ********************* @ e3g2000cwe.googlegrou ps.com ...

|这里是简化的代码:

|

|使用系统;

|使用System.Collections.Generic;

|使用System.ComponentModel;

|使用System.Data;

|使用System.Drawing;

|使用System.Text;

|使用System.Windows.Forms;

|使用System.Runtime.InteropServices;

|使用System.Threading;

|使用Word = Microsoft.Office.Interop.Word;

|

|命名空间btest

| {

|公共部分类Form1:表格

| {

|私有Word.Application m_WordApp;

|

|公共Form1()

| {

| InitializeComponent();

|

|

| m_WordApp =

| (Word.Application)Marshal.GetActiveObject(" Word.Ap plication");

| m_WordApp.WindowActivate + = new

|

Microsoft.Office.Interop.Word.ApplicationEvents4_W indowActivateEventHandler(Word_WindowActivate);

|

|线程t =新线程(新的ThreadStart(线程));

| 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 in handler handler" +(app == m_WordApp));

| }

|

| private void thread()

| {

| Word.Application app =

| (Word.Application)Marshal.GetActiveObject(" Word.Ap plication");

| MessageBox.Show(" In in thread" +(app == m_WordApp));

| }

| }

| }

|

| =============================================

|问题是 - 在我们的线程中结果是真的,如果事件

|处理程序结果为false。

|

|主线也是MTA。

|

| Willy Denoyette [MVP]写道:

| " LEIZ" < le ******** @ gmail.comwrote in message

|新闻:11 ********************* @ i42g2000cwa.googlegro ups.com ...

| |

| |

| |我正在使用COM来自动化Word。在我的程序的主线程中,我

| |存储Application.Documents中的所有Document对象。在一个

活动中

| | handler(由Word生成的事件),如果我得到所有当前的

| |使用Application.Documents的文档,我得到的对象不一样

| |以前存储的。一些线程调用事件处理程序

| |由C#生成。但是,如果我使用我自己的所有当前文件

| |线程,返回的线程与先前存储的线程相同。

| |

| |我做了同步,所有线程都是MTA。

| |

| |谁能告诉我为什么会这样?

| |

| |非常感谢

| |

| >

|这不是你第一次问这个,请发一个完整的样品



|说明了这个问题,如果没有看到任何

代码,真的很难帮助你。

| >

|请注意,如果您发布到

|,您可能会得到更好的答案microsoft.public.dotnet.framework.interop NG。

| >

|威利。

|
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.
|


这篇关于担心的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆