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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:42:30+00:00 2026-05-22T12:42:30+00:00

I have the following classes: using System; using System.Windows.Forms; namespace FastEyeControl { public partial

  • 0

I have the following classes:

        using System;
        using System.Windows.Forms;

        namespace FastEyeControl
        {
            public partial class ConnectView : Form, IConnectView
            {
                private IConnectPresenter m_Presenter;

                public ConnectView()
                {
                    InitializeComponent();

                    m_Presenter = new ConnectPresenter(this);
                }

                public string Hostname
                {
                    get
                    {
                        return m_Hostname.Text;
                    }
                }

                public int Port
                {
                    get
                    {
                        return Convert.ToInt32(m_Port.Text);
                    }
                }

                public void ShowMessage(string message)
                {
                    MessageBox.Show(message,
                                    "Success",
                                    MessageBoxButtons.OK,
                                    MessageBoxIcon.Information);
                }

                public void ShowError(string message)
                {
                    MessageBox.Show(message,
                                    "ERROR!",
                                    MessageBoxButtons.OK,
                                    MessageBoxIcon.Error);
                }

                private void m_ConnectButton_Click(object sender, EventArgs e)
                {
                    m_Presenter.ConnectButtonPressed();
                }
            }
        }

The presenter class:

        using System;
        using System.Collections.Generic;

        namespace FastEyeControl
        {
            public class ConnectPresenter : IConnectPresenter
            {
                private IConnectView m_View;
                private IConnectModel m_Model;

                public ConnectPresenter(IConnectView view)
                {
                    m_View = view;
                    m_Model = FastEyeClient.Instance;
                }

                public void ConnectButtonPressed()
                {
                    m_Model.Connect(m_View.Hostname, m_View.Port);
                }

                private void ConnectionComplete(object sender, ConnectionEventArgs e)
                {
                    // Code here indicating whether connection was successful and informing the view.
                    // i.e...

                    if (e.IsConnected)
                    {
                        m_View.ShowMessage("Successfully connected.");
                    }
                    else
                    {
                        m_View.ShowError("Unable to connect.");
                    }
                }
            }
        }

The model code runs in another thread. The problem is that when I call m_Model.Connect(), I’m calling code that’s usually running in another thread within the main thread still (the UI thread). This is not a database connection. This is a TCP/IP connection to a server. If I set a variable within the model, then I am doing this from the UI thread which is not thread safe.

I know that with user controls, they have InvokeRequired and Invoke/BeginInvoke operations that will handle this situation. But that is for user controls only. I know you can’t just interrupt another thread in the middle of its execution and tell it to call another method instead. I basically want the non-UI thread to call the Connect code somehow.

Just as a test, I tried using a delegate (fire off an event whenever I want to connect) and when I look in the debugger, the Connect code is still running in the UI thread.

I need a multi-threaded event queue essentially. What’s the best way to achieve what I want to do here? Thanks!

  • 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-22T12:42:31+00:00Added an answer on May 22, 2026 at 12:42 pm
     public void ConnectButtonPressed()
                {
                    var threadedTask = () => m_Model.Connect(m_View.Hostname, m_View.Port);
                    threadedTask.BeginInvoke(null,null);
                }
    

    This will, no question, use a background thread from the ThreadPool to do the work. Maybe you had tried to call the delegate directly, or called Invoke() on it; that will execute the delegate synchronously.

    Now, BeginInvoke is simple to set up, but it has its limitations; you cannot cancel execution of the background thread, and if it throws an exception you cannot catch it in the invoking thread.

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

Sidebar

Related Questions

I have defined the following classes and methods: using System; using System.Linq.Expressions; using System.Windows.Forms;
Using: C#, VS2008 I have the following main form class: [Main.cs] namespace Server {
I have following classes. class A { public: void fun(); } class B: public
I have the following classes public interface InterfaceBase { } public class ImplementA:InterfaceBase {
I have the following classes: public class Person { public String FirstName { set;
I have the following Extension Method Imports System.Runtime.CompilerServices Namespace Extensions Public Module IntegerExtensions <Extension()>
I have the following classes: Ingredients, Recipe and RecipeContent... class Ingredient(models.Model): name = models.CharField(max_length=30,
I have the following 3 classes Book Product SpecialOptions There are many Books, and
I have the following 3 rails classes, which are all stored in one table,
What is the best practice to implement the following: we have a classes Order

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.