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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:02:15+00:00 2026-06-18T08:02:15+00:00

Im working on my thesis about an C# WPF program, but i ran into

  • 0

Im working on my thesis about an C# WPF program, but i ran into an error i dont understand.

Somewhere in my MainWindow Code im starting a new Thread like this:

Thread searchServer = new Thread(new ThreadStart(doSearchServer));
            searchServer.SetApartmentState(ApartmentState.STA);
            searchServer.Start();

The doSearchServer methods does the following:

    private void doSearchServer()
    {
        bool connected = ServerConnection.authentication();
        ServerConnection.getDeviceList();
        gotDataFromServer = connected;

        if (connected)
          ..
          ..
    }

The ServerConnection class is static because i also need that class in some other Windows.

At ServerConnection.authentication() the client (my Program) tries to authenticate on my server. If a password is required, i wanted to open a new PasswordWindow as you can see here:

public static bool authentication()
    {
        UdpClient client = new UdpClient();
        IPEndPoint ip = new IPEndPoint(IPAddress.Broadcast, 55042);
        IPEndPoint ipRec = new IPEndPoint(IPAddress.Any, 0);

        byte[] bytes = Encoding.UTF8.GetBytes("authent|Username|Windows");
        client.Send(bytes, bytes.Length, ip);

        //Receive Answer
        byte[] recBuffer = client.Receive(ref ipRec);
        string recString = Encoding.UTF8.GetString(recBuffer);

        if (recString.Equals("authent|password"))
        {
            //Send Passwort
            Console.WriteLine("Password Required");

            Dispatcher.CurrentDispatcher.Invoke(new Action(() =>
            {
            PasswordWindow pw = new PasswordWindow();
            pw.ShowDialog();
            if (pw.ShowDialog() == true)
            {
                //send PW
            }
            else
            {
                //Dont send PW
            }
            }));

            client.Send(bytes, bytes.Length, ip);
            .
            .
            .
        }

At the PasswordWindow Contructor it crashes. I tried STA + Dispatcher, MTA + Dispatcher, STA only.. anything i tried didnt work… i really dont get it.

Can someone please explain me why it still says that the Thread needs to be an STA Thread?

Thanks for any kind of help!!

  • 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-06-18T08:02:16+00:00Added an answer on June 18, 2026 at 8:02 am

    change Dispatcher.CurrentDispatcher

    to

    System.Windows.Application.Current.Dispatcher

    because when accessing the Dispatcher.CurrentDispatcher property from a thread that is not the “UI Thread” (the one associated with the Dispatcher that started the application in the first place), a new Dispatcher is created, which is not what you need here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Longtime reader of stackoverflow but first question. I'm working with Wordpress (specifically thesis theme)
I'm working on some custom C++ static code analysis for my PHD thesis. As
Working on some code and I'm given the error when running it from the
I am working on a thesis about measuring quality of a product. The product
I'm working on my thesis and i'm trying to make a standart forum, but
I am trying to create a backup. I dont understand what the error I'm
I am working on some application for my masters thesis and in the process
Working with H2 I get this error when I try to write a row
Greeting, This month I will start working on my master thesis. My thesis's subject
I'm working on my thesis project on financial mathematics. One problem I'm having is

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.