2016-12-14 41 views
2

我有一个每10秒运行一次的循环,它会执行一些操作。它所做的一件事就是当有消息发送给应用用户时,它会启用按钮。我想在按钮启用时向系统托盘发送通知,但由于显而易见的原因,我只希望在用户有未读广播时触发一次通知。在循环中触发通知一次

这里是我的代码:

private void EnableBroadcasts() 
    { 
     string query = "SELECT COUNT(*) FROM db.broadcasts WHERE broadcast_active = '1'"; 
     int count = StoredProcedures.CountRecordsT4(query); 
     StreamReader re = new StreamReader(@"C:\\Users\\" + Environment.UserName + @"\\appdata\\Local\\Cache\\broadcasts.cache"); 
     List<string> ReadBroadcastList = new List<string>(); 
     List<string> BcastID = BroadcastID(); 
     if (BroadcastCount != count) 
     { 
      string text = re.ReadToEnd(); 
      re.Close(); 
      string[] lines = text.Split('\r'); 
      foreach (string s in lines) 
      { 
       ReadBroadcastList.Add(s); 
      } 
      for (int t = 0; t < ReadBroadcastList.Count(); t++) 
      { 
       ReadBroadcastList[t] = ReadBroadcastList[t].Trim('\n'); 
      } 
      ReadBroadcastList.Remove(""); 
      BroadcastCount = ReadBroadcastList.Count(); 

     } 
     var test = BcastID.Except(ReadBroadcastList).ToList(); 
     int read = test.Count(); 
     if (count != 0) 
     { 
      btnWESBroadcast.Visible = true; 
     } 
     else 
     { 
      btnWESBroadcast.Visible = false; 
     } 

按钮使一次计数不为零。我有从数据库活动的广播ID列表,我也有一个缓存文件,记录该用户已读取的广播ID。

我正在寻找一种解决方案,只有当按钮处于活动状态并且存在用户尚未阅读的广播时才会运行通知。

回答

3

用简单的方式包装你的string广播:BroadcastMessage。添加bool IsRead标志。

马克IsRead = true和消息将被忽略以下逻辑。

// pseudo 
if (button.IsEnabled && ReadBroadcastList.Any(msg => !msg.IsRead)) { 
    NotifyTray(); 
} 

然后您可以稍后添加一个功能,以便用户标记消息Unread

如果您打算将此数据保存在数据库中,则消息和标志都可以存储在BroadcastMessage对象中。当用户读取消息并将对象标记为已读时,请使用更改更新数据库。

更新:基于澄清评论
添加bool IsNotified标志的BroadcastMessage通知和检查!msg.IsNotified!msg.IsRead代替

+0

当没有未读消息时,每次循环运行时都不会触发通知事件吗? –

+0

是的,这就是我如何理解你的问题所需的行为:“*通知只在按钮处于活动状态并且有用户没有阅读的广播时运行*” – IAbstract

+0

道歉,我正在寻找的是只有当按钮激活时,通知才会触发一次。 –