开发者

Updating ShowDialog() Window

开发者 https://www.devze.com 2023-02-19 19:40 出处:网络
I have an application which posts progress/status messages as it runs (it\'s a database schema updater). Because it can run with or without a UI, those messages have to go to either the Console or a s

I have an application which posts progress/status messages as it runs (it's a database schema updater). Because it can run with or without a UI, those messages have to go to either the Console or a simple WPF window. The WPF window is opened modally. All it contains is a listbox to which the messages get posted.

In order to deal with threading issues (the GUI has to be on an STA thread, while the Console isn't) I use the following logic:

if( args.NoUI )
{
    // we're just doing plain old console-based logging
    frame = new nHydrateInstallationFrame(installer) { ReportingThreshold = threshold };

    frame.Execute();
}
else
{
    // GUI has to be on an STA thread, so wrap ourselves and go!
    frame = new nHydrateUIInstallationFrame(installer) { ReportingThreshold = threshold };
    // the Execute method on an nHydrateUIInstallationFrame object calls ShowDialog().
    // when the resulting window opens, the actual installation process begins to execute.
    // this allows UI-based logging, and, more importantly, keeps the logging window open
    // until the user explicitly closes it after the installation process completes. If 
    // we don't do this kind of indirect execution, the logging window will immediately close
    // after the installation method ends, which will keep the user from reviewing the
    // logging output
    Thread staThread = new Thread(frame.Execute);
    staThread.SetApartmentState(ApartmentState.STA);
    staThread.Start();
    staThread.Join();
}

This is just an extract, but it shows the approach, I think (i.e., creating a separate thread for the UI, setting its apartmentstate, starting it and joining it). This causes the Execute method on the frame (the object containing the output "sink") to run. The Execute method calls the actual database schema modification routine, passing itself as a parameter. The schema modification routine writes messages to the frame, which then outputs them to either the console or the UI.

This all works fine...except that the messages posted to the UI window don't show up until after the process being run by the UI completes. Except, I sh开发者_StackOverflowould add, if you expand the UI message window using the mouse. In that case, the messages show up as they are posted.

This makes me think I've got some kind of focus problem, with the UI window not having focus unless I play around with it. But I'm not sure. I did try setting focus to the UI window as soon as it was created, but that didn't do anything.

I'd appreciate thoughts on how I can get the messages posted to the UI window to show in real time.


My suspicion is that you run the database upgrader on the UI thread. In that case run the upgrade routine on a background thread and post yor messages to the window from there. You might want to have a look at the BackgroundWorker class for that purpose.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号