forked from gitextensions/gitextensions
/
ProcessOutputTimer.cs
75 lines (65 loc) · 1.76 KB
/
ProcessOutputTimer.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
using System.Text;
using System.Timers;
namespace GitUI
{
public class ProcessOutputTimer
{
public delegate void DoOutputCallback(string text);
private Timer _timer;
private StringBuilder textToAdd = new StringBuilder();
private DoOutputCallback doOutput;
public ProcessOutputTimer(DoOutputCallback doOutput)
{
this.doOutput = doOutput;
_timer = new Timer();
_timer.Elapsed += _timer_Elapsed;
}
public void Start(int interval)
{
_timer.Stop();
_timer.Interval = interval;
_timer.Enabled = true;
}
public void Start()
{
Start(600);
}
public void Stop(bool flush)
{
_timer.Stop();
if (flush)
_timer_Elapsed(null, null);
}
public void Append(string text)
{
lock(textToAdd)
{
textToAdd.Append(text);
}
}
private void _timer_Elapsed(object sender, ElapsedEventArgs e)
{
lock (textToAdd)
{
if (textToAdd.Length > 0 && doOutput != null)
doOutput(textToAdd.ToString());
Clear();
}
}
public void Clear()
{
lock (textToAdd)
{
textToAdd.Remove(0, textToAdd.Length);
}
}
public void Dispose()
{
Stop(false);
//clear will lock, to prevent outputting to disposed object
Clear();
doOutput = null;
_timer.Dispose();
}
}
}