7

I have a WPF application, which needs to log out user after 5 min of inactivity.

But if user open a print dialog of any page, and do not touch screen for 5 minutes, even if I log out user and clear all child elements, print dialog still stays on top of WPF form and somebody can come and continue to print what ever page user stayed.

I tried to use;

Window window = Application.Current.MainWindow;

or

FocusManager.GetFocusedElement();

but could not achieve to access to PrintDialog and close it.

Is there any way to access it and close if user did not respond to print dialog?

Joe Korolewicz
  • 474
  • 4
  • 21
Teoman shipahi
  • 47,454
  • 15
  • 134
  • 158
  • Do you have programmatic access to the `PrintDialog` object when you sign the user out? If so, you could try calling the `Dispose` method or `Reset` method. Alternatively, although not very nice, would it be possible to simply restart the program after the user is signed out? – keyboardP Jun 24 '13 at 18:22
  • WPF control has WebBrowser control embedded in some parts, and when user click print inside WebBrowser control then JavaScript fires PrintDialog. Then I have no reference of PrintDialog of that moment. – Teoman shipahi Jun 24 '13 at 18:32
  • Why is your original answer not good any more? Also have you seen my answer to this question: http://stackoverflow.com/questions/7926107/force-close-of-messagebox (btw: White uses UI automation). – Simon Mourier May 29 '15 at 05:38
  • It's unclear what else is supposed to happen after 5 minutes. Does the program continue to run with no user logged in, or is the program supposed to quit? – Mike Fulton May 31 '15 at 18:24
  • Since the Print dialog belongs to JS and not C#, I guess you may not be able to easily access and close it. But you can inject JavaScript code into the WebBrowser control... Maybe you should ask how to close the dialog from JavaScript, and then try to inject that code into the WebBrowser. – almulo Jun 02 '15 at 13:11

3 Answers3

4

I fixed this weird problem by using

white project. http://white.codeplex.com/wikipage?title=Working%20with%20window&referringTitle=Programming%20using%20white

By using application class, I am able to access all ModalDialogs in WPF project, and close them.

  Application application = White.Core.Application.Attach(Process.GetCurrentProcess().Id);

private void dispatcherTimer_Tick(object sender, EventArgs e)
    {
        White.Core.UIItems.WindowItems.Window window = application.GetWindow("MainWindow");
        List<White.Core.UIItems.WindowItems.Window> modalWindows = window.ModalWindows();
        foreach (White.Core.UIItems.WindowItems.Window modalWindow in modalWindows)
        {
            modalWindow.Close();
        }
    }
Teoman shipahi
  • 47,454
  • 15
  • 134
  • 158
1

You can use p/invoke for this. Use findwindow to find any window and destroywindow to close it.

https://msdn.microsoft.com/en-us/library/windows/desktop/ms633499(v=vs.85).aspx

user582734
  • 45
  • 6
0

If I understand correctly, you're able to do the user logout at the 5 minute mark (I presume via the timer event handler), but if the print dialog was open, then the printout can still end up happening.

The question is, what's really the important thing here, closing the dialog, or preventing the subsequent printout? Also, are you logging out the user but keeping the program going, or is the program quitting?

If the app is supposed to shut down, then your code can call the Application object's Shutdown() method, which will close any modal dialogs as part of the shutdown process.

If the app is supposed to keep running without a current user, then I would think the White library is your best bet. It's unclear why you've moved away from this.

With or without closing the print dialog, however, the printing code should check to see if the user is still logged in before it actually prints something. If the login expired while the print dialog was on screen, the printing code should simply exit without printing anything.

Mike Fulton
  • 920
  • 11
  • 22