मैं बंद के लिए कैसे .net कोर वेब होस्ट प्रतीक्षा करता है के लिए इसी तरह एक विचार कंसोल अनुप्रयोगों में। मैं GitHub पर इसकी समीक्षा की गई थी और का सार निकालने में सक्षम था कि वे किस तरह Run
https://github.com/aspnet/Hosting/blob/15008b0b7fcb54235a9de3ab844c066aaf42ea44/src/Microsoft.AspNetCore.Hosting/WebHostExtensions.cs#L86
public static class ConsoleHost {
/// <summary>
/// Block the calling thread until shutdown is triggered via Ctrl+C or SIGTERM.
/// </summary>
public static void WaitForShutdown() {
WaitForShutdownAsync().GetAwaiter().GetResult();
}
/// <summary>
/// Runs an application and block the calling thread until host shutdown.
/// </summary>
/// <param name="host">The <see cref="IWebHost"/> to run.</param>
public static void Wait() {
WaitAsync().GetAwaiter().GetResult();
}
/// <summary>
/// Runs an application and returns a Task that only completes when the token is triggered or shutdown is triggered.
/// </summary>
/// <param name="host">The <see cref="IConsoleHost"/> to run.</param>
/// <param name="token">The token to trigger shutdown.</param>
public static async Task WaitAsync(CancellationToken token = default(CancellationToken)) {
//Wait for the token shutdown if it can be cancelled
if (token.CanBeCanceled) {
await WaitAsync(token, shutdownMessage: null);
return;
}
//If token cannot be cancelled, attach Ctrl+C and SIGTERN shutdown
var done = new ManualResetEventSlim(false);
using (var cts = new CancellationTokenSource()) {
AttachCtrlcSigtermShutdown(cts, done, shutdownMessage: "Application is shutting down...");
await WaitAsync(cts.Token, "Application running. Press Ctrl+C to shut down.");
done.Set();
}
}
/// <summary>
/// Returns a Task that completes when shutdown is triggered via the given token, Ctrl+C or SIGTERM.
/// </summary>
/// <param name="token">The token to trigger shutdown.</param>
public static async Task WaitForShutdownAsync(CancellationToken token = default (CancellationToken)) {
var done = new ManualResetEventSlim(false);
using (var cts = CancellationTokenSource.CreateLinkedTokenSource(token)) {
AttachCtrlcSigtermShutdown(cts, done, shutdownMessage: string.Empty);
await WaitForTokenShutdownAsync(cts.Token);
done.Set();
}
}
private static async Task WaitAsync(CancellationToken token, string shutdownMessage) {
if (!string.IsNullOrEmpty(shutdownMessage)) {
Console.WriteLine(shutdownMessage);
}
await WaitForTokenShutdownAsync(token);
}
private static void AttachCtrlcSigtermShutdown(CancellationTokenSource cts, ManualResetEventSlim resetEvent, string shutdownMessage) {
Action ShutDown =() => {
if (!cts.IsCancellationRequested) {
if (!string.IsNullOrWhiteSpace(shutdownMessage)) {
Console.WriteLine(shutdownMessage);
}
try {
cts.Cancel();
} catch (ObjectDisposedException) { }
}
//Wait on the given reset event
resetEvent.Wait();
};
AppDomain.CurrentDomain.ProcessExit += delegate { ShutDown(); };
Console.CancelKeyPress += (sender, eventArgs) => {
ShutDown();
//Don't terminate the process immediately, wait for the Main thread to exit gracefully.
eventArgs.Cancel = true;
};
}
private static async Task WaitForTokenShutdownAsync(CancellationToken token) {
var waitForStop = new TaskCompletionSource<object>();
token.Register(obj => {
var tcs = (TaskCompletionSource<object>)obj;
tcs.TrySetResult(null);
}, waitForStop);
await waitForStop.Task;
}
}
प्रदर्शन किया मैं एक IConsoleHost
की तरह कुछ अनुकूल कोशिश की, लेकिन जल्दी से पता चला मैं इस पर इंजीनियरिंग था। await ConsoleUtil.WaitForShutdownAsync();
की तरह कुछ है कि Console.ReadLine
की तरह संचालित में मुख्य भागों निकाले यह तो अनुमति उपयोगिता वहाँ एक बनाने से इस
public class Program {
public static async Task Main(string[] args) {
//relevant code goes here
//...
//wait for application shutdown
await ConsoleUtil.WaitForShutdownAsync();
}
}
की तरह इस्तेमाल किया जा रहा नीचे दिए गए लिंक के रूप systemd आप आराम मिलना चाहिए रास्ते से
Writing a Linux daemon in C#
ट्यूटोरियल, सॉफ़्टवेयर और दस्तावेज़ीकरण जैसे ऑफ-साइट संसाधनों की मांग करने वाले प्रश्न SO पर यहां विषय बंद हैं। मैं बंद करने के लिए वोट दूंगा, लेकिन बक्षीस इसे रोक रहा है। – JNevill
आपके द्वारा सूचीबद्ध अधिकांश धारणा वास्तव में आधुनिक प्रणालियों पर चिंता नहीं है। सिस्टम प्रबंधकों जैसे सिस्टमड (जो फेडोरा/उबंटू/रेडहाट/सेंटोस/आर्क/अन्य पर प्रयोग किया जाता है) पृष्ठभूमि में चल रही चीजों का ख्याल रखता है, और वास्तव में उन कार्यक्रमों के साथ सबसे अच्छा काम करता है जो बस अग्रभूमि में रहते हैं और कुछ भी करने की कोशिश नहीं करते हैं कांटा() या सिग्नल के साथ फैंसी। – larsks
https://developers.redhat.com/blog/2017/06/07/writing-a-linux-daemon-in-c/ – Nkosi