Skip to content

Commit 6598aeb

Browse files
author
Ahmad Noman Musleh
committed
Added time out to wait event handle objects of data manager
1 parent 4810dc5 commit 6598aeb

File tree

2 files changed

+30
-26
lines changed

2 files changed

+30
-26
lines changed

src/Alert/DataManager.cs

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using cAlgo.API.Alert.Models;
22
using cAlgo.API.Alert.UI.Models;
33
using LiteDB;
4+
using System;
45
using System.Collections.Generic;
56
using System.IO;
67
using System.Linq;
@@ -21,16 +22,17 @@ public static IEnumerable<AlertModel> GetAlerts()
2122

2223
using (var eventWaitHandle = GetWaitHandle())
2324
{
24-
eventWaitHandle.WaitOne();
25-
26-
using (LiteDatabase database = new LiteDatabase(GetConnectionString()))
25+
if (eventWaitHandle.WaitOne(TimeSpan.FromMinutes(1)))
2726
{
28-
var collection = database.GetCollection<AlertModel>();
27+
using (LiteDatabase database = new LiteDatabase(GetConnectionString()))
28+
{
29+
var collection = database.GetCollection<AlertModel>();
2930

30-
result = collection.FindAll().ToList();
31-
}
31+
result = collection.FindAll().ToList();
32+
}
3233

33-
eventWaitHandle.Set();
34+
eventWaitHandle.Set();
35+
}
3436
}
3537

3638
return result;
@@ -40,33 +42,35 @@ public static void AddAlerts(params AlertModel[] alerts)
4042
{
4143
using (var eventWaitHandle = GetWaitHandle())
4244
{
43-
eventWaitHandle.WaitOne();
44-
45-
using (LiteDatabase database = new LiteDatabase(GetConnectionString()))
45+
if (eventWaitHandle.WaitOne(TimeSpan.FromMinutes(1)))
4646
{
47-
var collection = database.GetCollection<AlertModel>();
47+
using (LiteDatabase database = new LiteDatabase(GetConnectionString()))
48+
{
49+
var collection = database.GetCollection<AlertModel>();
4850

49-
collection.InsertBulk(alerts);
50-
}
51+
collection.InsertBulk(alerts);
52+
}
5153

52-
eventWaitHandle.Set();
54+
eventWaitHandle.Set();
55+
}
5356
}
5457
}
5558

5659
public static void RemoveAlerts(params AlertModel[] alerts)
5760
{
5861
using (var eventWaitHandle = GetWaitHandle())
5962
{
60-
eventWaitHandle.WaitOne();
61-
62-
using (LiteDatabase database = new LiteDatabase(GetConnectionString()))
63+
if (eventWaitHandle.WaitOne(TimeSpan.FromMinutes(1)))
6364
{
64-
var collection = database.GetCollection<AlertModel>();
65+
using (LiteDatabase database = new LiteDatabase(GetConnectionString()))
66+
{
67+
var collection = database.GetCollection<AlertModel>();
6568

66-
collection.DeleteMany(iAlert => alerts.Contains(iAlert));
67-
}
69+
collection.DeleteMany(iAlert => alerts.Contains(iAlert));
70+
}
6871

69-
eventWaitHandle.Set();
72+
eventWaitHandle.Set();
73+
}
7074
}
7175
}
7276

test/UITestConsole/Program.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ private static void Main(string[] args)
1010
{
1111
var notifications = new Notifications();
1212

13-
notifications.ShowPopup();
13+
//notifications.ShowPopup();
1414

1515
//notifications.ShowPopup("Hour", "EURUSD", "Trend Line", "UITestConsole", 1.23452, "No comment 2", DateTimeOffset.Now);
1616

1717
//TriggerAlerts(50, notifications);
1818

19-
//TestMultiThread();
19+
TestMultiThread(10);
2020
}
2121

2222
private static void TriggerAlerts(int number, Notifications notifications)
@@ -42,13 +42,13 @@ private static void TriggerAlerts(int number, Notifications notifications)
4242
TriggeredBy = "UITestConsole"
4343
};
4444

45-
notifications.TriggerAlert(alert);
45+
notifications.ShowPopup(alert);
4646
}
4747
}
4848

49-
private static void TestMultiThread()
49+
private static void TestMultiThread(int threadNumber)
5050
{
51-
for (int i = 0; i < 20; i++)
51+
for (int i = 1; i <= threadNumber; i++)
5252
{
5353
int number = i;
5454

0 commit comments

Comments
 (0)