47 lines
1.3 KiB
C#
47 lines
1.3 KiB
C#
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());
|
|
}
|
|
}
|
|
}
|
|
}
|