使用类中未识别的语句 [英] Using statements not recognized in a class
问题描述
我正在尝试编写一个线程,将数据加载到winform中,而其他处理在后台继续。我已将线程类的定义放在项目的一个单独文件中 - Thread.cs。以下是该文件中的代码:
I am trying to write a thread that will load data into a winform while other processing continues in the background. I have put the definition of the thread class in a separate file in the project - Thread.cs. Here is the code in that file:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Diagnostics;
using System.Windows.Forms;
namespace Thread_Test_Sending_Data_to_Thread_Function
{
using System.Windows.Forms;
public class Thread1
{
private delegate void SetLabelTextDelegate(FormData InputClass);
public void FormWriteMethod(FormData InputClass)
{
if (this.InvokeRequired)
{
this.BeginInvoke(new SetLabelTextDelegate(FormWriteMethod), new object[] { InputClass });
return;
}
TestLabel1.Text = InputClass.Label1Data.ToString();
TestLabel2.Text = InputClass.Label2Data.ToString();
}
}
}
我知道InvokeRequired的定义在System.Windows中。形式。引用在主体中工作,但是当我尝试在Thread.cs中引用InvokeRequired或BeginInvoke时,即使我使用了System.Windows.Forms,也无法识别它。文件中没有任何使用语句被识别。
发生了什么事?
谢谢你任何帮助,
Andy Cruce
我尝试过:
我尝试在主体中引用Invoke方法,它工作正常。也尝试过在其他小类文件中使用相同结果的类似引用 - 即它看起来不像使用语句。
I know the definition of InvokeRequired is in System.Windows.Forms. The reference works in the main body but when I try to reference InvokeRequired or BeginInvoke in the Thread.cs it isn't recognized even though I have used System.Windows.Forms. None of the using statements in the file are recognized.
What is going on?
Thanks for any help,
Andy Cruce
What I have tried:
I have tried referencing the Invoke methods in the main body and it works fine. Have also tried a similar reference in other small class files with the same result - namely it doesn't look like the using statements are recognized.
推荐答案
问题在于你的Thread1
类没有InvokeRequired
属性和BeginInvoke
方法。你是在Thread1
类实例(this
)上调用它们的。要使这些Invoke函数可访问,此
必须是 表单的类。
最简单的解决方案是在基于表单的类中实现FormWriteMethod
。
The problem is that yourThread1
class doesn't have theInvokeRequired
property andBeginInvoke
method. You're calling these against theThread1
class instance (this
). For these Invoke functions to be accessible,this
must be the class that is the Form.
The simplest solution is to implement theFormWriteMethod
in your Form-based class.
从此处删除使用行:
Remove the "using" line from here:
namespace Thread_Test_Sending_Data_to_Thread_Function
{
using System.Windows.Forms;
public class Thread1
{
您已经拥有该类的using语句,您不再需要它,也不能在命名空间或类中使用它。
You already have a using statement for that class, you don't need it again, and cannot use it inside a namespace or class.
这篇关于使用类中未识别的语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!