我有两个类;一个叫做 Form1.cs,另一个叫做 NoteThreader.cs,这是两个类的内容:
Form1.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace HitMachine
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void updateText(string theText)
{
label1.Text = theText;
}
private void Form1_Load(object sender, EventArgs e)
{
XML.LoadMP3.loadFile(XML.XmlParser.ParseDocument("music"));
Threading.NoteThreader.createTimer();
}
}
}
NoteThreader.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace HitMachine.Threading
{
public class NoteThreader
{
public static Thread createTimer()
{
Thread t = new Thread(threadDeligation);
t.Start();
return t;
}
public static int time = 0;
static void threadDeligation()
{
for (; ; )
{
Form1 a = new Form1();
a.updateText(time);
time++;
}
}
}
}
代码运行没有任何异常或错误;但是它不会更新文本。我猜这是因为调用该方法的线程与更新文本的线程不同?
我试图调用它,但无法这样做。我在 threadDeligation 中运行了一个 MessageBox 并且有效。