Skip to content

Commit 8563a8d

Browse files
committed
Thread long-running tasks to background so UI doesn't freeze
Also, added more legal headers
1 parent 165a969 commit 8563a8d

File tree

3 files changed

+184
-61
lines changed

3 files changed

+184
-61
lines changed

Keenou/EncryptHome.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,23 @@
1-
using System;
1+
/*
2+
* Keenou
3+
* Copyright (C) 2015 Charles Munson
4+
*
5+
* This program is free software; you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation; either version 2 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License along
16+
* with this program; if not, write to the Free Software Foundation, Inc.,
17+
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*/
19+
20+
using System;
221
using System.Diagnostics;
322
using System.IO;
423
using System.Threading;

Keenou/MainWindow.cs

Lines changed: 144 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,23 @@
1-
using System;
1+
/*
2+
* Keenou
3+
* Copyright (C) 2015 Charles Munson
4+
*
5+
* This program is free software; you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation; either version 2 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License along
16+
* with this program; if not, write to the Free Software Foundation, Inc.,
17+
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*/
19+
20+
using System;
221
using System.Windows.Forms;
322
using System.Security.Principal;
423
using System.IO;
@@ -211,6 +230,9 @@ private void b_encrypt_Click(object sender, EventArgs e)
211230

212231

213232
// Generate master key & protect with user password //
233+
l_statusLabel.Text = "Generating encryption key ...";
234+
Application.DoEvents();
235+
214236
string masterKey = null;
215237
string encMasterKey = null;
216238
try
@@ -234,74 +256,124 @@ private void b_encrypt_Click(object sender, EventArgs e)
234256

235257

236258

237-
// Create new encrypted volume //
238-
l_statusLabel.Text = "Creating encrypted volume ...";
239-
Application.DoEvents();
240-
res = EncryptHome.CreateEncryptedVolume(hashChosen, t_volumeLoc.Text, targetDrive, masterKey, cipherChosen, volSize);
241-
if (res == null || !res.Success)
259+
260+
// Run work-heavy tasks in a separate thread
261+
CancellationTokenSource cts = new CancellationTokenSource();
262+
CancellationToken cancelToken = cts.Token;
263+
var workerThread = Task.Factory.StartNew(() =>
242264
{
243-
ReportEncryptHomeError(res);
244-
return;
245-
}
246-
// * //
247265

266+
// Update UI
267+
this.Invoke((MethodInvoker)delegate
268+
{
269+
s_progress.Value = 25;
270+
l_statusLabel.Text = "Creating encrypted volume ...";
271+
Application.DoEvents();
272+
s_progress.ProgressBar.Refresh();
273+
});
274+
275+
// Create new encrypted volume //
276+
res = EncryptHome.CreateEncryptedVolume(hashChosen, t_volumeLoc.Text, targetDrive, masterKey, cipherChosen, volSize);
277+
if (res == null || !res.Success)
278+
{
279+
return res;
280+
}
281+
// * //
248282

249283

250-
// Mount home folder's encrypted file as targetDrive //
251-
s_progress.Value = 33;
252-
l_statusLabel.Text = "Mounting encrypted volume ...";
253-
Application.DoEvents();
254-
s_progress.ProgressBar.Refresh();
255-
res = EncryptHome.MountEncryptedVolume(hashChosen, t_volumeLoc.Text, targetDrive, masterKey);
256-
if (res == null || !res.Success)
257-
{
258-
ReportEncryptHomeError(res);
259-
return;
260-
}
261-
// * //
262284

285+
// Update UI
286+
this.Invoke((MethodInvoker)delegate
287+
{
288+
s_progress.Value = 50;
289+
l_statusLabel.Text = "Mounting encrypted volume ...";
290+
Application.DoEvents();
291+
s_progress.ProgressBar.Refresh();
292+
});
293+
294+
// Mount home folder's encrypted file as targetDrive //
295+
res = EncryptHome.MountEncryptedVolume(hashChosen, t_volumeLoc.Text, targetDrive, masterKey);
296+
if (res == null || !res.Success)
297+
{
298+
return res;
299+
}
300+
// * //
263301

264302

265-
// Copy everything over from home directory to encrypted container //
266-
s_progress.Value = 66;
267-
l_statusLabel.Text = "Copying home directory to encrypted container ...";
268-
Application.DoEvents();
269-
s_progress.ProgressBar.Refresh();
270-
res = EncryptHome.CopyDataFromHomeFolder(this.homeFolder, targetDrive);
271-
if (res == null || !res.Success)
303+
304+
// Update UI
305+
this.Invoke((MethodInvoker)delegate
306+
{
307+
s_progress.Value = 75;
308+
l_statusLabel.Text = "Copying home directory to encrypted container ...";
309+
Application.DoEvents();
310+
s_progress.ProgressBar.Refresh();
311+
});
312+
313+
// Copy everything over from home directory to encrypted container //
314+
res = EncryptHome.CopyDataFromHomeFolder(this.homeFolder, targetDrive);
315+
if (res == null || !res.Success)
316+
{
317+
return res;
318+
}
319+
// * //
320+
321+
322+
323+
// TODO: Unmount encrypted volume (after beta testing over)
324+
// VeraCrypt should auto-unmount, but we'll do it manually to be sure
325+
326+
327+
328+
return new BooleanResult() { Success = true };
329+
330+
}, TaskCreationOptions.LongRunning);
331+
332+
333+
334+
// When threaded tasks finish, check for errors and continue (if appropriate)
335+
workerThread.ContinueWith((antecedent) =>
272336
{
273-
ReportEncryptHomeError(res);
274-
return;
275-
}
276-
// * //
337+
// Block until we get a result back from previous thread
338+
BooleanResult result = antecedent.Result;
277339

278340

341+
// Check if there was an error in previous thread
342+
if (result == null || !result.Success)
343+
{
344+
ReportEncryptHomeError(result);
345+
return;
346+
}
279347

280-
// TODO: Unmount encrypted volume (after beta testing over)
281-
// VeraCrypt should auto-unmount, but we'll do it manually to be sure
282348

283349

350+
// Set necessary registry values //
351+
Registry.SetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Keenou\" + this.usrSID, "encContainerLoc", t_volumeLoc.Text);
352+
Registry.SetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Keenou\" + this.usrSID, "firstBoot", true, RegistryValueKind.DWord);
353+
Registry.SetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Keenou\" + this.usrSID, "hash", hashChosen);
354+
Registry.SetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Keenou\" + this.usrSID, "encHeader", encMasterKey);
355+
// * //
284356

285-
// Set necessary registry values //
286-
Registry.SetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Keenou\" + this.usrSID, "encContainerLoc", t_volumeLoc.Text);
287-
Registry.SetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Keenou\" + this.usrSID, "firstBoot", true, RegistryValueKind.DWord);
288-
Registry.SetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Keenou\" + this.usrSID, "hash", hashChosen);
289-
Registry.SetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Keenou\" + this.usrSID, "encHeader", encMasterKey);
290-
// * //
291357

292358

359+
// Re-enable everything //
360+
this.Cursor = Cursors.Default;
361+
l_statusLabel.Text = "Log out and back in to finish ...";
362+
s_progress.Value = 100;
363+
Application.DoEvents();
364+
// * //
293365

294-
// Re-enable everything //
295-
this.Cursor = Cursors.Default;
296-
l_statusLabel.Text = "Log out and back in to finish ...";
297-
s_progress.Value = 100;
298-
Application.DoEvents();
299-
// * //
300366

301367

368+
// Inform user of the good news
369+
MessageBox.Show("Almost done! You must log out and log back in via Keenou-pGina to finish the migration!");
370+
371+
},
372+
cancelToken,
373+
TaskContinuationOptions.OnlyOnRanToCompletion,
374+
TaskScheduler.FromCurrentSynchronizationContext()
375+
);
302376

303-
// Inform user of the good news
304-
MessageBox.Show("Almost done! You must log out and log back in via Keenou-pGina to finish the migration!");
305377
}
306378
// * //
307379

@@ -349,15 +421,33 @@ private void b_setVolumeSize_Click(object sender, EventArgs e)
349421
Application.DoEvents();
350422

351423

352-
// Determine free space on enc volume target drive
353-
long targetSpace = Toolbox.GetAvailableFreeSpace(t_volumeLoc.Text);
354-
355-
356424
// Do calculation of current size (if not already done)
357425
if (this.homeDirSize <= 0)
358426
{
359-
this.homeDirSize = Toolbox.GetDirectorySize(this.homeFolder);
427+
var taskA = Task.Factory.StartNew(() => Toolbox.GetDirectorySize(this.homeFolder), TaskCreationOptions.LongRunning);
428+
CancellationTokenSource cts = new CancellationTokenSource();
429+
CancellationToken cancelToken = cts.Token;
430+
431+
taskA.ContinueWith((antecedent) =>
432+
{
433+
this.homeDirSize = antecedent.Result;
434+
this.b_setVolumeSize_Click_Callback();
435+
},
436+
cancelToken,
437+
TaskContinuationOptions.OnlyOnRanToCompletion,
438+
TaskScheduler.FromCurrentSynchronizationContext()
439+
);
440+
}
441+
else
442+
{
443+
this.b_setVolumeSize_Click_Callback();
360444
}
445+
}
446+
447+
private void b_setVolumeSize_Click_Callback()
448+
{
449+
// Determine free space on enc volume target drive
450+
long targetSpace = Toolbox.GetAvailableFreeSpace(t_volumeLoc.Text);
361451

362452

363453
// Show suggested volume size

Keenou/Toolbox.cs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,23 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
1+
/*
2+
* Keenou
3+
* Copyright (C) 2015 Charles Munson
4+
*
5+
* This program is free software; you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation; either version 2 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License along
16+
* with this program; if not, write to the Free Software Foundation, Inc.,
17+
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*/
19+
20+
using System;
421
using System.Text;
522
using System.Diagnostics;
623
using System.Windows.Forms;
@@ -13,9 +30,6 @@ namespace Keenou
1330
static class Toolbox
1431
{
1532

16-
17-
18-
1933
// Get SHA-512 signature from input text //
2034
public static string SHA512_Base64(string input)
2135
{

0 commit comments

Comments
 (0)