最近我一直在將我的 node.js 機器人轉換為 C#,但我遇到了一個問題,因為一些奇怪的原因public async Task InitCloudCompute()阻止了我的其他代碼運行。我不知道為什么會發生這種情況,而且我對 C# 還很陌生。如果有人能幫我解決這個問題,我將不勝感激。:)
編輯:我忘了說程式啟動了,因為我可以在瀏覽器中打開本地主機。
using System;
using System.Diagnostics;
using System.Reflection;
using System.Threading.Tasks;
using Discord;
using Discord.Commands;
using Discord.WebSocket;
using Microsoft.Extensions.DependencyInjection;
using static Doom.Grid.Bot.Utility.ConsoleGlobals;
namespace Doom
{
class Program
{
static void Main(string[] args) => new Program().InitBotAsync()
.GetAwaiter().GetResult();
private DiscordSocketClient _client;
private CommandService _commands;
private IServiceProvider _services;
public async Task InitBotAsync()
{
_client = new DiscordSocketClient();
_commands = new CommandService();
_services = new ServiceCollection()
.AddSingleton(_client)
.AddSingleton(_commands)
.BuildServiceProvider();
_client.Log = _client_internal_Log;
await InitCloudCompute();
await RegisterCommandsAsync();
await _client.LoginAsync(TokenType.Bot, "Nope"); // TODO: Remove
// hard-coded token
await _client.StartAsync();
await Task.Delay(-1);
}
public async Task InitCloudCompute()
{
Console.WriteLine($"{CCServiceInternalLog} Initializing CCService");
ProcessStartInfo processStartInfo;
Process process;
processStartInfo = new ProcessStartInfo
{
CreateNoWindow = true,
RedirectStandardOutput = true,
RedirectStandardInput = true,
UseShellExecute = false,
Arguments = "-console -verbose 5000",
FileName = "D:\\21CC\\CC-FE4FEB09A756\\CCService.exe"
};
process = new Process
{
StartInfo = processStartInfo,
EnableRaisingEvents = true
};
process.OutputDataReceived = new DataReceivedEventHandler
(
delegate (object sender, DataReceivedEventArgs e)
{
Console.WriteLine($"{CCServiceInternalLog} {e.Data.ToString()}");
}
);
process.Start();
process.BeginOutputReadLine();
process.WaitForExit();
process.CancelOutputRead();
}
private Task _client_internal_Log(LogMessage arg)
{
Console.WriteLine($"{DiscordInternalLog} {arg}");
return Task.CompletedTask;
}
public async Task RegisterCommandsAsync()
{
_client.MessageReceived = HandleCommandAsync;
await _commands.AddModulesAsync(Assembly.GetEntryAssembly(), _services);
}
private async Task HandleCommandAsync(SocketMessage arg)
{
var message = arg as SocketUserMessage;
var context = new SocketCommandContext(_client, message);
if (message.Author.IsBot) return;
int argPos = 0;
if (message.HasStringPrefix(">", ref argPos))
{
var result = await _commands.ExecuteAsync(context, argPos, _services);
if (!result.IsSuccess) Console.WriteLine(result.ErrorReason);
}
}
}
}
uj5u.com熱心網友回復:
根據此處的檔案https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.process.waitforexit?view=net-6.0 Process.WaitForExit 將阻塞當前執行緒。
“設定等待關聯行程退出的時間段,并阻塞當前執行執行緒,直到時間過去或行程退出。為避免阻塞當前執行緒,請使用 Exited 事件。”
Task.GetAwaiter 的檔案同樣說它通常用于編譯器而不是應用程式使用。 https://docs.microsoft.com/en-us/dotnet/api/system.threading.tasks.task.getawaiter?view=net-6.0
所以你可以創建一個新任務并像這樣運行它
任務任務 = Task.Run({new Program().InitBotAsync()});
而不是 process.WaitForExit() 嘗試
process.Exited = new EventHandler(yourHandlerHere);
進行清理
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/421105.html
標籤:
