Note that there are some explanatory texts on larger screens.

plurals
  1. PONeed a way to have front end wait for a lengthy process to finish without stopping system message flow
    primarykey
    data
    text
    <p>I'm working with C# and .NET 4.0.</p> <p>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.</p> <p>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.</p> <p>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.</p> <p>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.</p> <p>How do I make this work? There is no Application.DoEvents method in WPF.</p> <p>Tony</p> <p>Edit:</p> <p>It looks like it may be helpful if I provide some more information.</p> <p>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.</p> <p>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.</p> <p>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:</p> <pre><code>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 &amp;&amp; timer.ElapsedMilliseconds &lt;= 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; } </code></pre> <p>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.</p> <p>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.</p> <p>Tony</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload