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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T06:35:56+00:00 2026-05-18T06:35:56+00:00

My question involves events and where I am triggering the events in my class.

  • 0

My question involves events and where I am triggering the events in my class. This class wraps my TCP functionality and I am using TcpListener to achieve this. I realize some TCP stuff may be missing from the following example but I want to make things as simple as possible:

c# 2.0 sample

class MyTcpClass
{
   public delegate void ClientConnectHandler(Socket client, int clientNum);

   public event ClientConnectHandler ClientConnect;

   private Socket wellKnownSocket;
   private Socket[] clientSockets = new Socket[MAX_CLIENTS];
   private int numConnected = 0;

   private void OnClientConnect(Socket client, int clientNum)
   {
      if (ClientConnect != null)
         ClientConnect(client, clientNum);
   }

   public void StartListening()
   {
      //initialize wellKnownSocket
      //...
      wellKnownSocket.BeginAccept(new AsyncCallback(internal_clientConnect);
   }

   public void internal_clientConnect(IAsyncResult ar)
   {
      //Add client socket to clientSocket[numConnected]
      //numConnected++;
      //...
      wellKnownSocket.EndAccept(ar);

      OnClientConnect(clientSocket[numConnected], numConnected);          
      //error: event happens on different thread!!
   }
}

class MainForm
{
   void Button_click()
   {
      MyTcpClass mtc = new MyTcpClass();
      mtc.ClientConnect += mtc_ClientConnected;
   }

   void mtc_clientConnected(Socket client, int clientNum)
   {
      ActivityListBox.Items.Add("Client #" + clientNum.ToString() + " connected.");
      //exception: cannot modify control on seperate thread
   }
}

I guess my question is, without breaking this pattern too much, what makes more sense? Also, if anyone has a better more elegant solution they are welcome.

Theory

class MainForm
{
   public MainForm()
   {
      MyTcpClass mtc = new MyTcpClass();
      MyTcpClass2 mtc2 = new MyTcpClass2(this);  
      //this version holds a Form handle to invoke the event

      mtc.ClientConnect += mtc_uglyClientConnect;
      mtc2.ClientConnect += mtc2_smartClientConnect;
   }

   //This event is being called in the AsyncCallback of MyTcpClass
   //the main form handles invoking the control, I want to avoid this
   void mtc_uglyClientConnect(Socket s, int n)
   {
      if (mycontrol.InvokeRequired)
      {
         //call begininvoke to update mycontrol
      }
      else
      {
         mycontrol.text = "Client " + n.ToString() + " connected.";
      }
   }

   //This is slightly cleaner, as it is triggered in MyTcpClass2 by using its
   //passed in Form handle's BeginInvoke to trigger the event on its own thread.
   //However, I also want to avoid this because referencing a form in a seperate class
   //while having it (the main form) observe events in the class as well seems... bad
   void mtc2_smartClientConnect(Socket s, int n)
   {
      mycontrol.text = "Client " + n.ToString() + " connected.";
   }
}
  • 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-18T06:35:57+00:00Added an answer on May 18, 2026 at 6:35 am

    While you did not post the code for MyTcpClass2, I’m pretty sure I see what you’re getting at.

    No, I wouldn’t do it the second way. Because, for example, if anything else ever needs to bind to that event at the same time, you are going to force it to run on another thread.

    In short, the method that receives the event should be responsible for running whatever code it needs to on whatever thread it needs to. The mechanism that raises the event should be completely oblivious to any sort of weird threading stuff that the receiver requires. Aside from complicating the multiple-event-binding scenario, it moves the logic of cross-thread invocation into a class where it doesn’t belong. The MyTcpClass class should be focused on handling TCP client/server matters, not mucking around with Winforms threads.

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

Sidebar

Related Questions

Question Using XSLT 1.0, given a string with arbitrary characters how can I get
As part of another question that did involve programming (I assure you), someone suggested
Question as stated in the title.
Question is pretty self explanitory. I want to do a simple find and replace,
Question Alright, I'm confused by all the buzzwords and press release bingo going on.
Question in the title. And what happens when all 3 of $_GET[foo] , $_POST[foo]
Question Can I build a image database/library that has an e-commerce style checkout system
Question says it all, really. My application is a time tracker. It's currently written
Question: ((lambda (x y) (x y)) (lambda (x) (* x x)) (* 3 3))
Question Is there a way to have a method that will always run anytime

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.