C# 从单独的线程/类设置控件属性

C# 从单独的线程/类设置控件属性,c#,multithreading,class,listbox,C#,Multithreading,Class,Listbox,我已经搜索过了,但找不到一个解决方案可以帮助我从单独类中运行的线程中获取文本,返回到创建线程的表单上的列表框 基本上,我有一个类,它包含一个“测试”,它在它自己的线程中从测试窗口调用。我想做的是在主窗体的列表框中添加文本,让用户知道测试的进展。我在Invoke上找到的所有示例都说明了如何在同一个类中执行此操作 我从哪里开始线程: PermeabilityTest Run_Test = new PermeabilityTest(); public Thread WorkerThre

我已经搜索过了,但找不到一个解决方案可以帮助我从单独类中运行的线程中获取文本,返回到创建线程的表单上的列表框

基本上,我有一个类,它包含一个“测试”,它在它自己的线程中从测试窗口调用。我想做的是在主窗体的列表框中添加文本,让用户知道测试的进展。我在Invoke上找到的所有示例都说明了如何在同一个类中执行此操作

我从哪里开始线程:

    PermeabilityTest Run_Test = new PermeabilityTest();
    public Thread WorkerThread;

    private void button2_Click(object sender, EventArgs e)
    {
        //enable timer for test duration display
        timer1.Enabled = true;

        //create and start new thread.
        WorkerThread = new Thread(Run_Test.RunTest);
        WorkerThread.Start();
    }
这是我的类,它实际完成了这项工作,我需要将文本以单独的形式从中返回到列表框

public class PermeabilityTest
{
    //volatile alerts the compiler that it will be used across threads.
    private volatile bool aborted;

    public void RequestStop()
    {
        //handle saving data file here as well.
        aborted = true;
    }

    public void RunTest()
    {
        //reference the comms class so we can communicate with the machine
        PMI_Software.COMMS COM = new COMMS();

        //some test stuffs here
        int x = 0;
        while( x < 100 && !aborted)
        {
            System.Diagnostics.Debug.Write("Well here it is, running it's own thread." + Environment.NewLine);
            COM.Pause(1);
        }  
    }        
}
公共类渗透性测试
{
//volatile警告编译器它将跨线程使用。
私人易变bool流产;
公共void RequestStop()
{
//在这里处理保存数据文件。
中止=真;
}
公共无效运行测试()
{
//引用comms类,以便我们可以与机器通信
PMI_Software.COMMS COM=新通信();
//这里有一些测试材料
int x=0;
而(x<100&!中止)
{
System.Diagnostics.Debug.Write(“好了,它在这里,运行它自己的线程。”+Environment.NewLine);
COM.Pause(1);
}  
}        
}
如果有人能帮助我理解如何将一些文本返回到具有启动线程按钮的表单的列表框中,我将不胜感激。

选项1:(首选)渗透性测试中添加一个事件,并在主表单中注册该事件。 然后从主窗体中修改列表框的内容

例如:

您的主要表格:

    PermeabilityTest Run_Test = new PermeabilityTest();
    public Thread WorkerThread;

    public form1()
    {
        // Register on the Progress event
        Run_Test.Progress += Run_Test_Progress;
    }

    void Run_Test_Progress(string message)
    {
    if(listBox.InvokeRequired)
        {
            // Running on a different thread than the one created the control
            Delegate d = new ProgressEventHandler(Run_Test_Progress);
            listBox.Invoke(d, message);
        }
        else
        {
            // Running on the same thread which created the control
            listBox.Items.Add(message);
        }
    }

    private void button2_Click(object sender, EventArgs e)
    {
        //enable timer for test duration display
        timer1.Enabled = true;

        //create and start new thread.
        WorkerThread = new Thread(Run_Test.RunTest);
        WorkerThread.Start();
    }
新代表:

    public delegate void ProgressEventHandler(string message);
改良渗透性试验等级:

    public class PermeabilityTest
    {
        //volatile alerts the compiler that it will be used across threads.
        private volatile bool aborted;
        public event ProgressEventHandler Progress;

        public void RequestStop()
        {
            //handle saving data file here as well.
            aborted = true;
        }

        public void RunTest()
        {
            //reference the comms class so we can communicate with the machine
            PMI_Software.COMMS COM = new COMMS();

            //some test stuffs here
            int x = 0;
            while (x < 100 && !aborted)
            {
                // Report on progress
                if(Progress != null)
                {
                    Progress("This message will appear in ListBox");
                }
                System.Diagnostics.Debug.Write("Well here it is, running it's own thread." + Environment.NewLine);
                COM.Pause(1);
            }
        }
    }
公共类渗透性测试
{
//volatile警告编译器它将跨线程使用。
私人易变bool流产;
公共事件进程EventHandler进程;
公共void RequestStop()
{
//在这里处理保存数据文件。
中止=真;
}
公共无效运行测试()
{
//引用comms类,以便我们可以与机器通信
PMI_Software.COMMS COM=新通信();
//这里有一些测试材料
int x=0;
而(x<100&!中止)
{
//进展报告
如果(进度!=null)
{
进度(“此消息将显示在列表框中”);
}
System.Diagnostics.Debug.Write(“好了,它在这里,运行它自己的线程。”+Environment.NewLine);
COM.Pause(1);
}
}
}
选项2: 您可以将渗透性测试作为主窗体的内部类,并允许它访问主窗体的私有成员。 然后,您需要将主窗体的引用传递给渗透性测试的构造函数,并将其保留为成员

选项3: 将列表框传递给渗透性测试的构造函数

不要忘记在控件上使用Invoke,因为您是从不同的线程运行的