calling concurrently Graphics.Draw and new Bitmap from memory in thread take long time

Posted by Abdul jalil on Stack Overflow See other posts from Stack Overflow or by Abdul jalil
Published on 2009-10-20T15:20:21Z Indexed on 2010/05/08 9:18 UTC
Read the original article Hit count: 358

Example1 public partial class Form1 : Form {

    public Form1()
    {

        InitializeComponent();
    pro = new Thread(new ThreadStart(Producer));
    con = new Thread(new ThreadStart(Consumer));
    }
    private AutoResetEvent m_DataAvailableEvent = new AutoResetEvent(false);
    Queue<Bitmap> queue = new Queue<Bitmap>();
    Thread pro;
    Thread con ;

    public void Producer()
    {
        MemoryStream[] ms = new MemoryStream[3];
        for (int y = 0; y < 3; y++)
        {
            StreamReader reader = new StreamReader("image"+(y+1)+".JPG");
            BinaryReader breader = new BinaryReader(reader.BaseStream);

            byte[] buffer=new byte[reader.BaseStream.Length];
            breader.Read(buffer,0,buffer.Length);
            ms[y] = new MemoryStream(buffer);
        }
        while (true)
        {
            for (int x = 0; x < 3; x++)
            {
                Bitmap bmp = new Bitmap(ms[x]);
                queue.Enqueue(bmp);

                m_DataAvailableEvent.Set();
                Thread.Sleep(6);
            }
        }
    }
    public void Consumer()
    {
        Graphics g= pictureBox1.CreateGraphics();
        while (true)
        {
            m_DataAvailableEvent.WaitOne();
            Bitmap bmp = queue.Dequeue();
            if (bmp != null)
            {
              //  Bitmap bmp = new Bitmap(ms);
                g.DrawImage(bmp,new Point(0,0));
                bmp.Dispose();

            }
        }

    }
    private void pictureBox1_Click(object sender, EventArgs e)
    {

        con.Start();
        pro.Start();

    }
}

when Creating bitmap and Drawing to picture box are in seperate thread then Bitmap bmp = new Bitmap(ms[x]) take 45.591 millisecond and g.DrawImage(bmp,new Point(0,0)) take 41.430 milisecond

when i make bitmap from memoryStream and draw it to picture box in one thread then Bitmap bmp = new Bitmap(ms[x]) take 29.619 and g.DrawImage(bmp,new Point(0,0)) take 35.540 the code is for Example 2 is

why it take more time to draw and bitmap take time in seperate thread and how to reduce the time when processing in seperate thread. i am using ANTS performance profiler 4.3 public Form1() {

        InitializeComponent();
    pro = new Thread(new ThreadStart(Producer));
    con = new Thread(new ThreadStart(Consumer));
    }
    private AutoResetEvent m_DataAvailableEvent = new AutoResetEvent(false);
    Queue<MemoryStream> queue = new Queue<MemoryStream>();
    Thread pro;
    Thread con ;

    public void Producer()
    {
        MemoryStream[] ms = new MemoryStream[3];
        for (int y = 0; y < 3; y++)
        {
            StreamReader reader = new StreamReader("image"+(y+1)+".JPG");
            BinaryReader breader = new BinaryReader(reader.BaseStream);

            byte[] buffer=new byte[reader.BaseStream.Length];
            breader.Read(buffer,0,buffer.Length);
            ms[y] = new MemoryStream(buffer);
        }
        while (true)
        {
            for (int x = 0; x < 3; x++)
            {
               // Bitmap bmp = new Bitmap(ms[x]);
                queue.Enqueue(ms[x]);

                m_DataAvailableEvent.Set();
                Thread.Sleep(6);
            }
        }
    }
    public void Consumer()
    {
        Graphics g= pictureBox1.CreateGraphics();
        while (true)
        {
            m_DataAvailableEvent.WaitOne();
            //Bitmap bmp = queue.Dequeue();
            MemoryStream ms= queue.Dequeue();
            if (ms != null)
            {
                Bitmap bmp = new Bitmap(ms);
                g.DrawImage(bmp,new Point(0,0));
                bmp.Dispose();

            }
        }

    }
    private void pictureBox1_Click(object sender, EventArgs e)
    {

        con.Start();
        pro.Start();

    }

© Stack Overflow or respective owner

Related posts about c#

Related posts about multithreading