如何在另一個線程中的窗口中的列表框中添加一個條目。 我的問題是,我必須線程。 一個工作線程,它執行所有實際工作,併爲要顯示狀態信息的窗口執行一個線程。 我試圖在構造函數中傳遞表單窗口作爲參數,但是當我調用它時,我得到一個異常。在另一個線程中訪問表單窗口
下面是我的一些代碼來說明這個問題:
public partial class Mainform : Form
{
private string db = String.Empty;
private string password = String.Empty;
private string sqlinifile = String.Empty;
DatabaseListener dbListener;
StatusWindow statusWindow;
public Mainform()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();
//
// TODO: Add constructor code after the InitializeComponent() call.
//
db = "EWAG";
password = "secret";
sqlinifile = "C:\\Programme\\Unify\\Team Developer 5.2\\sql.ini";
textBoxDB.Text = db;
textBoxPwd.Text = password;
textBoxSqlIni.Text = sqlinifile;
statusWindow = new StatusWindow();
dbListener = new DatabaseListener(statusWindow);
}
public void threadStarter()
{
this.dbListener.startSynchronizing(5);
}
void Button1Click(object sender, EventArgs e)
{
this.db = textBoxDB.Text;
this.password = textBoxPwd.Text;
this.sqlinifile = textBoxSqlIni.Text;
if (dbListener.connectToDatabase(db,"sysadm", password, sqlinifile))
{
this.Hide();
statusWindow.Show();
Thread synchronizer = new Thread(new ThreadStart(threadStarter));
synchronizer.Start();
}
}
}