Przeglądaj źródła

Moved batch download thread into BatchDownloader class.

Brett Credo 8 lat temu
rodzic
commit
641189771a

+ 269 - 0
BulkPrinting/BulkPrinting/BatchDownloader.cs

@@ -0,0 +1,269 @@
1
+using System;
2
+using System.Data.SQLite;
3
+using System.Threading;
4
+using Serilog;
5
+
6
+namespace BulkPrinting
7
+{
8
+    public class BatchDownloader : ThreadHelper
9
+    {
10
+        private const int _voucherPageSize = 5000;
11
+        private const int _retryInterval = 30000;
12
+
13
+        private DBHelper _db;
14
+        private Action _refreshAction;
15
+        private bool _checkForNewBatches;
16
+
17
+        public BatchDownloader(DBHelper db, Action refreshAction)
18
+        {
19
+            _db = db;
20
+            _refreshAction = refreshAction;
21
+        }
22
+
23
+        public void CheckForNewBatches()
24
+        {
25
+            lock (_lock)
26
+            {
27
+                _checkForNewBatches = true;
28
+                Monitor.Pulse(_lock);
29
+            }
30
+        }
31
+
32
+        protected override void Run()
33
+        {
34
+            var lastSyncedBatchId = Utility.GetSavedParameterAsInt(_db, "LastSyncedBatchId");
35
+
36
+            bool skipNewBatchCheck = false;
37
+            var isCancelled = false;
38
+            while (!isCancelled)
39
+            {
40
+                if (Globals.SessionMode == SessionModes.Online)
41
+                {
42
+                    Log.Debug("Checking for downloads");
43
+
44
+                    // Check for new batches if required.
45
+                    var doRefresh = false;
46
+                    try
47
+                    {
48
+                        while (!isCancelled && !skipNewBatchCheck)
49
+                        {
50
+                            Log.Debug("Querying batches");
51
+
52
+                            Page<Batch> BatchPage = new Page<Batch>();
53
+                            if (Utility.RESTRequest(ref BatchPage, String.Format("/api/batches/?minBatchId={0}", lastSyncedBatchId + 1)))
54
+                            {
55
+                                if (BatchPage.Items.Count == 0)
56
+                                {
57
+                                    // No more new batches, so stop checking until an order is placed.
58
+                                    skipNewBatchCheck = true;
59
+                                    Log.Debug("All batches downloaded");
60
+                                }
61
+                                else
62
+                                {
63
+                                    foreach (var batch in BatchPage.Items)
64
+                                    {
65
+                                        doRefresh = true;
66
+                                        Utility.SaveBatch(_db, batch);
67
+                                        lastSyncedBatchId = batch.Id;
68
+                                        if (!Utility.UpdateSavedParameter(_db, "LastSyncedBatchId", lastSyncedBatchId))
69
+                                        {
70
+                                            // Shouldn't happen.
71
+                                            throw new Exception("Failed to update LastSyncedBatchId");
72
+                                        }
73
+                                    }
74
+                                }
75
+                            }
76
+                            else
77
+                            {
78
+                                // Error, so leave skipNewBatchCheck unchanged.
79
+                                break;
80
+                            }
81
+
82
+                            lock (_lock)
83
+                            {
84
+                                isCancelled = _cancelled;
85
+                            }
86
+                        }
87
+                    }
88
+                    catch (Exception e)
89
+                    {
90
+                        Log.Error(e, "Error while downloading batch list");
91
+                    }
92
+
93
+                    if (!isCancelled && doRefresh)
94
+                    {
95
+                        doRefresh = false;
96
+                        _refreshAction();
97
+                    }
98
+
99
+                    // Continue downloading incomplete batches if any.
100
+                    try
101
+                    {
102
+                        using (var command = _db.CreateCommand(
103
+                            "SELECT Id FROM Batch WHERE Downloaded=0 ORDER BY Downloaded, Id"))
104
+                        {
105
+                            using (SQLiteDataReader row = command.ExecuteReader())
106
+                            {
107
+                                while (! isCancelled && row.Read())
108
+                                {
109
+                                    int batchId = (int)row["Id"];
110
+                                    try
111
+                                    {
112
+                                        Batch batch = new Batch();
113
+                                        if (Utility.RESTRequest(ref batch, String.Format("/api/batches/{0}", batchId)))
114
+                                        {
115
+                                            doRefresh = true;
116
+                                            Utility.SaveBatch(_db, batch);
117
+
118
+                                            if (batch.ReadyForDownload)
119
+                                            {
120
+                                                long result = (long)_db.ExecuteScalar(
121
+                                                    "SELECT COUNT(*) FROM Voucher WHERE BatchId=@BatchId",
122
+                                                    new SQLiteParameter("@BatchId", batchId));
123
+                                                int voucherCount = (int)result;
124
+
125
+                                                while (!isCancelled && (voucherCount < batch.DeliveredQuantity))
126
+                                                {
127
+                                                    Log.Debug("Downloading vouchers for batch {0} ({1} so far)", batchId, voucherCount);
128
+
129
+                                                    int page = voucherCount / _voucherPageSize + 1;
130
+                                                    int offset = voucherCount % _voucherPageSize;
131
+
132
+                                                    Page<Voucher> voucherPage = new Page<Voucher>();
133
+                                                    if (!Utility.RESTRequest(ref voucherPage, String.Format(
134
+                                                        "/api/batches/{0}/vouchers/?page={1}&pageSize={2}",
135
+                                                        batchId, page, _voucherPageSize)))
136
+                                                    {
137
+                                                        break;
138
+                                                    }
139
+                                                    if (voucherPage.Items.Count == 0)
140
+                                                    {
141
+                                                        throw new Exception(String.Format("Too few vouchers were returned for batch {0}", batchId));
142
+                                                    }
143
+
144
+                                                    Log.Debug("Downloaded vouchers for batch {0} ({1} so far)", batchId, voucherCount);
145
+
146
+                                                    lock (_db.WriteLock)
147
+                                                    {
148
+                                                        using (var trans = _db.BeginTransaction())
149
+                                                        {
150
+                                                            using (var insert = _db.CreateCommand(
151
+                                                                "INSERT INTO Voucher (Id, SequenceNumber, ExpiryDate, Serial, EncryptedPIN, BatchId)" +
152
+                                                                             "VALUES (@Id,@SequenceNumber,@ExpiryDate,@Serial,@EncryptedPin,@BatchId)",
153
+                                                                trans))
154
+                                                            {
155
+                                                                for (var i = offset; i < voucherPage.Items.Count; i++)
156
+                                                                {
157
+                                                                    var voucher = voucherPage.Items[i];
158
+                                                                    if (voucher.SequenceNumber != voucherCount + 1)
159
+                                                                    {
160
+                                                                        throw new Exception(String.Format("Vouchers for batch {0} are not sequential: expecting {1}, got {2}", batchId, voucherCount + 1, voucher.SequenceNumber));
161
+                                                                    }
162
+                                                                    insert.Parameters.Clear();
163
+                                                                    insert.Parameters.AddWithValue("@Id", voucher.Id);
164
+                                                                    insert.Parameters.AddWithValue("@SequenceNumber", voucher.SequenceNumber);
165
+                                                                    insert.Parameters.AddWithValue("@ExpiryDate", voucher.ExpiryDate.Date);
166
+                                                                    insert.Parameters.AddWithValue("@Serial", voucher.Serial);
167
+                                                                    insert.Parameters.AddWithValue("@EncryptedPIN", voucher.EncryptedPIN);
168
+                                                                    insert.Parameters.AddWithValue("@BatchId", batchId);
169
+                                                                    insert.ExecuteNonQuery();
170
+                                                                    voucherCount++;
171
+                                                                }
172
+                                                            }
173
+                                                            trans.Commit();
174
+                                                        }
175
+                                                    }
176
+
177
+                                                    Log.Debug("Inserted vouchers for batch {0}", batchId);
178
+
179
+                                                    lock (_lock)
180
+                                                    {
181
+                                                        isCancelled = _cancelled;
182
+                                                    }
183
+                                                }
184
+
185
+                                                if (voucherCount > batch.DeliveredQuantity)
186
+                                                {
187
+                                                    throw new Exception("Batch contains more than the specified number of vouchers");
188
+                                                }
189
+                                                else if (voucherCount == batch.DeliveredQuantity)
190
+                                                {
191
+                                                    Log.Debug("Batch {0} complete", batchId);
192
+
193
+                                                    _db.ExecuteNonQuery(
194
+                                                        "UPDATE Batch SET Downloaded=1 WHERE Id=@BatchId",
195
+                                                        new SQLiteParameter("@BatchId", batchId));
196
+                                                }
197
+                                            }
198
+                                        }
199
+                                        else if (batch == null)
200
+                                        {
201
+                                            // Batch will only be null if we got a 404.
202
+                                            Log.Debug("Removing deleted batch {0}", batchId);
203
+
204
+                                            _db.ExecuteNonQuery(
205
+                                                "DELETE FROM Voucher WHERE BatchId=@BatchId",
206
+                                                new SQLiteParameter("@BatchId", batchId));
207
+
208
+                                            _db.ExecuteNonQuery(
209
+                                                "DELETE FROM Batch WHERE Id=@BatchId",
210
+                                                new SQLiteParameter("@BatchId", batchId));
211
+
212
+                                            doRefresh = true;
213
+                                        }
214
+                                        // else retry later
215
+                                    }
216
+                                    catch (Exception e)
217
+                                    {
218
+                                        Log.Error(e, "Error while downloading vouchers for batch {0}", batchId);
219
+                                    }
220
+
221
+                                    if (!isCancelled && doRefresh)
222
+                                    {
223
+                                        doRefresh = false;
224
+                                        _refreshAction();
225
+                                    }
226
+
227
+                                    lock(_lock)
228
+                                    {
229
+                                        isCancelled = _cancelled;
230
+                                    }
231
+                                }
232
+                            }
233
+                        }
234
+                    }
235
+                    catch (Exception e)
236
+                    {
237
+                        Log.Error(e, "Error while downloading vouchers");
238
+                    }
239
+                }
240
+                else
241
+                {
242
+                    Log.Debug("Offline, skipping download check");
243
+                }
244
+
245
+                Log.Debug("Download thread sleeping");
246
+
247
+                lock (_lock)
248
+                {
249
+                    while (!_cancelled && !_checkForNewBatches)
250
+                    {
251
+                        if (! Monitor.Wait(_lock, _retryInterval))
252
+                        {
253
+                            // Timed out. Retry downloads.
254
+                            break;
255
+                        }
256
+                    }
257
+                    if (_checkForNewBatches)
258
+                    {
259
+                        _checkForNewBatches = false;
260
+                        skipNewBatchCheck = false;
261
+                    }
262
+                    isCancelled = _cancelled;
263
+                }
264
+
265
+                Log.Debug("Download thread woke up, cancelled={0}", isCancelled);
266
+            }
267
+        }
268
+    }
269
+}

+ 0 - 7
BulkPrinting/BulkPrinting/BatchForm.Designer.cs

@@ -53,7 +53,6 @@
53 53
             this.dgvBatches = new System.Windows.Forms.DataGridView();
54 54
             this.LogoutTimer = new System.Windows.Forms.Timer(this.components);
55 55
             this.lblLoading = new System.Windows.Forms.Label();
56
-            this.ReSyncTimer = new System.Windows.Forms.Timer(this.components);
57 56
             this.pnlSplitGrids.SuspendLayout();
58 57
             this.grpFilter.SuspendLayout();
59 58
             ((System.ComponentModel.ISupportInitialize)(this.dgvBatches)).BeginInit();
@@ -365,11 +364,6 @@
365 364
             this.lblLoading.TabIndex = 6;
366 365
             this.lblLoading.Text = "Loading...";
367 366
             // 
368
-            // ReSyncTimer
369
-            // 
370
-            this.ReSyncTimer.Interval = 30000;
371
-            this.ReSyncTimer.Tick += new System.EventHandler(this.ReSyncTimer_Tick);
372
-            // 
373 367
             // BatchForm
374 368
             // 
375 369
             this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
@@ -419,7 +413,6 @@
419 413
         private System.Windows.Forms.RadioButton rdoFilterReprinted;
420 414
         private System.Windows.Forms.RadioButton rdoFilterUnprinted;
421 415
         private System.Windows.Forms.RadioButton rdoFilterPrinted;
422
-        private System.Windows.Forms.Timer ReSyncTimer;
423 416
     }
424 417
 }
425 418
 

+ 25 - 256
BulkPrinting/BulkPrinting/BatchForm.cs

@@ -2,21 +2,16 @@
2 2
 using System.Collections.Generic;
3 3
 using System.ComponentModel;
4 4
 using System.Data.SQLite;
5
-using System.Diagnostics;
6 5
 using System.Drawing;
7 6
 using System.Globalization;
8
-using System.Threading;
7
+using System.Security.Permissions;
9 8
 using System.Windows.Forms;
10
-using Serilog;
11 9
 
12 10
 namespace BulkPrinting
13 11
 {
14 12
     public partial class BatchForm : ObservedForm
15 13
     {
16
-        Thread LoadingThread;
17
-        readonly object LoadingThreadLock = new object();
18
-        bool CheckForNewBatches;
19
-        bool RetryDownloads;
14
+        private BatchDownloader BatchDownloader;
20 15
 
21 16
         public BatchForm()
22 17
         {
@@ -258,254 +253,36 @@ namespace BulkPrinting
258 253
             dtpFilterStartDate.Value = DateTime.Now.AddMonths(-3);
259 254
             Globals.OpenBatches = new List<int>();
260 255
 
261
-            RetryDownloads = true;
262
-            CheckForNewBatches = true;
256
+            var hwnd = new System.Runtime.InteropServices.HandleRef(this, Handle);
257
+            BatchDownloader = new BatchDownloader(Globals.DB, () =>
258
+            {
259
+                // DO NOT use Invoke as this can cause a deadlock if we are attempting to join the BatchDownloader after a cancel.
260
+                // Also, use PostMessage rather than SendMessage as PostMessage doesn't wait for the message to be processed and
261
+                // therefore won't block.
262
+                Utility.PostMessage(hwnd, Utility.WM_USER, IntPtr.Zero, IntPtr.Zero);
263
+            });
264
+            BatchDownloader.Start();
263 265
 
264
-            LoadingThread = new Thread(LoadingThreadWorker);
265
-            LoadingThread.Start();
266 266
             Utility.InitialiseUserLimits(Globals.DB);
267
-            ReSyncTimer.Enabled = true;
267
+
268
+            PopulateGrid();
268 269
         }
269 270
 
270
-        private void LoadingThreadWorker()
271
+        [SecurityPermissionAttribute(SecurityAction.InheritanceDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
272
+        [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
273
+        protected override void WndProc(ref Message m)
271 274
         {
272
-            const int voucherPageSize = 5000;
273
-            var db = Globals.DB;
274
-            //using (var db = Utility.OpenDBConnection())
275
+            if (m.Msg == Utility.WM_USER)
275 276
             {
276
-                Invoke(new Action(() =>
277
-                {
278
-                    PopulateGrid();
279
-                }));
280
-
281
-                var lastSyncedBatchId = Utility.GetSavedParameterAsInt(db, "LastSyncedBatchId");
282
-
283
-                bool skipNewBatchCheck = false;
284
-                while (true)
285
-                {
286
-                    Log.Debug("Download thread sleeping");
287
-
288
-                    bool forceNewBatchCheck;
289
-                    lock (LoadingThreadLock)
290
-                    {
291
-                        while (!RetryDownloads)
292
-                        {
293
-                            Monitor.Wait(LoadingThreadLock);
294
-                        }
295
-                        forceNewBatchCheck = CheckForNewBatches;
296
-                        RetryDownloads = false;
297
-                        CheckForNewBatches = false;
298
-                    }
299
-
300
-                    Log.Debug("Download thread woke up");
301
-
302
-                    if (Globals.SessionMode == SessionModes.Online)
303
-                    {
304
-                        Log.Debug("Checking for downloads");
305
-
306
-                        if (forceNewBatchCheck)
307
-                        {
308
-                            skipNewBatchCheck = false;
309
-                        }
310
-
311
-                        // Check for new batches if required.
312
-                        var refreshGrid = false;
313
-                        try
314
-                        {
315
-                            while (!skipNewBatchCheck)
316
-                            {
317
-                                Log.Debug("Querying batches");
318
-
319
-                                Page<Batch> BatchPage = new Page<Batch>();
320
-                                if (Utility.RESTRequest(ref BatchPage, String.Format("/api/batches/?minBatchId={0}", lastSyncedBatchId + 1)))
321
-                                {
322
-                                    if (BatchPage.Items.Count == 0)
323
-                                    {
324
-                                        // No more new batches, so stop checking until an order is placed.
325
-                                        skipNewBatchCheck = true;
326
-                                    }
327
-                                    else
328
-                                    {
329
-                                        foreach (var batch in BatchPage.Items)
330
-                                        {
331
-                                            refreshGrid = true;
332
-                                            Utility.SaveBatch(db, batch);
333
-                                            lastSyncedBatchId = batch.Id;
334
-                                            if (!Utility.UpdateSavedParameter(db, "LastSyncedBatchId", lastSyncedBatchId))
335
-                                            {
336
-                                                // Shouldn't happen.
337
-                                                throw new Exception("Failed to update LastSyncedBatchId");
338
-                                            }
339
-                                        }
340
-                                    }
341
-                                }
342
-                                else
343
-                                {
344
-                                    // Error, so leave skipNewBatchCheck unchanged.
345
-                                    break;
346
-                                }
347
-                            }
348
-                        }
349
-                        catch (Exception e)
350
-                        {
351
-                            Log.Error(e, "Error while downloading batch list");
352
-                        }
353
-
354
-                        if (refreshGrid)
355
-                        {
356
-                            Invoke(new Action(() =>
357
-                            {
358
-                                PopulateGrid();
359
-                            }));
360
-                        }
361
-
362
-                        // Continue downloading incomplete batches if any.
363
-                        try
364
-                        {
365
-                            using (var command = db.CreateCommand("SELECT Id FROM Batch WHERE Downloaded=0 ORDER BY Downloaded, Id"))
366
-                            {
367
-                                using (SQLiteDataReader row = command.ExecuteReader())
368
-                                {
369
-                                    while (row.Read())
370
-                                    {
371
-                                        refreshGrid = false;
372
-
373
-                                        int batchId = (int)row["Id"];
374
-                                        try
375
-                                        {
376
-                                            Batch batch = new Batch();
377
-                                            if (Utility.RESTRequest(ref batch, String.Format("/api/batches/{0}", batchId)))
378
-                                            {
379
-                                                refreshGrid = true;
380
-                                                Utility.SaveBatch(db, batch);
381
-
382
-                                                if (batch.ReadyForDownload)
383
-                                                {
384
-                                                    long result = (long)db.ExecuteScalar(
385
-                                                        "SELECT COUNT(*) FROM Voucher WHERE BatchId=@BatchId",
386
-                                                        new SQLiteParameter("@BatchId", batchId));
387
-                                                    int voucherCount = (int)result;
388
-
389
-                                                    while (voucherCount < batch.DeliveredQuantity)
390
-                                                    {
391
-                                                        Log.Debug("Downloading vouchers for batch {0} ({1} so far)", batchId, voucherCount);
392
-
393
-                                                        int page = voucherCount / voucherPageSize + 1;
394
-                                                        int offset = voucherCount % voucherPageSize;
395
-
396
-                                                        Page<Voucher> voucherPage = new Page<Voucher>();
397
-                                                        if (!Utility.RESTRequest(ref voucherPage, String.Format("/api/batches/{0}/vouchers/?page={1}&pageSize={2}", batchId, page, voucherPageSize)))
398
-                                                        {
399
-                                                            break;
400
-                                                        }
401
-                                                        if (voucherPage.Items.Count == 0)
402
-                                                        {
403
-                                                            throw new Exception(String.Format("Too few vouchers were returned for batch {0}", batchId));
404
-                                                        }
405
-
406
-                                                        Log.Debug("Downloaded vouchers for batch {0} ({1} so far)", batchId, voucherCount);
407
-
408
-                                                        lock (db.WriteLock)
409
-                                                        {
410
-                                                            using (var trans = db.BeginTransaction())
411
-                                                            {
412
-                                                                using (var insert = db.CreateCommand(
413
-                                                                    "INSERT INTO Voucher (Id, SequenceNumber, ExpiryDate, Serial, EncryptedPIN, BatchId)" +
414
-                                                                                 "VALUES (@Id,@SequenceNumber,@ExpiryDate,@Serial,@EncryptedPin,@BatchId)",
415
-                                                                    trans))
416
-                                                                {
417
-                                                                    for (var i = offset; i < voucherPage.Items.Count; i++)
418
-                                                                    {
419
-                                                                        var voucher = voucherPage.Items[i];
420
-                                                                        if (voucher.SequenceNumber != voucherCount + 1)
421
-                                                                        {
422
-                                                                            throw new Exception(String.Format("Vouchers for batch {0} are not sequential: expecting {1}, got {2}", batchId, voucherCount + 1, voucher.SequenceNumber));
423
-                                                                        }
424
-                                                                        insert.Parameters.Clear();
425
-                                                                        insert.Parameters.AddWithValue("@Id", voucher.Id);
426
-                                                                        insert.Parameters.AddWithValue("@SequenceNumber", voucher.SequenceNumber);
427
-                                                                        insert.Parameters.AddWithValue("@ExpiryDate", voucher.ExpiryDate.Date);
428
-                                                                        insert.Parameters.AddWithValue("@Serial", voucher.Serial);
429
-                                                                        insert.Parameters.AddWithValue("@EncryptedPIN", voucher.EncryptedPIN);
430
-                                                                        insert.Parameters.AddWithValue("@BatchId", batchId);
431
-                                                                        insert.ExecuteNonQuery();
432
-                                                                        voucherCount++;
433
-                                                                    }
434
-                                                                }
435
-                                                                trans.Commit();
436
-                                                            }
437
-                                                        }
438
-
439
-                                                        Log.Debug("Inserted vouchers for batch {0}", batchId);
440
-                                                    }
441
-
442
-                                                    if (voucherCount > batch.DeliveredQuantity)
443
-                                                    {
444
-                                                        throw new Exception("Batch contains more than the specified number of vouchers");
445
-                                                    }
446
-                                                    else
447
-                                                    {
448
-                                                        Log.Debug("Batch {0} complete", batchId);
449
-
450
-                                                        db.ExecuteNonQuery(
451
-                                                            "UPDATE Batch SET Downloaded=1 WHERE Id=@BatchId",
452
-                                                            new SQLiteParameter("@BatchId", batchId));
453
-                                                    }
454
-                                                }
455
-                                            }
456
-                                            else if (batch == null)
457
-                                            {
458
-                                                // Batch will only be null if we got a 404.
459
-                                                Log.Debug("Removing deleted batch {0}", batchId);
460
-
461
-                                                db.ExecuteNonQuery(
462
-                                                    "DELETE FROM Voucher WHERE BatchId=@BatchId",
463
-                                                    new SQLiteParameter("@BatchId", batchId));
464
-
465
-                                                db.ExecuteNonQuery(
466
-                                                    "DELETE FROM Batch WHERE Id=@BatchId",
467
-                                                    new SQLiteParameter("@BatchId", batchId));
468
-
469
-                                                refreshGrid = true;
470
-                                            }
471
-                                        }
472
-                                        catch (Exception e)
473
-                                        {
474
-                                            Log.Error(e, "Error while downloading vouchers for batch {0}", batchId);
475
-                                        }
476
-
477
-                                        if (refreshGrid)
478
-                                        {
479
-                                            Invoke(new Action(() =>
480
-                                            {
481
-                                                PopulateGrid();
482
-                                            }));
483
-                                        }
484
-                                    }
485
-                                }
486
-                            }
487
-                        }
488
-                        catch (Exception e)
489
-                        {
490
-                            Log.Error(e, "Error while downloading vouchers");
491
-                        }
492
-                    }
493
-                    else
494
-                    {
495
-                        Log.Debug("Offline, skipping download check");
496
-                    }
497
-                }
277
+                PopulateGrid();
278
+                return;
498 279
             }
280
+            base.WndProc(ref m);
499 281
         }
500 282
 
501 283
         public void NewBatchAvailable()
502 284
         {
503
-            lock (LoadingThreadLock)
504
-            {
505
-                RetryDownloads = true;
506
-                CheckForNewBatches = true;
507
-                Monitor.Pulse(LoadingThreadLock);
508
-            }
285
+            BatchDownloader.CheckForNewBatches();
509 286
         }
510 287
 
511 288
         private void btnOrder_Click(object sender, EventArgs e)
@@ -588,11 +365,12 @@ namespace BulkPrinting
588 365
                 return;
589 366
             }
590 367
 
591
-            if ((LoadingThread != null) && (LoadingThread.IsAlive))
368
+            if (BatchDownloader != null)
592 369
             {
593
-                LoadingThread.Abort();
594
-                LoadingThread.Join();
370
+                BatchDownloader.Cancel();
371
+                BatchDownloader.Join();
595 372
             }
373
+
596 374
             Utility.Logout();
597 375
         }
598 376
 
@@ -774,14 +552,5 @@ namespace BulkPrinting
774 552
                 e.Graphics.DrawImage(b.BackgroundImage, b.ClientRectangle);
775 553
             }
776 554
         }
777
-
778
-        private void ReSyncTimer_Tick(object sender, EventArgs e)
779
-        {
780
-            lock (LoadingThreadLock)
781
-            {
782
-                RetryDownloads = true;
783
-                Monitor.Pulse(LoadingThreadLock);
784
-            }
785
-        }
786 555
     }
787 556
 }

+ 0 - 3
BulkPrinting/BulkPrinting/BatchForm.resx

@@ -123,9 +123,6 @@
123 123
   <metadata name="LogoutTimer.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
124 124
     <value>277, 17</value>
125 125
   </metadata>
126
-  <metadata name="ReSyncTimer.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
127
-    <value>398, 3</value>
128
-  </metadata>
129 126
   <metadata name="$this.TrayHeight" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
130 127
     <value>59</value>
131 128
   </metadata>

+ 1 - 0
BulkPrinting/BulkPrinting/BulkPrinting.csproj

@@ -154,6 +154,7 @@
154 154
     <Reference Include="System.Xml" />
155 155
   </ItemGroup>
156 156
   <ItemGroup>
157
+    <Compile Include="BatchDownloader.cs" />
157 158
     <Compile Include="Configuration.cs" />
158 159
     <Compile Include="DBHelper.cs" />
159 160
     <Compile Include="ExportForm.cs">

+ 6 - 1
BulkPrinting/BulkPrinting/Utility.cs

@@ -7,15 +7,20 @@ using System.IO;
7 7
 using System.Linq;
8 8
 using System.Management;
9 9
 using System.Net;
10
+using System.Runtime.InteropServices;
10 11
 using System.Security.Cryptography;
11 12
 using System.Text;
12
-using System.Threading;
13 13
 using System.Windows.Forms;
14 14
 
15 15
 namespace BulkPrinting
16 16
 {
17 17
     public class Utility
18 18
     {
19
+        public const int WM_USER = 0x400;
20
+
21
+        [DllImport("user32.dll", SetLastError = true)]
22
+        public static extern bool PostMessage(HandleRef hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
23
+
19 24
         public static string GetHDDSerial()
20 25
         {
21 26
             ManagementObjectSearcher managementObjectSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");