Click here to Skip to main content
13,766,878 members
Rate this:
Please Sign up or sign in to vote.
See more:

I have client/server program
I get this exception in the server
Cannot access a disposed object. Object name: 'System.Net.Sockets.TcpClient'.

how can solve it ?

this is my code
the server :
public Bitmap ResizeBitmap(Bitmap b, int nWidth, int nHeight)
   Bitmap result = new Bitmap(nWidth, nHeight);
   using (Graphics g = Graphics.FromImage((Image)result)) g.DrawImage(b, 0, 0, nWidth, nHeight);
   return result;

NetworkStream myns;

private void timer1_Tick(object sender, EventArgs e)
   listBox1.SetSelected(0, true);
   ClientInfo clientInfo = (ClientInfo)listBox1.SelectedItem;
   Client = clientInfo.Client;
   Bitmap bt;
   bt = new Bitmap(CaptureScreen.CaptureDesktopWithCursor());
   pictureBox1.Image = ResizeBitmap(bt, 877, 482);
   MemoryStream ms = new MemoryStream();
   pictureBox1.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
   byte[] arrImage = ms.GetBuffer();
   myns = Client.GetStream();
   BinaryWriter mysw = new BinaryWriter(myns);
   mysw.Write(arrImage);//send the stream to above address

please help me
Posted 14-Jun-12 8:24am
Updated 14-Jun-12 9:42am
OriginalGriff 14-Jun-12 14:03pm
Where do you get the error?
maxpower12345 14-Jun-12 14:24pm
in : myns = Client.GetStream();
Sergey Alexandrovich Kryukov 14-Jun-12 15:26pm
Why would you do all that? What's the purpose of your application? Why timers? What is exact timer type?
Please see my comment to the answer by Tim.
maxpower12345 14-Jun-12 15:51pm
i have server take a screenshot every 10 msec. by timer
and send the pic. to the client
it just send the first pic.
then the error happened
Amund Gjersøe 15-Jun-12 3:06am
You do a Client.Close() that renders the Client useless for later use. You have to re-initialize it, or keep it open.

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

I think this Stack Overflow question can shed some light onto your issue:[^]

This one also has some good information:[^]

It looks like you might be dealing with a threading issue. One thing I would recommend is putting the NetworkStream declaration inside the Tick method. Also, since you are working with bitmaps and other heavy items, I would recommend turning off the timer at the start of the tick method and then turning it back on after it finishes. That will mess with how often your event fires, but that should prevent a lot of problems. If that isn't viable, you might need to look at using async and a task for this.
Sergey Alexandrovich Kryukov 14-Jun-12 15:25pm
That is a good explanation why timers are bad in principle with such things (and many others). Unfortunately, we do not know the OP's purpose this time, but usually, the safe, reliable and easy solution would be using threads.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Cookies | Terms of Service
Web06-2016 | 2.8.181114.1 | Last Updated 14 Jun 2012
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100