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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T21:35:41+00:00 2026-05-14T21:35:41+00:00

Maybe I’m mis-remembering how Winforms works or I’m overcomplicating the hell out of this,

  • 0

Maybe I’m mis-remembering how Winforms works or I’m overcomplicating the hell out of this, but here’s my problem.

I have a WPF client app application that talks to a server over WCF. The current user may “log out” of the WPF client, which closes all open screens, leaves only the navigation pane, and minimizes the program window. When the user re-maximizes the program window, they are prompted to log in. Simple.

But sometimes things happen on background threads – like every 5 minutes the client tries to make a WCF calls that refreshes some cached data. And what if the user is logged out when this 5 minute timer triggers? Well, then the user should be prompted to log back in…and this must of course happen on the UI thread.

    private static ISecurityContext securityContext;
    public static ISecurityContext SecurityContext
    {
        get
        {
            if (securityContext == null)
            {
                // Login method shows a window and prompts the user to log in
                Application.Current.Dispatcher.Invoke((Action)Login); 
            }
            return securityContext;
        }
    }

    private static void Login()
    {
       if (securityContext == null) { \
         /* show login window and set securityContext */ 
         var w = new LoginWindow();
         w.ShowDialog();
         securityContext = w.GetSecurityContext();
       }
    }

So far so good, right? But what happens when multiple threads hit this spot of code?

Well, my first intuition was that since I’m syncrhonizing across the Application.Current.Dispatcher, I should be fine, and whichever thread hit first would be responsible for showing the login form and getting the user logged in…

Not the case…

  1. Thread 1 will hit the code and call ShowDialog on the login form

  2. Thread 2 will also hit the code and will call Login as soon as Thread 1 has called ShowDialog, since calling ShowDialog unblocked Thread 1 (I believe because of the way the WPF message pump works)

…the end effect being that I have multiple login forms popped up to the user at once.

All I want is a synchronized way of getting the user logged back into the application…what am I missing here?

Thanks in advance.

  • 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-14T21:35:42+00:00Added an answer on May 14, 2026 at 9:35 pm

    Sorry for the delayed follow-up.

    I fixed the blocking problem a few days ago on the UI thread by basically implementing DoEvents for WPF:
    http://khason.net/blog/how-to-doevents-in-wpf/

    So now, many threads, both background and UI can invoke onto the UI thread, and if the window is already shown, will “emulate” the behavior of ShowDialog, but not block and not show a second Login window… Hope that makes sense to anyone reading.

    void ShowLoginWindow(Window window) 
              {
                    if (window != null )
                    {
                        if (window.Visibility != Visibility.Visible)
                        {
                            try
                            {
                                result = window.ShowDialog();
                            }
                            catch (Exception ex)
                            {
                            }
                        }
                        else
                        {
                            // don't block the UI thread, but wait till the dialog window returns 
                            while(window.Visibilit y== Visibility.Visible)
                            {
                                DoEvents();
                            }
                            return window.DialogResult;
                        }
                    }
                    return result;
            }
    
            void DoEvents()
            {
                DispatcherFrame f = new DispatcherFrame();
                Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background,
                (SendOrPostCallback)delegate(object arg)
                {
                    DispatcherFrame fr = arg as DispatcherFrame;
                    fr.Continue = false;
                }, f);
                Dispatcher.PushFrame(f);
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Maybe I'm misunderstanding how inheritance works here, but here's my problem: I have a
Maybe title is confusing you, but the problem is concretely here : I have
Maybe I am out of my mind right now, but I have a question
Maybe this is very basic, but I am all confused. I have a simple
Maybe I am being stupid, but this isn't making sense to me... I have
Maybe a silly question but here goes anyway. Example: Let's say I have one
Maybe I misunderstood this but from what I read I may have several problems
Maybe this is a very simple question, but I just can not figure out.
Maybe I'm just thinking about this too hard, but I'm having a problem figuring
Maybe world peace would be easier, but I have a problem with the widths

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.