Click here to Skip to main content
15,920,217 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi there Im trying to receive series of pictures frames on server application and display them in picture box in the begin the program work perfect but after 5 to 10 minuets depends on how frame I send I got error "An unhandled exception of type 'System.StackOverflowException' occurred in System.Drawing.dll" my code in the server look like that


C#
public void ImgRecive()
    {
       try
       {
            tcp.Start();
            sock = tcp.AcceptSocket();
            ns = new NetworkStream(sock);
            pictureBox1.Image = Image.FromStream(ns);
            tcp.Stop();
            if (sock.Connected == true)
            {
                while (true) { ImgRecive(); }
            }
            ns.Flush();
        }
        catch (Exception exxx) { MessageBox.Show(exxx.Message); }
    }


i tried
pictureBox1.Image.Dispose();
and i also tried to remove the picture box and recreat it again but still have the seem problem

so any idea to solve the problem guys?

What I have tried:

i tried
pictureBox1.Image.Dispose();
and i also tried to remove the picture box and recreat it again but still have the seem problem
Posted
Updated 6-Mar-16 20:16pm

You are calling ImgRecive() recursively, so each image you receive, you are allocating another (probably large) stack frame.
You need to change your recursion to some sort of looping construct.
 
Share this answer
 
Comments
Member 12178782 7-Mar-16 2:16am    
thanks peter that was very helpful
i replaced the recursion with while loop and it worked for me the new code is like this

C#
public void ImgRecive()
    {
        tcp.Start();
        sock = tcp.AcceptSocket();
        ns = new NetworkStream(sock);
        while (sock.Connected == true)
        {
            try
            {
                tcp.Start();
                sock = tcp.AcceptSocket();
                ns = new NetworkStream(sock);
                pictureBox1.Image = Image.FromStream(ns);
                tcp.Stop();
                ns.Flush();
            }
            catch (Exception exxx) { MessageBox.Show(exxx.Message); }
        }
    }


so if that helped any one i will be happy :D
 
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