进程/任务完成后线程未退出

时间:2012-10-11 06:48:04

标签: c# asp.net webforms

在代码中发现问题。似乎代码或线程(我认为)没有正确关闭。
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
     }
  }
}

0 个答案:

没有答案