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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:30:29+00:00 2026-06-18T07:30:29+00:00

I have an application that is using asynchronous sockets with the begin/end design pattern.

  • 0

I have an application that is using asynchronous sockets with the begin/end design pattern.

Whenever I receive a new connection I create an object and store it in a collection. This object also stores the socket connection within itself and will be passed along in the begin methods “object state”.

Now that the server is running asynchronously, sending, receiving etc, there are multiple code paths executing at any given time for an object.

What I do not understand is:
If I call a function passing in my current object and in another thread, that object was just disposed, what happens to the other object that is currently inside executing code.

EDIT:

ex.)If you look at ReadDataCallback() where it is calling doSomthing(). If it is about to call doSomthing() with my “device” object, but the corresponding device had an exception in the SendCallback(). What state is the device object in that is just about to call doSomthing()?

Here is a code example.

void waitForData(MyDevice device)
{
    try
    {
        if (device.SocketState.IsSSL)
        {
            device.SocketState.sslStream.BeginRead(device.SocketState.DataBuffer, 0, device.SocketState.DataBuffer.Length, m_readDataCallback, device);
        }
        else
        {
            device.SocketState.DeviceSocket.BeginReceive(device.SocketState.DataBuffer, 0, device.SocketState.DataBuffer.Length, SocketFlags.None, m_readDataCallback, device);
        }
    }
    catch (SocketException se)
    {
        DisconnectAndRemove(device);
    }
}

public void ReadDataCallback(IAsyncResult ar)
{
    MyDevice device = (MyDevice)ar.AsyncState;
    try
    {
        Queue<kustompacket> qps = null;
        int iRx = 0;

        if (device.SocketState.IsSSL)
        {
            iRx = device.SocketState.sslStream.EndRead(ar);
            if (iRx == 0)
            {
                DisconnectAndRemove(device);
            }
            else if (iRx > 0)
            {
                device.CircularBuff.Add(iRx, device.SocketState.DataBuffer);
                qps = device.CircularBuff.ReadPackets();
                doSomthing(device);
            }
        }
        else
        {
            if (device.SocketState.DeviceSocket != null)
            {
                if (device.SocketState.DeviceSocket.Connected)
                {
                    // Read data from the client socket.
                    iRx = device.SocketState.DeviceSocket.EndReceive(ar);
                    if (iRx == 0)
                    {
                        DisconnectAndRemove(device);
                    }
                    else if (iRx > 0)
                    {
                        device.CircularBuff.Add(iRx, device.SocketState.DataBuffer);
                        qps = device.CircularBuff.ReadEncryptedPackets(device.SocketState.SessionID);
                        doSomthing(device);
                    }
                }
            }
        }

        if (qps != null)
        {
            MyDelegate meh = new MyDelegate(HandleDataReceived);
            meh.BeginInvoke(device, qps, null, null);
        }

        if (iRx != 0)
        {
            waitForData(device);
        }
    }
    catch (ObjectDisposedException ode)
    {
        //Socket has been closed
        //DisconnectAndRemove(device);
    }
    catch (SocketException se)
    {
        //if (se.ErrorCode == 10054) // Error code for Connection reset by peer
        //{
        DisconnectAndRemove(device);
        //}
    }
    catch (Exception ex)
    {
        DisconnectAndRemove(device);
    }
}


public void SendCallback(IAsyncResult ar)
{
    MyDevice device = (MyDevice)ar.AsyncState;
    try
    {
        // Complete sending the data to the remote device.
        if (device.SocketState.IsSSL)
        {
            device.SocketState.sslStream.EndWrite(ar);
        }
        else
        {
            int bytesSent = device.SocketState.DeviceSocket.EndSend(ar);
        }
        device.ResetAge();
    }
    catch (SocketException se)
    {
        DisconnectAndRemove(device);
    }
    catch (Exception ex)
    {
    }

    if (device.SocketState.IsSSL)
    {
        device.Write();
    }
}
  • 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-18T07:30:30+00:00Added an answer on June 18, 2026 at 7:30 am

    If an object is disposed on one thread, it is disposed on all threads. If you do anything with it after the other thread disposes it, it should (but is not guaranteed to) throw an ObjectDisposedException.

    http://msdn.microsoft.com/en-us/library/system.objectdisposedexception.aspx

    If you are disposing an object on a thread while another thread may be expecting to work with it, you are doing something wrong. Consider controlling life time of the object on a single thread in a deterministic way.

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

Sidebar

Related Questions

I have got a Client/Server Application that using Asynchronous Socket.My problem is i cant
We have an application that logs using log4net. But we would like to delete
I have an application that is using both the carrierwave & tire gems. The
I have an MVC application that is using MEF for the controllers, all working
I have a web application that is using both jQuery 1.2.6 and YUI 2.6.0
I have a Silverlight application that is using a DataGrid. Inside of that DataGrid
I have a WCF application that is using NetTcpBinding . I want to invoke
I have a WPF application that is using a WindowsFormsHost control to host a
Here's the core problem: I have a .NET application that is using COM interop
I have created an android application that calls (using kSOAP library) a SOAP based

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.