C#服务器套接字我在system.drawing.dll中发生了'system.stackoverflowexception'类型的未处理异常
在那里我试图在服务器应用程序上接收一系列图片帧,并在开始程序工作完美时将它们显示在图片框中但是在5到10分钟之后取决于我发送的帧我得到错误未处理的类型异常System.Drawing.dll中出现'System.StackOverflowException'服务器中的代码看起来像是这样的
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
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尝试
pictureBox1.Image.Dispose();
和我也试图删除图片框并再次重新调整但仍然有似乎问题
所以任何想法解决问题伙计们?
我尝试了什么:
i试过
pictureBox1.Image.Dispose();
和我也试图删除图片框并再次重新调整它但仍然有似乎问题
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
你正在递归地调用ImgRecive()
,所以你收到的每个图像都要分配另一个(可能是大的)堆栈帧。
您需要将递归更改为某种循环结构。
You are callingImgRecive()
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.
我用while循环替换了递归,它对我有用,新代码就像这样
i replaced the recursion with while loop and it worked for me the new code is like this
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); }
}
}
所以如果这对任何人有帮助我会很高兴:D
so if that helped any one i will be happy :D