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. 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. 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 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. I'm calling hte print method multiple times, so I want to wait until all are done, then do something in my code.
My logic goes like this:
- User Selects things to print
- Hits Print Button (on my WPF window)
- My Code then sets the PDF creator settings, and registers to listen to the event
- I call the Print() api method, which then takes some time to print each of the items
- I then want to be alerted when everything is done printing, and do some 'post processing' on the documents.
- Post processing involves renaming/moving PDFs around among other things
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.
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(). This just got stuck on WaitOne and never continued.
- Application.DoEvents until a certain class variable has been set by the event handler..
- 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.