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 7531611
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:13:08+00:00 2026-05-30T05:13:08+00:00

I the following code that creates windows in an mdi form. The idea is

  • 0

I the following code that creates windows in an mdi form. The idea is to create a window of a certain type if it dosent exist, or bring it to front if there is already an instance.

  public static object CreateWindow(Type windowType, params object[] args)
    {
        try
        {
            lock (_definitionToWindow)
            {
                var def = new WindowDefinition {ControlType = windowType, Args = args};

                System.Windows.Forms.Form win = null;
                if (_definitionToWindow.TryGetValue(def, out win))
                {                      
                    win.Activate();
                    return win;
                }      

                System.Windows.Controls.Control uiElement =
                    (System.Windows.Controls.Control) Activator.CreateInstance(windowType, args);


                object result = null;
                if (uiElement is Window)
                    result = WpfMdiHelper.ShowWpfWindowInMdi((Window) uiElement);
                else
                    result = WpfMdiHelper.ShowWpfControlInMdi((System.Windows.Controls.Control) uiElement);

                if (result is System.Windows.Forms.Form)
                {                       
                    _definitionToWindow.Add(def, result as System.Windows.Forms.Form);
                    lock (_windowslock)
                    {
                        _windows.Add((System.Windows.Forms.Form) result, uiElement as IHasViewModel);                           
                    }
                    ((System.Windows.Forms.Form) result).Disposed += new EventHandler(WindowsFactory_Disposed);
                }                
                return result;
            }
        }
        catch (Exception ex)
        {
            Logger.WriteError("Window creation exception", ex.ToString(), LogEntryCodes.UIException);
        }
        return null;
    }

The code more or less works, but when you click a button that opens a window several types in quick succession it opens up several windows.

After running debug traces I found that lock (_definitionToWindow) is being bypassed by all the clicks (it looks like all calls are being made on the same thread) and the method blocks on Activator.CreateInstance. So when the 2nd call arrives to the dictionary check it doesn’t find any previous instances and proceeds to recreate the window.

Anyone knows why this happens? and the proper way to handle this situation?

  • 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-30T05:13:09+00:00Added an answer on May 30, 2026 at 5:13 am

    This should give you a thread safe lock that only allows one caller into CreateWindowImpl even if they’re on the same thread. It doesn’t block any threads though unlike lock().

    static long Locked = 0;
    
    static void CreateWindow(...)
    {
       if(0 == Interlocked.Exchange(ref Locked, 1))
       {
          try
          {
             CreateWindowImpl(...);
          }
          finally 
          {
             Interlocked.Exchange(ref Locked, 0);
          }
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code that creates two objects (ProfileManager and EmployerManager) where the
I have the following code that creates a serverside object of the xmlhttp class.
I have a method that contains the following (Java) code: doSomeThings(); doSomeOtherThings(); doSomeThings() creates
Lets say that I'm trying to create a new process with the following code:
I have the following code, that in my head should create a new row
I have the following Windows code that spawns two threads then waits until they
I have the following code which creates a dynamic object that is assigned to
The following code in a WPF app creates a hyperlink that looks and acts
I've got the following code that creates a PopupWindow containing an EditText: lbs.setOnTouchListener(new OnTouchListener(){
Given the following code (that doesn't work): while True: # Snip: print out current

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.