【发布时间】:2011-11-04 15:04:49
【问题描述】:
我在 c# 程序中使用线程,但是线程运行的进程调用另一个函数,该函数具有锁定程序的无限循环,也就是说,如果我单击另一个选项Windows 窗体(例如:关闭等)将不再响应。
这个循环是必要的,目前还不能改变。
有没有一种方法可以让我像“背景”一样运行这个循环并且仍然使用程序中的其他选项,这是:循环不会阻塞进程(我不喜欢在线程内部使用线程!)。
Main Program
|
-------Thread(Function)
|
--------In the function ANOTHER
function is called with
an infinite loop inside
(this loop is NOT part of the
Thread function directly)
编辑:添加一些示例代码:
//Here I call the threads
private void userTest_Click(object sender, EventArgs e)
{
for (int i = 0; i < numberOfDevices; i++)
{
try
{
Thread t = new Thread(unused => device(i, sender, e));
t.IsBackground = true;
t.Start();
}
catch (ThreadStateException f)
{
MessageBox.Show("ERROR:" + f); // Display text of exception
}
}
}
线程函数:
//This infinite loop is useless, so it could be deleted. This is not
// the loop I´m talking about
public void device(object i, object s, object f)
{
while (true)
{
if (!killEm)
{
int j = (int)i;
EventArgs g = (EventArgs)f;
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]);
userTest.ShowDialog();
}
else
{
userTest.Dispose();
MessageBox.Show("Why don´t u kill it!!?");
break;
}
}
}
在 userTest.ShowDialog() 函数中是我所说的无限循环
EDIT 这是 userTest.ShowDialog()
中调用的函数的一部分private void user_Click(object sender, EventArgs e) {
//THIS IS THE LOOP I´M TALKING!
while (true) {
Keep listening for an user put his finger in the device
...
Do things with that finger template
...
}
}
谢谢。
【问题讨论】:
-
请向我们展示您的代码;如果您正确使用线程,则不会发生这种情况。
-
你需要异步运行线程。
-
那么让我直截了当 - 你是在一个新线程上打开一个新窗口并在那个新窗口内运行一个无限循环?
-
@Toomai :是的,这正是我正在做的事情
-
@Kani 错了,错了,错了。进程包含线程。线程不执行进程。 (虽然在线程中运行的代码可以生成另一个进程...这肯定不是这里发生的...)
标签: c# process infinite-loop