[英]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: 我的逻辑是这样的:
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: 阅读其他一些问题后,我尝试了以下方法:
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.