简体   繁体   English

在线程中从内存中同时调用 Graphics.Draw 和 new Bitmap 需要很长时间

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

Example1示例 1

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]) takes 45.591 millisecond and g.DrawImage(bmp,new Point(0,0)) takes 41.430 milisecond.当创建位图和绘制到图片框在单独的线程中时, Bitmap bmp = new Bitmap(ms[x])需要 45.591 毫秒, g.DrawImage(bmp,new Point(0,0))需要 41.430 毫秒。

When I make a bitmap from memoryStream and draw it to picture box in one thread then Bitmap bmp = new Bitmap(ms[x]) takes 29.619 and g.DrawImage(bmp,new Point(0,0)) takes 35.540.当我从 memoryStream 制作位图并将其绘制到一个线程中的图片框时, Bitmap bmp = new Bitmap(ms[x])需要 29.619, g.DrawImage(bmp,new Point(0,0))需要 35.540。

The code is for Example 2 is示例 2 的代码是

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();

}

Why does it take more time to draw and create a bitmap in seperate thread and how to reduce the time when processing in seperate thread ?为什么在单独的线程中绘制和创建位图需要更多时间以及如何减少在单独线程中处理的时间? I am using ANTS performance profiler 4.3我正在使用 ANTS 性能分析器 4.3

All UI operations should take place on the same thread.所有 UI 操作都应该在同一个线程上进行。 In fact if you want your system to be stable they must all take place on the same thread.事实上,如果你想让你的系统稳定,它们必须都发生在同一个线程上。 I guess that what you're seeing with the slower performance on the multithreaded code is evidence of that instability.我猜你在多线程代码上看到的性能较慢是这种不稳定性的证据。

By all means load the images in separate threads, but update the UI in the main thread.无论如何,在单独的线程中加载图像,但在主线程中更新 UI。

If you search for questions tagged [multithreading] [c#] here on Stack Overflow you'll see many other questions along the same lines as yours.如果您在 Stack Overflow 上搜索标记为 [multithreading] [c#] 的问题,您会看到许多其他问题与您的问题相同。

See here and here for example.例如,请参见此处此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM