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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T22:27:37+00:00 2026-06-03T22:27:37+00:00

I want to make server application. In the beginning it should make thread for

  • 0

I want to make server application. In the beginning it should make thread for organizing every connection and write logs in Listbox. I have problem because i don’t know where can i make new thread which would have access to Form1.Listbox1. This is what i tried:

public class ServerLoop
{
    Form1 form1;
    public ServerLoop(Form1 f)
    {
        form1 = f;
    }
    public void loop()
    {
        form1.addConsoleMessage("test");
    }
}

And Form1 class:

public partial class Form1 : Form
{
    public Thread tServerLoop;
    public ServerLoop serverLoop;

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        console.Items.Clear();
        players.Items.Clear();
        players.Items.Add("Witaj w serwerze");
        addConsoleMessage("test");
        serverLoop = new ServerLoop(this);
        tServerLoop = new Thread(serverLoop.loop);
        tServerLoop.Start();
    }

    private void connectButton_Click(object sender, EventArgs e)
    {

    }

    public void addConsoleMessage(String msg)
    {
        console.Items.Add(msg);
    }
}

Anyone knows what can i do to acheive this?

  • 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-03T22:27:39+00:00Added an answer on June 3, 2026 at 10:27 pm

    Well, you could use Invoke to marshal a delegate back onto the UI thread where that ListBox can be safely accessed.

    public void loop() 
    {
      form1.Invoke(new Action(
        () =>
        {
          form1.addConsoleMessage("test");
        }));
    } 
    

    But alas, this option is inferior. Actually, these marshaling techniques are generally terrible. Do not get me wrong. There is a time and place for Invoke (and the like), but this, like many situations, is not one of them.

    • The code is ugly because you have to sprinkle Invoke calls all over the place.
    • It forces you into a design where the UI thread and worker thread are tightly coupled.
    • The worker thread is dictating the update frequency of the UI.
    • It is inefficient.
    • It can flood the UI message queue (at least it could with BeginInvoke).
    • The worker thread has to wait for a response from the UI thread before it can proceed (it will with Invoke anyway).

    So how would I solve this problem? Well, with the boring old System.Windows.Forms.Timer and the fancy new ConcurrentQueue<T> of course.

    public partial class Form1 : Form                    
    {
      private ConcurrentQueue<string> queue = new ConcurrentQueue<string>();
    
      public Form1()                    
      {                    
        InitializeComponent();                    
      }                    
    
      private void Form1_Load(object sender, EventArgs e)                    
      {                    
        console.Items.Clear();                    
        console.Items.Add("test");
        players.Items.Clear();                    
        players.Items.Add("Witaj w serwerze");                    
        Task.Factory.StartNew(
          () =>
          {
            while (GetSomeCondition())
            {
              string value = GetSomeValue();
              queue.Enqueue(value);
            }
          });
      }                    
    
      private void YourTimer_Tick(object sender, EventArgs e)                    
      {                    
        string value;
        while (queue.TryDequeue(out value)
        {
          console.Items.Add(value);
        }
      }                                        
    }                    
    

    So what do we have now.

    • It looks elegant.
    • Our background task knows only about a queue. The tight coupling has been broken.
    • The UI thread is now dictating the update frequency…the way it should be.
    • It is a lot more efficient.
    • There is no chance that the UI message queue will get flooded.
    • And finally, the worker can speed along merrily completely unware of what the UI thread is doing.

    This solution is not completely devoid of disadvantages though. Now that we have our worker thread speeding along it is possible that it produces more items for the queue then what the UI thread can consume. It would not typically be a problem, but there are techniques for dealing with that.

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

Sidebar

Related Questions

I am going to make a client-server application using Node.js, and I want to
I want to make a business logic of server side Java application as a
I am new in Java development. I Want to make chat server Application in
I want to make a remoting application. On my server application, I added System.Runtime.Remoting
I basically want to make this in my application, so our technicians have an
I want to make (for fun, challenge) a videoconference application, I have some ideas
I want to make an iPhone app and an application (server) which runs on
I have a game server which is made in Java. I want to make
In a socket-based application (client/server), I want to make the server perform as a
I have a client/server application written in C#/.NET 3.5 that I want to do

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.