我有一个相当简单的if
语句,每次通过语句时都会设置几个变量。我遇到一个问题,当它到达AgentsAvailable == 0
或点击else
语句时,它无法恢复到以前的任何语句。有人可以提供一些方法来帮助它,以便每当其中一个陈述为真且不会卡住时,语句就会正确循环。
我的if
声明:
if (e.CmsData.Skill.AgentsAvailable > 0)
{
Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)(() => {
callsWaitingData.Text = e.CmsData.Skill.AgentsAvailable.ToString();
callsWaitingData.Foreground = new SolidColorBrush(Colors.Green);
callsWaitingText.Text = "Available";
longestWaitingData.Text = max.ToString();
longestWaitingText.Text = "Available";
callimgae.Source = new BitmapImage(new Uri("pack://application:,,,/ScoreBoardClientTest;component/images/circle_green.png", UriKind.Absolute));
}));
}
else if (e.CmsData.Skill.InQueueInRing > 5)
{
Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)(() => {
callsWaitingData.Text = e.CmsData.Skill.InQueueInRing.ToString();
callsWaitingData.Foreground = new SolidColorBrush(Colors.Red);
callsWaitingText.Text = "Waiting";
longestWaitingData.Text = e.CmsData.Skill.OldestCall.ToString().Substring(3);
longestWaitingText.Text = "Waiting";
timer = new System.Threading.Timer(OnTimerEllapsed, new object(), 0, 2000);
}));
}
else if (e.CmsData.Skill.InQueueInRing > 0)
{
Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)(() => {
callsWaitingData.Text = e.CmsData.Skill.InQueueInRing.ToString();
callsWaitingData.Foreground = new SolidColorBrush(Colors.Red);
callsWaitingText.Text = "Waiting";
longestWaitingData.Text = e.CmsData.Skill.OldestCall.ToString().Substring(3);
longestWaitingText.Text = "Waiting";
callimgae.Source = new BitmapImage(new Uri("pack://application:,,,/ScoreBoardClientTest;component/images/circle_red.png", UriKind.Absolute));
}));
}
else if (e.CmsData.Skill.AgentsAvailable == 0)
{
Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action) (() =>
{
callsWaitingData.Text = e.CmsData.Skill.AgentsAvailable.ToString();
callsWaitingData.Foreground = new SolidColorBrush(Colors.Yellow);
callsWaitingText.Text = "Available";
longestWaitingData.Text = max.ToString();
longestWaitingText.Text = "Available";
callimgae.Source =
new BitmapImage(
new Uri("pack://application:,,,/ScoreBoardClientTest;component/images/circle_yellow.png",
UriKind.Absolute));
}));
}
else
{
Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action) (() =>
{
callsWaitingData.Text = e.CmsData.Skill.AgentsAvailable.ToString();
callsWaitingData.Foreground = new SolidColorBrush(Colors.Yellow);
callsWaitingText.Text = "Available";
longestWaitingData.Text = max.ToString();
longestWaitingText.Text = "Available";
callimgae.Source =
new BitmapImage(
new Uri("pack://application:,,,/ScoreBoardClientTest;component/images/circle_yellow.png",
UriKind.Absolute));
}));
}
答案 0 :(得分:1)
似乎与if-else
陈述的目的有些混淆
它们不是循环。它们的目的是为执行特定代码提供条件。满足条件的第一种情况(返回true
)将被执行,而其他所有条件都不会被执行。如果您定义else
案例,则在以上所有案例都失败时执行。
关于此的官方文档可以在here找到。
总结:每次调用上述if-else
语句时,只会执行一个个案。回到上述情况并非如此。
如果您对您要做的事情给出更详细的解释,我可以提供进一步的帮助。
此外,我在你的案例中看到了很多重复的代码(例如,最后两个似乎是相同的,使第一个过时)。你应该尽量减少这个。
答案 1 :(得分:1)
这与子句的排序有关,并且评估为true
的第一个条件是将执行的唯一块。如果您希望某些先前的条件适用,无论是否AgentsAvailable == 0
// *** Agents available logic ***
if (e.CmsData.Skill.AgentsAvailable > 0)
{
// Stuff that needs to happen if Agents are available
}
else
{
// Stuff that needs to happen if they're not available
}
// *** Queue ring logic *** (happens regardless of agents available
if (e.CmsData.Skill.InQueueInRing > 5)
{
}
else if (e.CmsData.Skill.InQueueInRing > 0)
{
}
else
{
}
如果只有当代理可用且InQueueInRing
为> 5
时才需要发生,那么您需要在其中一个if
子句中添加内容,例如:
if (e.CmsData.Skill.InQueueInRing > 5)
{
if (e.CmsData.Skill.AgentsAvailable > 0)
{
// Do stuff here that requires agents available
}
else
{
// If there's some counterpart logic for when
// no agents are available, do it here
}
// Do other stuff here that doesn't care about agents one way or the
// other (i.e. stuff that should ALWAYS happen when InQueueInRing > 5
}