C# 从运行脚本

C# 从运行脚本,c#,backgroundworker,C#,Backgroundworker,我有一个应用程序,它在GUI的主窗口中包含一个富文本框。我的主GUI代码是我的“main.cs”文件。我在“TestScripts.cs”文件中编写了一个测试脚本。我的主GUI上还有一个BackgroundWorker 我的问题是如何在“Main.cs”代码的BackgroundWorker中执行测试脚本?此代码设置为更新主GUI上的富文本框 我猜想会是这样的: private void backgroundWorker_DoWork(object sender, DoWorkEventArgs

我有一个应用程序,它在GUI的主窗口中包含一个富文本框。我的主GUI代码是我的“main.cs”文件。我在“TestScripts.cs”文件中编写了一个测试脚本。我的主GUI上还有一个
BackgroundWorker

我的问题是如何在“Main.cs”代码的
BackgroundWorker
中执行测试脚本?此代码设置为更新主GUI上的富文本框

我猜想会是这样的:

private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
   TestScripts.Test1();  // Find the specific script in my "TestScripts" code and execute in the background worker
}
richTextBox1.Invoke(new Action(() => { richTextBox1.Text += "New Text Here From Worker!"; }));
然而,我还没有弄清楚如何在我的背景代码中访问特定的测试脚本。换句话说,我可以访问
TestScripts
,但不能访问
TestScripts.Test1()


任何帮助都将不胜感激

如果我可以建议的话,你真的不需要辅助脚本;我觉得有点不需要了。相反,你可以这样做:

private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
   TestScripts.Test1();  // Find the specific script in my "TestScripts" code and execute in the background worker
}
richTextBox1.Invoke(new Action(() => { richTextBox1.Text += "New Text Here From Worker!"; }));
如果你想这样称呼它,你甚至可以把它放在一个空白处:

public void UpdateText(string text){
richTextBox1.Invoke(new Action(() => { richTextBox1.Text += text; }));
}

那就叫它

UpdateText("Hey New text!");

如果这对你没有帮助,我很抱歉,不过我想我会建议你的。祝你好运

请发布TestScripts.cs的代码,因为问题很可能就在那里;您所拥有的几乎肯定不是“脚本”,我们需要了解
TestScripts
的定义及其
Test1()
方法。您可能需要实例化它的一个实例