我試圖運行在一個單獨的線程的方法,但是當我嘗試初始化新的線程我得到這個錯誤:線程的方法
A field initializer cannot reference the non-static field, method, or property Form1.update()
更新方法:
public void update()
{
XmlDocument doc = new XmlDocument();
status s = new status();
doc.LoadXml(s.getStatus("12345"));
char[] xmlChar = { 'z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h' };
int[] lightStatus = new int[9];
int[] doorStatus = new int[5];
int[] fanStatus = new int[5];
int[] windowStatus = new int[5];
for (int i = 1; i < 9; i++)
{
XmlNode lights = doc.SelectSingleNode("All/Lights/status/" + xmlChar[i] + "/text()");
lightStatus[i] = Convert.ToInt16(lights.Value);
}
for (int i = 1; i < 5; i++)
{
XmlNode fans = doc.SelectSingleNode("All/Fans/status/" + xmlChar[i] + "/text()");
XmlNode doors = doc.SelectSingleNode("All/Doors/status/" + xmlChar[i] + "/text()");
XmlNode windows = doc.SelectSingleNode("All/Windows/status/" + xmlChar[i] + "/text()");
fanStatus[i] = Convert.ToInt16(fans.Value);
doorStatus[i] = Convert.ToInt16(doors.Value);
windowStatus[i] = Convert.ToInt16(windows.Value);
}
u1.update(lightStatus);
u2.update(fanStatus);
}
我在初始化得到一個錯誤:
System.Threading.Thread updateThread = new System.Threading.Thread(update);
http://stackoverflow.com/questions/12659551/threading-problems-in-c-sharp-a-field-initializer-cannot-reference-the-non-stat 這可能會幫助你。 – Nil23
或者這個:http://stackoverflow.com/questions/2642978/how-to-call-the-method-in-thread-with-aruguments-and-return-some-value –