forked from NewLifeX/XCoder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.xaml.cs
109 lines (91 loc) · 2.82 KB
/
App.xaml.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
using System;
using System.Diagnostics;
using System.Windows;
using NewLife;
using NewLife.Log;
using NewLife.Threading;
using Stardust;
namespace CrazyCoder
{
/// <summary>
/// App.xaml 的交互逻辑
/// </summary>
public partial class App : Application
{
public App()
{
InitializeComponent();
}
protected override void OnStartup(StartupEventArgs e)
{
#if NET45
XTrace.UseWinForm();
#endif
StartClient();
StringHelper.EnableSpeechTip = Setting.Current.SpeechTip;
if (Setting.Current.IsNew) "学无先后达者为师,欢迎使用新生命码神工具!".SpeechTip();
base.OnStartup(e);
}
protected override void OnExit(ExitEventArgs e)
{
_Client.Logout("ExitCode=" + e.ApplicationExitCode);
base.OnExit(e);
}
static TimerX _timer;
static StarClient _Client;
private static void StartClient()
{
var set = Setting.Current;
var server = set.Server;
if (server.IsNullOrEmpty()) return;
XTrace.WriteLine("初始化服务端地址:{0}", server);
var client = new StarClient(server)
{
Code = set.Code,
Secret = set.Secret,
Log = XTrace.Log,
};
// 登录后保存证书
client.OnLogined += (s, e) =>
{
var inf = client.Info;
if (inf != null && !inf.Code.IsNullOrEmpty())
{
set.Code = inf.Code;
set.Secret = inf.Secret;
set.Save();
}
};
// 可能需要多次尝试
_timer = new TimerX(TryConnectServer, client, 0, 5_000) { Async = true };
_Client = client;
}
private static void TryConnectServer(Object state)
{
var client = state as StarClient;
var set = Setting.Current;
client.Login().Wait();
CheckUpgrade(client, set.Channel);
// 登录成功,销毁定时器
//TimerX.Current.Period = 0;
_timer.TryDispose();
_timer = null;
}
private static void CheckUpgrade(StarClient client, String channel)
{
// 检查更新
var ur = client.Upgrade(channel).Result;
if (ur != null)
{
//var rs = client.ProcessUpgrade(ur);
//// 强制更新时,马上重启
//if (rs && ur.Force)
//{
// var p = Process.GetCurrentProcess();
// p.Close();
// p.Kill();
//}
}
}
}
}