我通过进一步的阅读自己解决了这个问题,我以为你应该引用线程而不是对象。现在它可以工作了。:)
原帖:
我研究了大部分类似的问题,并且看到了很多包含监视器、脉冲等答案。然而我无法使它工作。
我对C#还比较陌生,如果我使用线程有误,请原谅我。但我的问题如下:
我有4个线程,其中一个将三个不同表中的某一列的整数减一。然后是3个不同的线程,根据任何值是否达到零来执行动作。
我想做的是让线程倒计时只在某些值达到零时唤醒其他三个正确的线程。检查这个并不是问题,问题是如何唤醒线程。我目前有一个所有线程都同时运行的工作线程,但我想使用这个方法使其更有效率。
这是我使用的代码,为了简单起见,我只包括了一个,思路相同。这是我应该使用的方法吗?
这是我从阅读示例和谷歌搜索结果中得到的。
public class UpdateQueueSuggestion
{
public void startGame()
{
Thread Check = new Thread(new ThreadStart(checkUpdate));
Check.Start();
}
public void checkUpdate()
{
// (...) Some intialization of variables
// Create the thread used for when entries in the Queue table reaches 0.
Thread Build = new Thread(new ThreadStart(performBuildingUpdate));
Build.Start();
// Some sqlcommands.
while (true)
{
try
{
// Enter to synchronize, if not it yields
// Object synchronization method was called from an unsynchronized block of code.
Monitor.Enter(this);
connection.Open();
// Execute commands. Get COUNT(*) for results reaching 0 in tables and save int in i.
if (i > 0)
{
// Pulse the thread called Build, in order to wake it again.
Monitor.Pulse(Build);
}
}
finally
{
// Exit monitor.
Monitor.Exit(this);
connection.Close();
}
// This one is supposed to run each second, decrement and check for zeros.
// Works just fine if I just put everything in here without monitors,
// but as I said I want to split it up for efficiency.
Thread.Sleep(1000);
}
}
public void performBuildingUpdate()
{
// Some sqlcommands
while (true)
{
Monitor.Enter(this);
try
{
connection.Open();
// Execute commands.
}
finally
{
connection.Close();
Monitor.Wait(this);
Monitor.Exit(this);
}
}
}
非常感谢您的帮助,谢谢。
如果有人想知道,这是我们在学校C#课程中制作的浏览器游戏项目。这个程序用于计算游戏事件的大致时间。目前我们正在使用一个线程运行所有命令,并每10秒运行一次。如果能够使其每秒运行一次将会很好。