从线程中的类访问动态控件

时间:2012-05-24 14:04:07

标签: c# forms interface controls invoke

我正在开发一个小型项目,处理轮询设备以检查I / O控件的状态。我已经实现了一个处理特定设备的小项目,但已经决定我最终会实现不同的设备,因此已经转移到了一个类:接口方法。然而,这引起了一些问题,因为我移动了很多代码。

在我移动代码之前,我正在通过使用委托来访问动态表单控件;

 if (result != null)
            {
                this.Invoke((MethodInvoker)delegate
                {
                    txtOutput1.Text = (result[4] == 0x00 ? "HIGH" : "LOW"); // runs on UI thread

                    if (result[4] == 0x00)
                    {
                        this.Controls["btn" + buttonNumber].BackColor = Color.Green;
                    }
                    else
                    {
                        this.Controls["btn" + buttonNumber].BackColor = Color.Red;
                    }


                });

            }

这很好用,直到我将某些方法移动到一个继承自接口的新类。我不想只是将动态按钮设置为公共,我不确定我可以创建get; set;对于动态按钮,考虑到很多它们并在启动时创建。另一个问题是this.invoke“命令。我相信invoke命令不起作用,除非它被放在一个表单上......现在它被移动到一个类,所以我需要看看另一种方法。< / p>

有没有人对我应该采用哪种方式有任何想法?

编辑1:

该程序被设计为用于处理输入/输出的硬件设备的监视系统。使用这些我可以检查,例如,是否已触发门警报等。程序本身在形式/设计方面非常简单。目前我有一个表单,它根据数据库中的信息生成按钮,例如,如果配置了10个设备,则有10个按钮。每个都显示绿色/红色取决于硬件状态。

我的主窗体为每个监视它的设备触发一个线程,但是因为我希望有多种类型的设备,我将它们移动到不同的类和一个处理所有常用方法的接口。目前我有一个设备类,它实现了一个接口。关于这个问题,我现在需要访问我正在更新的单个主窗体的实例,而不是创建一个新实例,以便我可以使用我将所述逻辑移动到窗体本身时创建的新方法

编辑2:

    IdeviceInterface bfdeviceimp = new bf2300deviceimp();
   // some other declarations and initialize components

 private void btnConnect_Click(object sender, EventArgs e)
    {
        updateUI();
    }

    public void updateUI()
    {

        DBConnector mDBConnector = new DBConnector();
        int count = mDBConnector.Count() - 1;
        DataTable dataTable = mDBConnector.Select("SELECT * FROM devices");


        int x = 12;
        int y = 65;
        for (int i = 0; i <= count && i < 25; i++)
        {

            Button btnAdd = new Button();
            btnAdd.Text = dataTable.Rows[i]["deviceDescription"].ToString();
            btnAdd.Location = new Point(x, y);
            btnAdd.Tag = i;
            btnAdd.Name = "btn" + i.ToString();
            btnAdd.BackColor = Color.Green;
            var temp = i + 1;
            this.Controls.Add(btnAdd);

            this.Controls[btnAdd.Name].MouseClick += (sender, e) =>
            {
                int index = temp;
                generalMethods.generatePopup(sender, e, index);
            };

            string address = dataTable.Rows[i]["deviceIP"].ToString();
            int port = int.Parse(dataTable.Rows[i]["devicePort"].ToString());


            ThreadStart workerThread = delegate { start(address, port, i); };
            new Thread(workerThread).Start();

            x = (x + 75);
            if (i != 0 && (i % 5) == 0)
            {
                x = 12;
                y = y + 30;
            }
            if (i == 25)
            {
                Button btnPreviousPage = new Button();
                btnPreviousPage.Text = "<";
                btnPreviousPage.Location = new Point(150, 350);
                btnPreviousPage.Tag = "left";
                this.Controls.Add(btnPreviousPage);

                Button btnNextPage = new Button();
                btnNextPage.Text = ">";
                btnNextPage.Location = new Point(225, 350);
                btnNextPage.Tag = "right";
                this.Controls.Add(btnNextPage);
            }
        }
    }
    public void start(string address, int port, int i)
    {
        if (timer == null)
        {
            timer = new System.Timers.Timer(1000);


            timer.Elapsed += delegate(object sender, ElapsedEventArgs e) { timerElapsed(sender, e, address, port, i); };
        }
        timer.Enabled = true;
        // MessageBox.Show("Thread " + i + " Started.");
    }
    public void timerElapsed(object sender, ElapsedEventArgs e, string address, int port, int i)
    {
        bfdeviceimp.newconnect(address, port, i);
    }

然后最后我的设备类:

   class bf2300deviceimp : IdeviceInterface
{
   public void newconnect(string address, int port, int buttonNumber)
    {
        //send data
        byte[] bData = new byte[71];
        bData[0] = 240;
        bData[1] = 240;
        bData[2] = 0;
        bData[3] = 1;
        bData[68] = 240;
        bData[69] = 240;
        bData[70] = this.newCalculateCheckSum(bData);


        try
        {
            byte[] result = this.newSendCommandResult(address, port, bData, 72);

           //form1.setAlarmColour(result, buttonNumber);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }

    }

你建议我把statechanged处理程序放在哪里?

3 个答案:

答案 0 :(得分:1)

您应该使用基于事件的方法来解决此问题,就像在表单之间传递信息时一样。您的每个设备都应该有一个自定义事件,它定义了在该设备状态发生变化时触发的事件。该事件应该只在接口中定义,以便与该设备进行交互。表单在创建各种设备类时应该订阅事件,在事件处理程序中它应该适当地更新按钮/文本框。

如果您不习惯这种编程方式,这可能是一个很好的选择。请随意在评论中询问更多细节,我可以详细说明为什么我按照我的方式或实际做的方式做了一些事情。

public Form1()
{
    InitializeComponent();

    //not sure if this is on initialization or in a button click event handler or wherever.
    IDevice device = new SomeDevice();
    device.StatusChanged += GetHandlerForDevice(1);
    device.DoStuff();

    IDevice device2 = new SomeDevice(); //could be another class that implements IDevice
    device.StatusChanged += GetHandlerForDevice(2);
    device.DoStuff();
}

/// <summary>
/// The handlers for device status changed only vary based on the button number for each one.
/// This method takes a button number and returns an event handler that uses that button number.
/// </summary>
/// <param name="buttonNumber"></param>
/// <returns></returns>
private EventHandler<StatusChangedEventArgs> GetHandlerForDevice(int buttonNumber)
{
    //use currying so that the event handler which doesn't have an appropriate signature
    //can be attached to the status changed event.
    return (sender, args) => device_StatusChanged(sender, args, buttonNumber);
}

private void device_StatusChanged(object sender, StatusChangedEventArgs args, int buttonNumber)
{
    this.Invoke((MethodInvoker)delegate
    {
        txtOutput1.Text = (args.CurrentStatus == IDevice.Status.Green ? "HIGH" : "LOW"); // runs on UI thread

        if (args.CurrentStatus == IDevice.Status.Green)
        {
            this.Controls["btn" + buttonNumber].BackColor = Color.Green;
        }
        else
        {
            this.Controls["btn" + buttonNumber].BackColor = Color.Red;
        }


    });
}



public interface IDevice
{
    event EventHandler<StatusChangedEventArgs> StatusChanged;
    Status CurrentStatus { get; }


    public enum Status
    {
        Green,
        Red
    }

    void DoStuff();
    // rest of interface ...
}

public class StatusChangedEventArgs : EventArgs
{
    public IDevice.Status CurrentStatus { get; set; }
    //can add additional info to pass from an IDevice to a form if needed.
}

public class SomeDevice : IDevice
{
    public event EventHandler<StatusChangedEventArgs> StatusChanged;

    private IDevice.Status _currentStatus;
    /// <summary>
    /// Gets the current status of the device this object represents.
    /// When set (privately) it fires the StatusChanged event.
    /// </summary>
    public IDevice.Status CurrentStatus
    {
        get { return _currentStatus; }
        private set
        {
            _currentStatus = value;
            if (StatusChanged != null)
            {
                StatusChangedEventArgs args = new StatusChangedEventArgs();
                args.CurrentStatus = value;
                StatusChanged(this, args);
            }
        }
    }


    public void DoStuff()
    {
        //... do stuff
        CurrentStatus = IDevice.Status.Green; //will fire status changed event
    }
}

答案 1 :(得分:0)

将所有逻辑移到表单中的方法中并在外部使用它。

答案 2 :(得分:0)

在表单中创建一个属性

public SynchronizationContext SyncContext { get; set;}

在表单构造函数中添加:

this.SyncContext = WindowsFormsSynchronizationContext.Current;

制作界面:

public Interface IChangeClient 
{
    void Process(<some_type> result); // place your logic here
}

(或类似的东西)并在表单中实现它以更改按钮和文本。

扩展您的原始界面(可能作为参数)SynchronizationContextIBackgroundChangeClient

而且你的代码看起来像这样:

if (result != null)
{
    oSyncContext.Post(new System.Threading.SendOrPostCallback(
            delegate(object state)
            {
                IBackgroundChangeClient client = (state as object[])[0] as IBackgroundChangeClient
                //i dont konw the type of this
                var innerResult= (state as object[])[1];
                client.Process(innerResult);
            }), new object[] { oBackgroundChangeClient, result[4]});

}