forked from UnigramDev/Unigram
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
132 lines (123 loc) · 5.15 KB
/
Program.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
//
// Copyright Fela Ameghino 2015-2023
//
// Distributed under the GNU General Public License v3.0. (See accompanying
// file LICENSE or copy at https://www.gnu.org/licenses/gpl-3.0.txt)
//
#if DISABLE_XAML_GENERATED_MAIN
using Mono.Options;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.ApplicationModel;
using Windows.ApplicationModel.Activation;
namespace Unigram
{
public static class Program
{
// This example code shows how you could implement the required Main method to
// support multi-instance redirection. The minimum requirement is to call
// Application.Start with a new App object. Beyond that, you may delete the
// rest of the example code and replace it with your custom code if you wish.
static int _lastId = 0;
static void Main(string[] args)
{
// First, we'll get our activation event args, which are typically richer
// than the incoming command-line args. We can use these in our app-defined
// logic for generating the key for this instance.
var activatedArgs = AppInstance.GetActivatedEventArgs();
if (activatedArgs is CommandLineActivatedEventArgs commandLine && TryParseCommandLine(commandLine, out int id, out bool test))
{
_lastId = id;
var instance = AppInstance.FindOrRegisterInstanceForKey(id.ToString());
if (instance.IsCurrentInstance)
{
// If we successfully registered this instance, we can now just
// go ahead and do normal XAML initialization.
global::Windows.UI.Xaml.Application.Start((p) => new App(id));
}
else
{
// Some other instance has registered for this key, so we'll
// redirect this activation to that instance instead.
instance.RedirectActivationTo();
}
}
else
{
// The platform might provide a recommended instance.
if (AppInstance.RecommendedInstance != null)
{
AppInstance.RecommendedInstance.RedirectActivationTo();
}
else
{
// If the platform hasn't expressed a preference, we need to examine all
// other instances to see if any are suitable for redirecting this request.
// In the simple case, any instance will do.
//AppInstance instance = instances.FirstOrDefault();
// If the app re-registers re-usable instances, we can filter for these instead.
//AppInstance instance = instances.Where((i) => i.Key.StartsWith("REUSABLE")).FirstOrDefault();
//if (instance != null)
//{
// Debug.WriteLine($"instance = {instance.Key}");
// instance.RedirectActivationTo();
//}
//else
//{
// AppInstance.FindOrRegisterInstanceForKey("REUSABLE" + App.Id.ToString());
// UpdateSharedInstanceNumber();
// global::Windows.UI.Xaml.Application.Start((p) => new App());
//}
var instance = AppInstance.FindOrRegisterInstanceForKey(_lastId.ToString());
if (instance.IsCurrentInstance)
{
// If we successfully registered this instance, we can now just
// go ahead and do normal XAML initialization.
global::Windows.UI.Xaml.Application.Start((p) => new App(0));
}
else
{
// Some other instance has registered for this key, so we'll
// redirect this activation to that instance instead.
instance.RedirectActivationTo();
}
}
}
}
private static bool TryParseCommandLine(CommandLineActivatedEventArgs args, out int id, out bool test)
{
#if !DEBUG
if (args.PreviousExecutionState != ApplicationExecutionState.Terminated)
{
id = 0;
test = false;
return false;
}
#endif
try
{
var v_id = 0;
var v_test = false;
var p = new OptionSet()
{
{ "i|id=", (int v) => v_id = v },
{ "t|test", v => v_test = v != null },
};
var extra = p.Parse(args.Operation.Arguments.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries));
id = v_id;
test = v_test;
return true;
}
catch
{
id = 0;
test = false;
return false;
}
}
}
}
#endif