C# 可以在线程中传递参数吗?

C# 可以在线程中传递参数吗?,c#,C#,嘿,所有的程序员,我正试图通过一个线程传递一个图形参数,但每次我这样做,我都会得到一个期望错误,请有人帮忙 我的代码: private Thread GraphicThread; public void GraphicThreading(Graphics g) { GraphicThread = new Thread(delegate() { Render(g); }); GraphicThread.Start(); } public void

嘿,所有的程序员,我正试图通过一个线程传递一个图形参数,但每次我这样做,我都会得到一个期望错误,请有人帮忙

我的代码:

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的方法,它不会给我任何错误,但它不会显示任何内容,因为它需要直接绘制到面板中,以便可以缓冲

这是我的主要课程:

公共部分类引擎:表单 { //命令行将被看到 公共常数引擎显示宽度=1041; 发动机显示高度的公共常数=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);
        }

    }



}

您是否正在使用GDI(一个
图形
对象)从背景线程中的
OnPaint
?或者这是您自己创建的
Graphics
实例?使用面板进行绘制,然后从那里我将其发送到GraphicsThreading函数您不能对该函数执行线程,传递给该函数的
Graphics
对象可能会在您对其进行绘制之前被处理。是吗还有另一种方法可以在面板中显示图形,顺便说一句,我也在使用双缓冲面板?当然,请手动进行双缓冲,绘制到线程本地
Bitmap
,然后当yoyu在UI线程上完成在UI上绘制位图时。但是,如果您在UI上做了这么多工作,您可能需要对绘图执行线程最好放弃winforms,使用一个设计用于快速更新的库,比如或XNA