0

在代码中发现问题。似乎代码或线程(我认为)没有正确关闭。
WebDev.WebServer40.EXE 仍在运行并使用我计算机的所有内存。即使在我停止 Visual Studio(停止调试)之后,我也可以看到任务管理器的内存使用量在增加。
我需要使用任务管理器结束任务来停止进程。该代码在页面加载时执行,它假设在任务完成但未完成时终止/关闭所有操作。怎么解决问题??我应该添加/删除什么?

namespace test
{
    public partial class liveRecording : System.Web.UI.Page
    {
    //video codec
    AVIWriter writer = new AVIWriter("MSVC");  

    protected void Page_Load(object sender, EventArgs e)
    {
        string streamingSource = "http://xxx.sample.com:85/snapshot.cgi";
        string login = "login";
        string password = "password";

        JPEGStream JPEGSource = new JPEGStream(streamingSource);
        JPEGSource.Login = login;
        JPEGSource.Password = password;
        JPEGSource.NewFrame += new NewFrameEventHandler(video_NewFrame);
        JPEGSource.Start();
    }

    public bool IsRecording = false;
    int width = 0;
    int height = 0;

    Queue<Bitmap> frames = new Queue<Bitmap>(); //Queue that store frames to be written by the recorder thread

    private void video_NewFrame(object sender, NewFrameEventArgs eventArgs) //event handler for NewFrame
    {
        //get frame from JPEGStream source
        //Bitmap image = eventArgs.Frame;
        Bitmap image = (Bitmap)eventArgs.Frame.Clone(); //get a copy of the Bitmap from the source

        width = image.Width;
        height = image.Height;

        if (IsRecording)
        {
            //enqueue the current frame to be encoded to a video file
            frames.Enqueue((Bitmap)image.Clone());
        }

        if (!IsRecording)
        {
            IsRecording = true;
            Thread th = new Thread(DoRecording);
            th.Start();
        }
    }

    private void DoRecording()
    {
        //writer.FrameRate = 5;
        string SavingPath = (Server.MapPath("~\\video\\")); 
        string VideoName = "ICS_" + String.Format("{0:yyyyMMdd_hhmmss}", DateTime.Now) + ".avi";
        writer.Open(SavingPath + VideoName, width, height);

        DateTime start = DateTime.Now;
        while (DateTime.Now.Subtract(start).Seconds < 30)
        {
            if (frames.Count > 0)
            {
                Bitmap bmp = frames.Dequeue();
                writer.AddFrame(bmp);//add frames to AVI file
            }
        }
        writer.Close();//close
     }
  }
}
4

0 回答 0