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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:16:30+00:00 2026-06-04T05:16:30+00:00

I am making a server-client project. Here the server makes changes in database based

  • 0

I am making a server-client project. Here the server makes changes in database based on the clients calls. The clients calls are asynchronous. I am invoking some delegates based on the incoming call using events. The below code can give you idea

public delegate void Client_Logging_In_Delegate(WebSocketSession session, string e);
public event Client_Logging_In_Delegate Admin_Logging_In_Event;
public void Client_Logging_In_Function(WebSocketSession session, string e)
{
    //Database -> this particular client status= true
}

public void Main()
{
    server.NewMessageReceived += new SessionEventHandler<WebSocketSession, string>     (server.WebSocketServer_NewMessageReceived);

    server.Client_Logging_In_Event = new Client_Logging_In_Delegate(server.Client_Logging_In_Function);
}

public void WebSocketServer_NewMessageReceived(WebSocketSession session, string e)
{
    string type = e.Substring(0, e.IndexOf("#"));
    if (type == "Client_Logging_In")
        Client_Logging_In_Event(session, e);
}

I am running the both in same system. I didnt receive any crash yet. But in real case the clients => 5 -> 20 . So the question is if two events arrived simultaneously then what will happen ?

EDIT:

I am using a third party library “SuperWebsocket” for server part. This one defines the message_Received event. The documentation says that it is thread safe. Mine is a single thread application. There is only one server object. Some functions(called by delegates) have some long codes. For example take this

public void called_By_Some_Delegate(WebSocketSession session, string e)
{
       //1) retrieve a big list from database
       //2) change the list into a dictionary object
       //3) create a string using dictionary object
       //4) send this string to the client using the "session" object
}

Now, assume there are two events arrived for the same delegate/function. Now assume the first one will execute the function and it executes in the second point. If the second one accesses the function at the same time it will change the list. So, I think there will be a crash. This is the situation I have to figure out.

  • 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-04T05:16:32+00:00Added an answer on June 4, 2026 at 5:16 am

    When you say simultaneously, I kind of understand that there are more than one thread in the server listening for requests and raising events. In this case each of the thread are going to fire the events and the event handlers are going to run in the respective threads without any problems (provided the event handlers are thread safe).

    If not, and the there is only one thread listening to the socket, then anyway you are sequential and should not have issues.

    If the listening thread triggers another thread for processing the incoming requests, again you have the event handler run in its own thread context and as long as your handler is thread safe, then you are still safe.

    I wrote the below program to simulate the server generating multiple requests and processing the same.

    using System;
    using System.Threading;
    namespace AsyncFiber
    {
        class Program
        {
            public event EventHandler<int> ClientEventReceived;
            public event EventHandler<int> MessageProcessed;
            static void Main(string[] args)
            {
                Program p = new Program();
                p.ClientEventReceived += p.p_ClientEventReceived;
                p.MessageProcessed += p.p_MessageProcessed;
                for (int i = 0; i < 5; i++)
                    new Thread(new ThreadStart(p.target)).Start();
            }
            private void p_MessageProcessed(object sender, int e)
            {
                PrintHelper("C->" + e);
            }
    
            private void p_ClientEventReceived(object sender, int e)
            {
                MessageProcessed(sender, e);
            }
    
            void target()
            {
                while (true)
                {
                    var data = new Random().Next();
                    Console.WriteLine("P->" + data);
                    ClientEventReceived(Thread.CurrentThread.Name, data);
                }
            }
    
            void PrintHelper(string message)
            {
                for (int i = 0; i < Thread.CurrentThread.ManagedThreadId; i++)
                    Console.Write("  ");
                Console.Write(message);
                Console.WriteLine();
            }
        }
    }
    

    So to answer your question the event handlers will be executed in respective threads simultaneously. However the database locking scheme should be clear enough to handle this scenario.

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

Sidebar

Related Questions

I am making a simple client/server that will pass strings back and forth. It
I am making a C# client / Java server chatroom, and everything works fine
I`m making an little php server that has 2 clients, on connects to the
in making a simple cgi server for a course. To do that in some
I am making HTTP connections to server to get some data. It works on
I'm making AJAX calls to a server that sometimes return unparseable JSON. The server
I have a client-server project (small project for companies in C#) and the server
I have a Appengine connected Andorid project, to enable RPC calls between the server
I'm making a client-server program for picking a file from a location on a
I am making an server/client application. I set server socket to listening, and set

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.