如何在此 C# 示例中避免 while(true)
How to avoid while(true) in this C# example
我正在寻找一种方法来用更高效的东西(例如事件驱动代码)替换此 while 循环。
While(true)\ must constantly monitor units
{
if(units.count< 10)
{
\ launch units
}
thread.sleep(100); \ to avoid 100% CPU usage
}
单元可以在其他线程中删除,它们在线程安全的concurrentDictionary 中。
如果您提出更好地实施此代码的建议,我将不胜感激。
谢谢
您可以使用计时器摆脱 while
和 thread.sleep
,同时摆脱主线程上的阻塞:
private static void OnTimedEvent(object source, System.Timers.ElapsedEventArgs e)
{
if(unitsCount < 10)
{
//launch Units
}
}
public static void Main (string[] args)
{
System.Timers.Timer aTimer = new System.Timers.Timer();
aTimer.Elapsed+=new System.Timers.ElapsedEventHandler(OnTimedEvent);
aTimer.Interval=100;
aTimer.Enabled=true;
}
工作示例:fiddle
Related Answer
这是我使用事件创建的示例,它显然不完整,但您应该能够向其中添加您需要的内容以按照您的需要获得它。
当从字典中删除键时检查字典的计数,然后如果计数小于指定的数字则触发事件。
注意:我不知道这是否是线程安全的,我不熟悉使用线程,我希望 ConcurrentDictionary 能解决这个问题。
public static partial class Program
{
static void Main(string[] args)
{
DictionaryCount<int, string> dict = new DictionaryCount<int, string>();
dict.CountLessThan += dict_TryRemove;
dict.CountToFireOn = 1;
dict.TryAdd(1, "hello");
dict.TryAdd(2, "world");
dict.TryAdd(3, "!");
string outValue;
dict.TryRemove(2, out outValue);
dict.TryRemove(1, out outValue);
Console.ReadKey(true);
}
private static void dict_TryRemove(object sender, CountEventArgs e)
{
DictionaryCount<int, string> dict = sender as DictionaryCount<int, string>;
Console.WriteLine(dict.Count);
Console.WriteLine("Count less than 2!");
}
public class DictionaryCount<TKey, TValue> : ConcurrentDictionary<TKey, TValue>
{
public int CountToFireOn { get; set; }
public DictionaryCount() : base() { }
public delegate void CountEventHandler(object sender, CountEventArgs e);
public event CountEventHandler CountLessThan;
public new bool TryRemove(TKey key, out TValue value)
{
bool retVal = base.TryRemove(key, out value);
if (this.Count <= CountToFireOn)
{
CountEventArgs args = new CountEventArgs(this.Count);
CountLessThan(this, args);
}
return retVal;
}
}
public class CountEventArgs
{
public int Count { get; set; }
public CountEventArgs(int count)
{
this.Count = count;
}
}
}
我正在寻找一种方法来用更高效的东西(例如事件驱动代码)替换此 while 循环。
While(true)\ must constantly monitor units
{
if(units.count< 10)
{
\ launch units
}
thread.sleep(100); \ to avoid 100% CPU usage
}
单元可以在其他线程中删除,它们在线程安全的concurrentDictionary 中。
如果您提出更好地实施此代码的建议,我将不胜感激。
谢谢
您可以使用计时器摆脱 while
和 thread.sleep
,同时摆脱主线程上的阻塞:
private static void OnTimedEvent(object source, System.Timers.ElapsedEventArgs e)
{
if(unitsCount < 10)
{
//launch Units
}
}
public static void Main (string[] args)
{
System.Timers.Timer aTimer = new System.Timers.Timer();
aTimer.Elapsed+=new System.Timers.ElapsedEventHandler(OnTimedEvent);
aTimer.Interval=100;
aTimer.Enabled=true;
}
工作示例:fiddle
Related Answer
这是我使用事件创建的示例,它显然不完整,但您应该能够向其中添加您需要的内容以按照您的需要获得它。
当从字典中删除键时检查字典的计数,然后如果计数小于指定的数字则触发事件。
注意:我不知道这是否是线程安全的,我不熟悉使用线程,我希望 ConcurrentDictionary 能解决这个问题。
public static partial class Program
{
static void Main(string[] args)
{
DictionaryCount<int, string> dict = new DictionaryCount<int, string>();
dict.CountLessThan += dict_TryRemove;
dict.CountToFireOn = 1;
dict.TryAdd(1, "hello");
dict.TryAdd(2, "world");
dict.TryAdd(3, "!");
string outValue;
dict.TryRemove(2, out outValue);
dict.TryRemove(1, out outValue);
Console.ReadKey(true);
}
private static void dict_TryRemove(object sender, CountEventArgs e)
{
DictionaryCount<int, string> dict = sender as DictionaryCount<int, string>;
Console.WriteLine(dict.Count);
Console.WriteLine("Count less than 2!");
}
public class DictionaryCount<TKey, TValue> : ConcurrentDictionary<TKey, TValue>
{
public int CountToFireOn { get; set; }
public DictionaryCount() : base() { }
public delegate void CountEventHandler(object sender, CountEventArgs e);
public event CountEventHandler CountLessThan;
public new bool TryRemove(TKey key, out TValue value)
{
bool retVal = base.TryRemove(key, out value);
if (this.Count <= CountToFireOn)
{
CountEventArgs args = new CountEventArgs(this.Count);
CountLessThan(this, args);
}
return retVal;
}
}
public class CountEventArgs
{
public int Count { get; set; }
public CountEventArgs(int count)
{
this.Count = count;
}
}
}