none
Cannot create more than one window with async method. (Threading problem)

    Question

  • I've created an async method like this:

    public async static void Start()
            {
                LoginInterface LoginInterface = new LoginInterface();
                LoginInterface.Show();
    
                while (true)
                {
                    await LoginInterface.CredentialsInput.Task;
                    if (LoginInterface.ProfilePassword != StringHash.SHA256ComputeHash("69855"))
                    {
                        LoginInterface.Dispatcher.Invoke(() =>
                        {
                            LoginInterface.Reset();
                            LoginInterface.IsInvalidLoginVisible = true;
                        });
                    }
                    else
                    {
                        LoginInterface.Dispatcher.Invoke(() =>
                        {
                            LoginInterface.Close();
                        });
                        LoginInterface = null;
    
                        break;
                    }
                }
            }

    This method is supposed to start a LoginInterface window and keep trying (or looping) until the users credentials are correct. When the user clicks the login button on the window, it calls an event and using a TaskCompletionSource (CredentialsInput), tells the system to continue and check the password. Once it is correct, it breaks and continues. Everything works great, but once I try the next part which is to open the loading screen:
    LoadingInterface LoadingInterface = new LoadingInterface();
                LoadingInterface.Show();
    It tells be something about the caller must be an STA thread. I've tried setting the [STAThread] attribute, using Dispatcher.Invoke, Dispatcher.BeginInvoke, and nothing seems to be working. I'm not very familiar with threading so please forgive me if I'm way off or not correct about a couple of things. This is really beginning to frustrate me. Thanks for any help!
    Wednesday, January 03, 2018 8:33 PM

Answers

All replies

  • urthread.SetApartmentState(ApartmentState.STA);
    • Marked as answer by Varscott11 Thursday, January 04, 2018 1:50 AM
    Wednesday, January 03, 2018 9:15 PM
  • Does this imply that I should run the above code in a new thread?
    Wednesday, January 03, 2018 9:22 PM
  • Just out of curiosity, does the method need to be static and wonder if making it non static would cause the same issue.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Thursday, January 04, 2018 12:22 AM
    Moderator
  • This method in particular does need to be static. I've actually tried putting everything in a new thread and putting the LoginInterface LoginInterface = new LoginInterface(); outside of the thread. Now it seems that I can just use the dispatcher to perform calls. This now seems to let me create the other loadinginterface as well so I think with blake's advice of using a STA thread, I've solved the issue. I appreciate your guys help!
    Thursday, January 04, 2018 1:49 AM