Click here to Skip to main content
15,891,687 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
hello

why can't but
C#
Client.GetStream();

in timer ?

and give an error
Cannot access a disposed object. Object name: 'System.Net.Sockets.TcpClient'.

??
please I need a help

this is the code
C#
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;
      byte[] arrImage;

      private void timer1_Tick(object sender, EventArgs e)
      {

          timer1.Enabled = false;


          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);
          arrImage = ms.GetBuffer();
          ms.Close();

          listBox1.SetSelected(0, true);

          ClientInfo clientInfo = (ClientInfo)listBox1.SelectedItem;

          Client = clientInfo.Client;

          Thread t = new Thread(new ParameterizedThreadStart(send));
          t.Start();
          timer1.Enabled = true;

      }


      private void send(object client)
      {
          try
          {
              myns = Client.GetStream();
              BinaryWriter mysw = new BinaryWriter(myns);
              mysw.Write(arrImage);//send the stream to above address

              mysw.Close();
              myns.Close();
              myns.Flush();
              Client.Close();
          }
          catch (Exception e)
          {
              MessageBox.Show(e.Message);
          }


      }


the error in line
C#
myns = Client.GetStream();


the project is there server take a screenshot and send the pic. to client as(remote pc)
Posted
Updated 14-Jun-12 11:36am
v3
Comments
Kschuler 14-Jun-12 17:19pm    
You need to provide A LOT more context than just that one line of code. Click the Improve Question link and add more of your code to the question. Include the relevant code...for example, I'm assuming when you say this code is "in timer" you mean that it's in the Tick event handler, is that correct? If so, show us that code. The error message would suggest that you at one point disposed something and then are trying to use it again...are you?
Sergey Alexandrovich Kryukov 14-Jun-12 17:44pm    
Please do not re-post. I told you using a timer is a bad idea. You need to explain why are you doing it, what do you want to achieve.
--SA
maxpower12345 14-Jun-12 18:19pm    
the timer I use it to make screenshot every 10 second
that the reason why I use timer
and this is the only way
Sergey Alexandrovich Kryukov 14-Jun-12 20:47pm    
No, this is not yet the reason. Now, why do you need a screenshot every 10 seconds, not 5 or 11? Do you know that there is no deterministic way to guarantee time of execution anyway. Therefore, use threads. You can even get much better accuracy (if compared to no-good System.Windows.Timer, for example; by the way, you did not tell us the type of the timer -- there are at least 3), and surely much better reliability.
--SA
miwuawen 14-Jun-12 21:20pm    
Imports System.Net.Sockets

1 solution

add this space :Imports System.Net.Sockets
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900