Newer
Older
// Copyright (C) 2019 Yuanle Song <root@emacsos.com>
//
// This file is part of mbackup-for-windows.
//
// mbackup-for-windows is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by the
// Free Software Foundation, either version 3 of the License, or (at your
// option) any later version.
//
// mbackup-for-windows is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License along with
// mbackup-for-windows. If not, see <http://www.gnu.org/licenses/>.
using System;
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace mbackupw
{
/**
* This is a winforms application used to invoke mbackup.exe without showing cmd.exe window.
* This application is invoked by schtasks to run mbackup regularly.
*/
public partial class Form1 : Form
{
private Timer exitTimer;
// https://docs.microsoft.com/en-us/windows/win32/winmsg/extended-window-styles
private const int WS_EX_NOACTIVATE = 0x08000000;
private const int EXIT_TIMEOUT = 2;
protected override CreateParams CreateParams
{
get
{
CreateParams createParams = base.CreateParams;
createParams.ExStyle |= WS_EX_NOACTIVATE;
return createParams;
}
}
protected override bool ShowWithoutActivation
{
get { return true; }
}
private static void ExitTimerHandler(Object timer, EventArgs eventArgs)
{
Application.Exit();
}
private void ExitInNSeconds(int n)
{
exitTimer = new Timer();
exitTimer.Tick += new EventHandler(ExitTimerHandler);
exitTimer.Interval = n * 1000;
exitTimer.Start();
}
private void RunMbackup()
{
string programFilesDir = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
string mbackupExe = programFilesDir + "\\mbackup\\publish\\mbackup.exe";
int timeoutSeconds = 4 * 3600;
ProcessStartInfo startInfo = new ProcessStartInfo {
FileName = mbackupExe,
Arguments = "",
CreateNoWindow = true
};
Process proc = Process.Start(startInfo);
if (proc.WaitForExit(timeoutSeconds * 1000)) {
Environment.ExitCode = proc.ExitCode;
} else {
Environment.ExitCode = EXIT_TIMEOUT;
proc.Kill();
}
Application.Exit();
Environment.Exit(Environment.ExitCode);
}
public Form1()
{
InitializeComponent();
// Hide form
Opacity = 0;
ShowInTaskbar = false;
RunMbackup();
// auto exit. just used for easier testing.
// ExitInNSeconds(2);
}
}
}