c#从UI线程静态更新控件

c#从UI线程静态更新控件,c#,multithreading,user-interface,static,C#,Multithreading,User Interface,Static,例外情况: Controls created on one thread cannot be parented to a control on a different thread. 很好地解释了如何在类的实例上执行此操作,(使用此关键字)静态不引用该实例,因此我在这里运气不佳 调用函数不是静态的,并且像这样调用此函数的类 DrawPlane.drawPlane(ref pnl); 同样在调用类中,我使用这个顽皮的属性试图缓解非法的跨线程 CheckForIllegalCrossThread

例外情况:

Controls created on one thread cannot be parented to a control on a different thread.
很好地解释了如何在类的实例上执行此操作,(使用此关键字)静态不引用该实例,因此我在这里运气不佳

调用函数不是静态的,并且像这样调用此函数的类

DrawPlane.drawPlane(ref pnl);
同样在调用类中,我使用这个顽皮的属性试图缓解非法的跨线程

CheckForIllegalCrossThreadCalls = false;
全班:

using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace Skork.ui {
class DrawPlane {

    private static int numX;
    private static int numY;
    private static BackgroundWorker bg;

    static DrawPlane() {
        numX = 0;
        numY = 0;
        bg = new BackgroundWorker();

    }      

    public static void drawPlane(ref Panel plane) {           
        bg.DoWork += Bg_DoWork;
        bg.RunWorkerAsync(plane);
    }

    private static void Bg_DoWork(object sender, DoWorkEventArgs e) {
        Panel plane = new Panel();

        if (e.Argument is Panel) {
            plane = (Panel) e.Argument;
        } else {
            throw new Exception("Object is not a panel!!" +
                e.Argument.ToString());
        }


        plane.Controls.Clear();
        numX = 0;
        numY = 0;

        Random rnd = new Random();
        Size sz = plane.Size;
        Point temp = new Point(0, 0);
        const int sizeUnit = 4; // 4 pixels wide
        bg.DoWork += Bg_DoWork; // add event
        int x = 0;

        for (int y = 0; y < sz.Height; y += sizeUnit * sizeUnit) {
            temp.X = 0;
            numY++;

            for (x = 0; x < sz.Width; x += sizeUnit * (sizeUnit / 2)) {

                if (x + temp.X < sz.Width) {
                    PictureBox unit = new PictureBox();
                    rnd = new Random(rnd.Next());

                    unit.Size = new Size(sizeUnit * sizeUnit, sizeUnit * sizeUnit);
                    unit.Location = new Point(x + temp.X, y + temp.Y);
                    unit.BackColor = Color.FromArgb(255, rnd.Next(255),
                        rnd.Next(255), rnd.Next(255));
                    unit.Click += Unit_Click;
                    plane.Controls.Add(unit);

                    temp.X += sizeUnit * 2;
                    temp.Y = 0;
                    numX++;

                } else {
                    continue;
               }
            }
        }
        numX = numX / numY; // determine number of boxes on X-axis
    }

    private static void Unit_Click(object sender, EventArgs e) {
        if (sender is PictureBox) {
            PictureBox p = (PictureBox)sender;
            MessageBox.Show(p.Location.ToString() + " = Number in x-axis " + numX + " - number in y-axis " + numY);
            return;
        }            
        throw new Exception("Not a picturebox for some reason. - " + sender.ToString());
    }        
}
使用系统;
使用系统组件模型;
使用系统图;
使用System.Windows.Forms;
命名空间Skork.ui{
类拉丝机{
私有静态int-numX;
私有静态整数;
私人静态后台工作人员bg;
静态绘图平面(){
numX=0;
numY=0;
bg=新的BackgroundWorker();
}      
公共静态空心图纸平面(参考面板平面){
bg.DoWork+=bg_DoWork;
bg.RunWorkerAsync(平面);
}
私有静态无效Bg_DoWork(对象发送方,DoWorkEventArgs e){
面板平面=新面板();
如果(例如,参数为面板){
平面=(面板)e.参数;
}否则{
抛出新异常(“对象不是面板!!”+
e、 Argument.ToString());
}
plane.Controls.Clear();
numX=0;
numY=0;
随机rnd=新随机();
尺寸sz=平面尺寸;
点温度=新点(0,0);
const int sizeUnit=4;//4像素宽
bg.DoWork+=bg_DoWork;//添加事件
int x=0;
对于(整数y=0;y

}

如果没有理由,您应该避免使用静态。编程类的全部思想是能够重用它们。这里没有后台工作人员,因为您只忙于控制。无法在非gui线程上访问。不要生成大量图片框,而是使用一个图片框。我建议重新考虑一下这个设计。有些技术没有得到正确应用。失去静力学,失去背景工作者。我重新考虑了整个设计。静态、后台工作程序和生成~n^2 picturebox不再是这种情况。我花了一些时间才更有力地运用这些技术,我需要时间重新考虑设计。在第一次迭代中,我没有花太多精力去研究它,我真的很关心结果,但是你的建设性反馈使我能够以完全不同的方式解决问题。