【问题标题】:Draw on a form by a separate thread通过单独的线程在表单上绘图
【发布时间】:2012-05-29 16:07:14
【问题描述】:

我正在尝试构建一个多线程游戏,其中我有一个单独的线程用于在不是主线程的表单上绘画。这给我们带来了线程安全技术,我已经阅读了很多文章,但我不确定我是否理解正确。

我的问题是我有一个结构,其中每个数据对象都在表单上自行绘制,所以我不知道如何实现它。

这是我的工作单线程代码的 sn-p:

public partial class Form1 : Form
{
    GameEngine Engine;
    public Form1()
    {
        InitializeComponent();
        Engine = new GameEngine();
    }
    protected override void OnPaint(PaintEventArgs e)
    {
        Engine.Draw(e.Graphics);
    }

}

class GameEngine
{

    Maze Map;
    List<Player> Players;

    public void Draw(Graphics graphics)
    {
            Map.Draw(graphics);
            foreach (var p in Players)
            {
                p.Draw(graphics);
            }
    }

}

所以请任何人给我一个提示或链接到好文章帮助我学习如何在另一个线程上分离绘图?

[编辑]

我设法实现了我打算做的事情 这就是我的编码方式

    protected override void OnPaint(PaintEventArgs e)
    {
        formGraphics = e.Graphics;
        DisplayThread = new Thread(new ThreadStart(Draw));
        DisplayThread.Start();
    }

    private void Draw()
    {
        if (this.InvokeRequired)
        {
            this.Invoke(new DrawDelegate(this.Draw));
        }
        else
        {
            Engine.Draw(formGraphics);
        }
    }

但我得到了一个 ArgumentException:参数无效

请您指出该代码中的错误

【问题讨论】:

  • 那么你想在这段代码的哪里插入线程?
  • @Tudor 回答您的问题,请参阅编辑段落
  • 哪一行抛出ArgumentException?
  • 您在处理 e.Graphics 后正在使用它。你不能这样做。

标签: c# winforms multithreading


【解决方案1】:

我认为您需要绘制位图,然后在 OnPaint 方法中将该位图绘制到窗口。我稍后会演示。

正如 Hans 指出的,在 OnPaint 方法中你正在设置

formGraphics = e.Graphics;

但是在方法 e.Graphics 的末尾被释放,所以如果你的代码到了,你就不能再使用它了

Engine.Draw(formGraphics);

你会得到一个例外。

所以基本上你需要一个全局的

Bitmap buffer = new Bitmap(this.Width, this.Height)

在你的异步线程中,你可以调用你的绘图到你可以使用的位图

Graphics g=Graphics.FromBitmap(buffer);//

要获得一个图形对象,但记住你必须这样做

g.Dispose()

它或将它包装在一个

中
using (Graphics g=Graphics.FromBitmap(buffer))
{
//do something here

}

我会用它玩一会儿,看看能不能给你一个工作样本

编辑这是您的工作示例。 我开始了一个新表格并在上面扔了一个按钮。我将主窗体背景图像布局更改为无。

我认为您需要使用 .net 4.0 或更高版本,如果不使用此版本,请告诉我我可以更改它以匹配您的版本...我认为。

//you need this line to use the tasks
using System.Threading.Tasks;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    public void Draw()
    {
        Bitmap buffer;
        buffer = new Bitmap(this.Width, this.Height);
        //start an async task
        Task.Factory.StartNew( () =>
        {
                using (Graphics g =Graphics.FromImage(buffer))
                {
                    g.DrawRectangle(Pens.Red, 0, 0, 200, 400);
                    //do your drawing routines here
                }
            //invoke an action against the main thread to draw the buffer to the background image of the main form.
            this.Invoke( new Action(() =>
            {
                    this.BackgroundImage = buffer;
            }));
        });

    }

    private void button1_Click(object sender, EventArgs e)
    {
        //clicking this button starts the async draw method
        Draw();
    }

}

}

【讨论】:

    猜你喜欢
    • 2012-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多