-
Notifications
You must be signed in to change notification settings - Fork 0
/
ApplicationQueue.cs
83 lines (78 loc) · 2.21 KB
/
ApplicationQueue.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace AppTools
{
class ApplicationQueue
{
private static ApplicationQueue instance = new ApplicationQueue();
public static ApplicationQueue GetInstance()
{
return instance;
}
private Queue<Form> formQueue = new Queue<Form>();
private List<Form> openForms = new List<Form>();
public void AddFormQueue(Form form)
{
lock (this)
{
formQueue.Enqueue(form);
}
}
public void RemoveOpenForm(object sender)
{
if (sender is Form)
{
Form form = (Form)sender;
form.Dispose();
lock (this)
{
this.openForms.Remove(form);
}
}
}
public static void InstanceAddFormQueue(Form form)
{
instance.AddFormQueue(form);
}
public static void RunApplicationQueue()
{
bool stillRunning = true;
while (stillRunning)
{
lock (instance)
{
if (instance.formQueue.Count > 0)
{
Form form = instance.formQueue.Dequeue();
form.FormClosed += FormClosed;
Thread th = new Thread(Run);
th.SetApartmentState(ApartmentState.STA);
th.Start(form);
instance.openForms.Add(form);
}
}
Thread.Sleep(10);
lock (instance)
{
stillRunning = instance.openForms.Count>0;
}
}
}
private static void FormClosed(object sender, FormClosedEventArgs e)
{
lock (instance)
{
instance.RemoveOpenForm(sender);
}
}
private static void Run(object form)
{
Application.Run((Form)form);
}
}
}