Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7590027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T20:18:19+00:00 2026-05-30T20:18:19+00:00

I have made a small function which should print some InfoPath files. It is

  • 0

I have made a small function which should print some InfoPath files. It is necessary to stop at some points the thread shortly that everything works fine. If I remove the Thread.Sleep(); for example the function close the application to fast.
Now I would like to know, if there is a nicer way to solve that problem instead to stop the thread?

Here a piece of the function:

Application InfoPath = new Microsoft.Office.Interop.InfoPath.Application();
XDocument InfoPathXml = InfoPath.XDocuments.Open(XmlFile);
Thread.Sleep(Sleep);
InfoPathXml.PrintOut();
Thread.Sleep(Sleep);
InfoPath.Quit();
Thread.Sleep(Sleep);

The first sleep waits, until InfoPath has opened the document. The second one, waits for creating the print job. The last one, waits until the file is saved.

Edit
Here is some new code… But sadly it doesn’t work quite well… Only sometimes the event will be fired. Is there any other solution?

try
{
    Application InfoPath = new Microsoft.Office.Interop.InfoPath.Application();
    XDocument InfoPathXml = InfoPath.XDocuments.Open(XmlFile);
    InfoPathXml.OnContextChange += new _XDocumentEventSink2_OnContextChangeEventHandler(XmlFileLoaded);
    do
    {
        Thread.Sleep(0);
    } while (IsFileLocked(TempPath) == true);
    InfoPath.Quit();
}
catch (Exception exp)
{
    log.Write(exp.Message);
}

static private void XmlFileLoaded(DocEvent pEvent)
{
    pEvent.XDocument.PrintOut();
}

Edit2

Could anyone imagination why the event is not activated every time. If I start the program, the event is fired every 4th or 5th time.

There are two different cases which happen:

  1. The program opens the xml file with InfoPath, and nothing happens,
    it just waits. (Cause of the ” IsFileLocked” function.) The event
    is not triggered.
  2. The event is triggered after the first file is open. After that, the event occurs at every file, everything works fine.

Could it be a problem with cashing the files, or something like that?

Edit3

Well, with the following event handling, I could solve the problem:

_Application3 InfoPathApplication = null;
ApplicationEvents InfoPathApplicationEvents = null;

InfoPathApplication = (Microsoft.Office.Interop.InfoPath._Application3)InfoPath;
InfoPathApplicationEvents = (Microsoft.Office.Interop.InfoPath.ApplicationEvents)InfoPathApplication.Events;
InfoPathApplicationEvents.XDocumentOpen += new _ApplicationEvents_XDocumentOpenEventHandler(XmlFileLoaded);


static private void XmlFileLoaded(_XDocument pEvent)
{
    pEvent.PrintOut();
    //pEvent.XDocument.PrintOut();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T20:18:20+00:00Added an answer on May 30, 2026 at 8:18 pm
    1. Subscribe to event _ApplicationEvents_QuitEventHandler Quit
    2. Before exiting the app, wait for a semaphore.
    3. In the event handler (that is, when the InfoPath app exits), open up the semaphore.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello every one I have made a small function which takes a pointer to
I have made some code which exports some details of a journal article to
I have made a program which is a small library operated via software. When
I have some larger files which I need to share with people and to
I have made a small log service that i want to publish to a
I have made a small application in Java and I would like to make
Post-release, I have made one small change to one form in our development site
I have just started programming and have made a few small applications in C
I have made an update on Google App Engine with a small fix and
I have made an app that starts a service, which starts a timer, which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.