-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBackgroundTasks.cs
86 lines (78 loc) · 2.97 KB
/
BackgroundTasks.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
84
85
86
using System;
using System.ComponentModel;
using System.Windows.Forms;
namespace PictureViewer
{
public partial class Form1 : Form
{
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
// Fill picture-cache all the time. The maximum number of cached pictures before AND after the actual position is defined in "cachefiles".
while (true)
{
idle = true;
// Depending on the actual viewing-direction, make sure the _next_ picture will be processed first.
if (direction_down)
{
// first fill cache in actual direction
for (int i = actfile; i < (actfile + cachefiles) && i < (numfiles - 1); i++)
{
if (picdata[i] == null)
{
idle = false;
DecodePic(i);
}
}
// second fill cache in other direction
for (int i = actfile; i > (actfile - cachefiles) && i > 0; i--)
{
if (picdata[i] == null)
{
idle = false;
DecodePic(i);
}
}
}
// direction: up
else
{
// first fill cache in actual direction
for (int i = actfile; i > (actfile - cachefiles) && i > 0; i--)
{
if (picdata[i] == null)
{
idle = false;
DecodePic(i);
}
}
// second fill cache in other direction
for (int i = actfile; i < (actfile + cachefiles) && i < (numfiles - 1); i++)
{
if (picdata[i] == null)
{
idle = false;
DecodePic(i);
}
}
}
}
}
private void timer1_Tick(object sender, EventArgs e)
{
// Nothing to do? -> Garbage collection!
if (idle)
{
// always hold first & last pic in cache
DecodePic(0);
DecodePic(numfiles - 1);
// delete cache for pics out of cache-range (except first & last)
for (int i = 1; i < (numfiles - 1); i++)
{
if (i > (actfile + cachefiles) || i < (actfile - cachefiles)) picdata[i] = null;
}
// let the system free up unused memory
GC.Collect();
}
}
}
}