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

  • Home
  • SEARCH
  • 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 7503043
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T21:05:08+00:00 2026-05-29T21:05:08+00:00

I’m working with C# and .NET 4.0. My program consists of a front-end and

  • 0

I’m working with C# and .NET 4.0.

My program consists of a front-end and a back-end. The back-end will be running as a Windows service that starts automatically at system start-up. The front-end is a WPF application that the user will start when they’re ready to work with the application.

As part of its start-up process, the front-end needs to establish communications with the back-end. This is done using XMPP and works fine. The code doing the communications has a BeginStart method that returns a WaitHandle. Inside of a try-catch, I call BeginStart, get the WaitHandle, and call the Waithandle’s WaitOne( TimeSpan ) method. Note that the program is displaying a splash screen at this point. And these calls are being made on the front-end thread.

While this is working fine, I’ve found that clicking on other windows while the WaitOne call is running has no effect. Essentially, all Windows message traffic is halted waiting for this WaitHandle to return.

I believe I need to do the BeginStart and WaitOne call in another thread; I still need the front-end to wait for the WaitOne to return, as the program is supposed to die and no user interface is to display unless the WaitOne call returns successfully (no timeout). It just needs to let other programs process their Windows messages.

How do I make this work? There is no Application.DoEvents method in WPF.

Tony

Edit:

It looks like it may be helpful if I provide some more information.

My WPF application is the user interface for this product and only makes up half of the entire product. The other half is running as a Windows service on the same PC and we call it the back-end. What is going on in the back-end is unimportant, except that it is listening for connections to it using XMPP.

When the user interface (or front-end) starts up, one of the things it has to do is establish communications with the back-end using XMPP. This should only take a second or two maximum normally. However, there could be something wrong or the back-end could be down when the user starts the front-end. I do not want the front-end to wait indefinitely; it needs to time out after a reasonable amount of time and display an error message and die.

To establish the communication, there’s a module that runs as a separate thread that I start. I am calling its BeginStart method, which returns a WaitHandle. I then Wait on the WaitHandle, passing a TimeSpan set to my time out period. Here’s the snippet:

try {
    WaitHandle handle = BackgroundProcess.BeginStart();
    if ( !handle.WaitOne( Timeout ) ) {
        // Something went wrong; Notify user here
        return false;
    }

    // Make sure that the Background Process has started all of it's internal modules
    bool allAreRunning;
    Stopwatch timer = new Stopwatch();
    timer.Reset(); timer.Start();
    do {
        allAreRunning = true;
        foreach ( Module module in BackgroundProcess.Modules ) {
            if ( module.State != Module.States.Running ) {
                allAreRunning = false;
                break;
            }
        }

        if ( !allAreRunning ) {
            // I hate this loop, but I can't think of a better way to do this
            Thread.Sleep( 1000 );
        }

    } while ( !allAreRunning && timer.ElapsedMilliseconds <= Timeout.TotalMilliseconds );

    timer.Stop();

    if ( !allAreRunning ) {
        // Something went wrong; Notify user here
        return false;
    }

    // Other initialization done here

} catch ( Exception ex ) {
    // Something went wrong; Notify user here
    return false;
}

To test this, I have run the program in the debugger while the back-end was down. I can click on any open window and switch to it and interact with it fine. What I cannot do is click on the desktop. The WaitHandle.WaitOne() call returns very quickly, as that just returns once the background process has started running and does not wait for the connection to be established. It’s the do-while loop that’s running for most of the time out period.

I need to rewrite this so the user can switch not just to open programs but click on the desktop, too. I just don’t know how to make it work.

Tony

  • 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-29T21:05:09+00:00Added an answer on May 29, 2026 at 9:05 pm

    I’ve found a better way to code this start up logic. The code, however, relies on the architecture of our internal code.

    I was able to remove the do-while loop in the code snippet and replace it with better code. Now, the code loops over all of the Module objects and queries them for their Running state WaitHandle. These are added to a List collection. After the loop completes, I convert the List into an array by calling .ToArray() and then call the WaitHandle.WaitAll method.

    The code looks something like this now:

    try {
        WaitHandle handle = BackgroundProcess.BeginStart();
        if ( !handle.WaitOne( Timeout ) ) {
            . . .
           return false;
        }
    
        List<WaitHandle> waitHandles = new List<WaitHandle>();
        foreach ( Module module in BackgroundProcess.Transit.SubModules ) {
            WaitHandle waitHandle = module.GetWaitHandleForState( Module.States.Running );
            waitHandles.Add( waitHandle );
        }
    
        if ( !WaitHandle.WaitAll( waitHandles.ToArray(), Timeout ) ) {
            . . . 
            return false;
        }
    
        // If we get here, everything is communicating properly.
        . . .
    
    } catch ( Exception ex ) {
        . . .
        return false;
    }
    
    return true;
    

    Thanks

    Tony

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am currently running into a problem where an element is coming back from
I need a function that will clean a strings' special characters. I do NOT
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am doing a simple coin flipping experiment for class that involves flipping a

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.