【发布时间】:2011-11-03 19:41:04
【问题描述】:
我在 C# 中使用 -first time- threads -with multiparameters-。我遇到的问题是:
如果我使用这些功能 - 我会发布 - 没有威胁,它将遵循程序的自然逻辑。不过,我需要使用线程同时调用函数的多个实例。但是当我使用线程时,程序的执行会停止,尽管我知道线程正在正确调用函数,因为我看到了它 - 它会弹出一个 Windows形式!它会打印一些我输入的消息-
我不知道为什么,是比较简单的功能。我认为它可能与 EventArgs 有关,但不确定-我也将它作为参数传递!-。
让我们看看线程和函数:
//This is the function that I call in the threads
public void device(object i, object s, object f)
{
int j = (int)i;
EventArgs g = (EventArgs)f;
//Connect the devices
BSSDK.BS_SetDeviceID(m_ConnectedDeviceHandle[j],
m_ConnectedDeviceID[j], m_ConnectedDeviceType[j]);
UserManagement userTest = new UserManagement();
userTest.SetDevice(m_ConnectedDeviceHandle[j],
m_ConnectedDeviceID[j], m_ConnectedDeviceType[j]);
//Open a windows form and show the interface!
userTest.Show();
}
// The function with the threads
private void userTest_Click(object sender, EventArgs e)
{
...
//This loop if for testing, the actual value will be other.
for (int i = 0; i < 2; i++)
{
//Notice that I´m sending 3 parameters!
Thread t = new Thread(unused => device(i, sender, e));
try
{
t.Start();
}
catch (ThreadStateException f)
{
MessageBox.Show("ERROR:" + f);
}
}
}
谢谢。
【问题讨论】:
标签: c# multithreading