如何在winform c中的一个面板上使2个线程工作#

时间:2017-03-30 13:40:14

标签: c# multithreading winforms

我试图在Windows窗体的同一面板上同时使两个线程工作,线程是相同的。我已经找到了答案,但没有问题对待这件事(至少从我所看到的)。这是代码:

form.cs中的代码:

semaphore = new Semaphore();



    p1t1 = new PanelThread(new Point(10, 10),
                         150, Direction.West, Track.track1, pnl1, typetrain.loco,
                         Color.Blue,
                         semaphore
                         );



    p2t2 = new PanelThread(new Point(390, 390),
                         150, Direction.East, Track.track2, panel5, typetrain.loco,
                         Color.Red,
                         semaphore
                         );

    green1t1 = new PanelThread(new Point(10, 10),
                         150, Direction.North, Track.track1, pnl1, typetrain.wagon,
                         Color.Green,
                         semaphore
                         );


    semThread = new Thread(new ThreadStart(semaphore.Start));

    thread1 = new Thread(new ThreadStart(p1t1.Start));

    thread2 = new Thread(new ThreadStart(p2t2.Start));

    thread3 = new Thread(new ThreadStart(green1t1.Start));

    this.Closing += new CancelEventHandler(this.Form1_Closing);

    semThread.Start();
    thread1.Start();
    thread2.Start();
    thread3.Start();

panelthread.cs中的代码:

public PanelThread(Point origin,
                       int delay,
                       Direction direction,
                       Track track,
                       Panel panel,
                       typetrain locwag,
                       Color colour,
                       Semaphore semaphore)
    {
        this.origin = origin;
        this.delay = delay;
        this.direction = direction;
        this.panel = panel;
        this.colour = colour;
        this.plane = origin;
        this.panel.Paint += new PaintEventHandler(this.panel_Paint);
        this.track = track;
        this.locwag = locwag;
        switch (this.direction)
        {
            case Direction.East:
                this.xDelta = -10;
                this.yDelta = 0;
                break;

            case Direction.West:
                this.xDelta = +10;
                this.yDelta = 0;
                break;

            case Direction.North:
                this.xDelta = 0;
                this.yDelta = -10;
                break;

            case Direction.South:
                this.xDelta = 0;
                this.yDelta = +10;
                break;
        }


        this.semaphore = semaphore;
    }

当我启动程序时,程序会在paint事件处理程序中出错,并在g.FillRectangle行显示System.ArgumentException:

private void panel_Paint(object sender, PaintEventArgs e)
{
    Graphics g = e.Graphics;

    SolidBrush brush = new SolidBrush(colour);
    g.FillRectangle(brush, plane.X, plane.Y, 10, 10);

    brush.Dispose();    //  Dispose of graphic
    g.Dispose();        //  resources  
}

更新: 我尝试在panelthread.cs中使用invoke,如下所示:

if (this.panel.InvokeRequired)
    {
        this.panel.Invoke((MethodInvoker)delegate { this.panel.Paint += new PaintEventHandler(this.panel_Paint); });
    }

我现在没有任何错误,但是没有创建正方形,所以我在panel_paint函数中根本没有。我肯定在Invoke调用中写错了,或者我把它写在了错误的地方。

1 个答案:

答案 0 :(得分:0)

我实际上发现了错误,并且发现panel_paint实际上总是在主线程上,错误发生在其他地方。最后,我看到我只需要在panel_paint中取出g.dispose命令,因为它在绘制下一个对象之前销毁了图形。