简体   繁体   中英

Create wait cursor when launching external app

This is a neubi question... I've searched the internet, but evidently I'm not forming the question well.

My winform program launches an external application, let's call it "watchit.exe". It takes watchit 5-15 seconds from the time it's process starts until it is visible for the user (finally appearing as a task in the task manager). I don't want the user to get frustrated during this time and keep clicking the button that launches watchit, so I need a wait cursor, or some other indication.

  1. There are several custom controls on this form, is there a way to override their individual cursors with a wait cursor? Maybe putting text in the form status strip would be better than a wait cursor?

  2. How do I know in my program when Watchit is up and running so I can set the cursor back to default, or change the text in the form status strip? I don't want to use a timer to keep checking if Watchit is up and running yet... Not even sure what to check !

I've attempted to use WMI, but it seems to look at processes (or maybe I'm not fully grasping WMI ).

Here is how the process is launched.

Process watchIt = new Process();

string path = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
path = Path.Combine(path, "Watchit.exe");
watchIt.StartInfo = new ProcessStartInfo(path, e.ID.ToString());

if (File.Exists(path))
{
    watchIt.Start();  //Starts Watchit, but how do you know when it's 'up' ?
    Cursor.Current = Cursors.WaitCursor;
}

TIA

You can use the Process.WaitForInputIdle() method . Quoted straight from the MSDN documentation:

"This state is useful, for example, when your application needs to wait 
for a starting process to finish creating its main window before the 
application communicates with that window."

I usually put up a modal dialog with its cursor set to a wait cursor. You can make it borderless like a splash screen. Start with it always on top so that it won't appear behind anything, but once it's finished loading set it to normal so the user is not inconvenienced trying to switch apps while waiting.

icemanind provides an excellent solution here if you want to know when to end the wait cursor for a windowed application (Process.WaitForInputIdle). If it's a console app, or if it has significant setup to do after the main window appears, you will need to use a shared wait handle such as a semaphore or mutex to know when to drop the wait cursor (or use named pipes, but that's more overhead). I typically name a mutxe with a GUID so that it's unique.

Creation and waiting in your main app:

Mutex unlockMutex = new Mutex(false, "MyUniqueName");

try
{
    unlockMutex.WaitOne();
}
catch (AbandonedMutexException ex)
{
    Exception ex2 = ex.InnerException;
}

Release in your child app:

Mutex unlockMutex = new Mutex(false, "MyUniqueName");

// Release access to unlock mutex
try
{
    unlockMutex.ReleaseMutex();
}
catch (ApplicationException ex)
{
    Exception ex2 = ex.InnerException;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM