简体   繁体   English

C#需要等待COM事件触发,然后才能继续执行而无法控制事件

[英]C# Need to wait for a COM event to fire before continuing execution without control over event

I've read a few other questions that seem similar but I'm still very confused, and none of the answers seem to be working for me so I decided to ask another question. 我看过其他一些看似相似的问题,但我仍然很困惑,所有答案似乎都不适合我,所以我决定再问一个问题。 Please bear with me, I'm not very well versed on threading and what not. 请多多包涵,我对线程不是很精通,反之亦然。

The application I am making is a 3rd party add-in for Revit Structure. 我正在制作的应用程序是Revit Structure的第三方插件。 The way they work is I hae a class library with a class that implements an interface, when that is called my application runs. 它们的工作方式是,我有一个带有实现接口的类的类库,这称为我的应用程序运行。

My tool is a Printing tool, it aims to automate printing to a PDF. 我的工具是“打印”工具,它旨在自动将打印到PDF。 I am trying to move my code over to using PdfCreator to print the PDFs (the way printing works in Revit is you set the printer and call the 'print' method in the API, so I can't do PDFs any way other than through a printer). 我正在尝试将代码移到使用PdfCreator来打印PDF(Revit中打印的工作方式是您设置打印机并在API中调用“ print”方法,因此我只能通过打印机)。

PDFCreator has a great COM interface that allows you to set its settings, as well as subscribing to an event which fires after every document is printed. PDFCreator具有出色的COM界面,可用于设置其设置以及订阅在打印每个文档后触发的事件。 I'm calling hte print method multiple times, so I want to wait until all are done, then do something in my code. 我多次调用hte打印方法,因此我想等到所有步骤完成后再在代码中做些事情。

My logic goes like this: 我的逻辑是这样的:

  1. User Selects things to print 用户选择要打印的东西
  2. Hits Print Button (on my WPF window) 点击“打印”按钮(在我的WPF窗口中)
  3. My Code then sets the PDF creator settings, and registers to listen to the event 然后,我的代码会设置PDF创建者设置,并注册以收听事件
  4. I call the Print() api method, which then takes some time to print each of the items 我调用Print()api方法,然后花一些时间来打印每个项目
  5. I then want to be alerted when everything is done printing, and do some 'post processing' on the documents. 然后,当所有内容都完成打印并在文档上进行一些“后处理”时,我想得到提醒。
  6. Post processing involves renaming/moving PDFs around among other things 后处理包括重命名/移动PDF

My problem is, that when I call the print method, my code doesn't wait for the PDFcreator events, it just continues through and exits, which means the post processing is never fired. 我的问题是,当我调用print方法时,我的代码不会等待PDFcreator事件,它只会继续执行并退出,这意味着永远不会触发后期处理。

What I need to do is somehow wait for the event, without blocking. 我需要做的是以某种方式等待事件,而不会阻塞。

After reading some other questions I've tried this: 阅读其他一些问题后,我尝试了以下方法:

  • AutoResetEvent, and use WaitOne until the event handler calls Set(). AutoResetEvent,并使用WaitOne,直到事件处理程序调用Set()。 This just got stuck on WaitOne and never continued. 这只是停留在WaitOne上,并且从未继续。
  • Application.DoEvents until a certain class variable has been set by the event handler.. Application.DoEvents,直到事件处理程序设置了某个类变量为止。
  • I tried creating a new thread to sign up to the events but that didnt seem to help 我尝试创建一个新线程来注册事件,但这似乎没有帮助

how do I make the event fire on a new thread and then alert the current thread to keep going? 如何使事件在新线程上触发,然后提醒当前线程继续进行? Or how can I make the code wait for the event to fire ? 或者如何使代码等待事件触发?

Let me know if you need any more info, happy to provide. 如果您需要更多信息,欢迎与我们联系。

Your problem is the following. 您的问题如下。 That other library needs a running message loop to be able to fire the event. 该其他库需要运行的消息循环才能触发事件。 Specifically when a certain message arrives from the message loop that message is dispatched into the library and causes it to fire the event. 具体来说,当某个特定消息从消息循环到达时,该消息将被分派到库中并导致其触发事件。

If you use some "wait" primitive in your calling code you block the message loop and thus the event is never fired. 如果在调用代码中使用某些“等待”原语,则会阻塞消息循环,因此永远不会触发该事件。 Your program should not block - it should continue running the message loop like every Windows GUI program does. 您的程序不应阻塞-它应该像每个Windows GUI程序一样继续运行消息循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM