是否可以在线程中传递参数?

时间:2014-12-28 03:05:36

标签: c#

嘿所有程序员我试图通过一个线程传递一个图形参数,但每次我得到一个期望错误,有人可以请帮助。

我的代码:

private Thread GraphicThread;

public void GraphicThreading(Graphics g)
{
    GraphicThread = new Thread(delegate()
    {
        Render(g);
    });
    GraphicThread.Start();
}
public void Render(Graphics g)
{
    g.FillRectangle(new SolidBrush(Color.Red), 0, 0, 100, 100); 
}

我的错误:

  

发生了'System.ArgumentException'类型的未处理异常   System.Drawing.dll程序

     

附加信息:参数无效。

看到的问题是,因为我有一个双缓冲的面板我需要直接绘制到它我试过你刚才链接Arkady的方法它并没有给我任何错误,但它没有显示任何东西,因为它需要直接绘制到面板中,以便可以缓冲。

所以这是我的主要课程:

公共部分类引擎:表单     {         //将看到命令行         public const int ENGINE_DISPLAY_WIDTH = 1041;         public const int ENGINE_DISPLAY_HEIGHT = 539;

    [DllImport("kernel32.dll", SetLastError = true)]
    [return: MarshalAsAttribute(UnmanagedType.Bool)]
    static extern bool AllocConsole();

    public Engine()
    {
        InitializeComponent();
    }
    private GraphicEngine GraphicalEngine;
    private void display_Paint(object sender, PaintEventArgs e)
    {
        Console.WriteLine("*-SimpleEngine: DoubleBuffering And GraphicsEngine Has Started-*");
        display = new DoubleBuffering();


        GraphicalEngine = new GraphicEngine();

        GraphicalEngine.GraphicThreading(e.Graphics);
    }

    private void Engine_Load_1(object sender, EventArgs e)
    {
        AllocConsole();
    }
}

这是display_paint函数将数据发送到的类:

 class GraphicEngine
{
    //Graphic Objects
    private Thread GraphicThread;
    //Rate
    //Functions
    public void GraphicThreading(Graphics g)
    {
        GraphicThread = new Thread(delegate()
        {
            GraphicalDisplay(g);
        });
        GraphicThread.Start();
    }


    float Pos = 0;
    public void GraphicalDisplay(Graphics g)
    {
        g.FillRectangle(new SolidBrush(Color.Gray), 0, 0, Engine.ENGINE_DISPLAY_WIDTH, Engine.ENGINE_DISPLAY_HEIGHT);
        while (true)
        {

            g.DrawImage(SimpleEngine_Improved.Properties.Resources.player, (int)Pos, (int)Pos);

            Pos+= 0.1f;
            Thread.Sleep(2);
        }

    }



}

0 个答案:

没有答案