我想在另一個線程中運行自己的類,但是如果我這樣做,則無法使用我的例如EventHandler
內的標籤,我該如何避免這種情況?在單獨的線程上運行自己的類
這是我的代碼的外觀:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Ts3_Movearound
{
public partial class Form1 : Form
{
TS3_Connector conn = new TS3_Connector();
Thread workerThread = null;
public Form1()
{
InitializeComponent();
conn.runningHandle += new EventHandler(started);
conn.stoppedHandle += new EventHandler(stopped);
}
private void button1_Click(object sender, EventArgs e)
{
//System.Threading.Thread connw = new System.Threading.Thread(conn);
workerThread = new Thread(conn.Main);
workerThread.Start();
}
public void started(Object sender, EventArgs e)
{
label1.Text = "Status: Running!";
}
public void stopped(Object sender, EventArgs e)
{
label1.Text = "Status: Stopped!";
}
}
}
這就是錯誤:
InvalidOperationExpetion in Line "label1.Text = "Status: Running!";"