init
This commit is contained in:
46
Common/DTS.Common.Utilities/KeywordAlert.cs
Normal file
46
Common/DTS.Common.Utilities/KeywordAlert.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace DTS.Common.Utilities
|
||||
{
|
||||
public sealed class KeywordAlert
|
||||
{
|
||||
public static KeywordAlert Instance { get; } = new KeywordAlert();
|
||||
|
||||
private List<string> alerts = null;
|
||||
|
||||
private const string KeywordFile = "Keywords.txt";
|
||||
private KeywordAlert()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (System.IO.File.Exists(KeywordFile))
|
||||
{
|
||||
alerts = new List<string>(System.IO.File.ReadAllLines(KeywordFile));
|
||||
}
|
||||
}
|
||||
catch (System.Exception) { }
|
||||
}
|
||||
|
||||
public void ProcessMsg(string msg)
|
||||
{
|
||||
if (null != alerts)
|
||||
{
|
||||
System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(DoWork), msg);
|
||||
}
|
||||
}
|
||||
private void DoWork(object o)
|
||||
{
|
||||
var msg = o as string;
|
||||
var keys = alerts.ToArray();//avoid accessing the list since many threads may be operating simultaneously
|
||||
|
||||
var matches = from k in keys.AsParallel() where msg.Contains(k) select k;
|
||||
if (matches.Any())
|
||||
{
|
||||
var foundKeys = string.Join(", ", matches.ToArray());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user