Warten Sie auf die Anwendung und warten Sie, bis er asynchronisiert ist/Aufgabe ist

Post a reply

Smilies
:) :( :oops: :chelo: :roll: :wink: :muza: :sorry: :angel: :read: *x) :clever:
View more smilies

BBCode is ON
[img] is ON
[flash] is OFF
[url] is ON
Smilies are ON

Topic review
   

Expand view Topic review: Warten Sie auf die Anwendung und warten Sie, bis er asynchronisiert ist/Aufgabe ist

by Anonymous » 14 Apr 2025, 16:37

Ich habe eine Windows Forms -App, die am Ende eine SteamApp startet. Ich möchte, dass meine App wartet, bis das Spiel gefunden wird und wenn es geschlossen ist, um ein paar weitere Sachen zu machen.

Code: Select all

void LaunchViaSteam()
{
try
{
Process.Start(new ProcessStartInfo
{
FileName = "steam://rungameid/1086940",
UseShellExecute = true,
});
}
catch (Exception ex)
{
MessageBox.Show("An error occurred: " + ex.Message);
}
}
< /code>
Ich habe schon so etwas ausprobiert: < /p>
private async void buttonStart_Click(object sender, EventArgs e)
{
Process process = new Process();
process.StartInfo.FileName = SteamDetection.GetSteamDirectory() + "\\steam.exe";
process.StartInfo.Arguments = "steam://rungameid/xxxxxx";
process.Start();
await process.WaitForExitAsync();
}
< /code>
Aber es funktioniert "nicht wie erwartet". Manchmal wartet es einfach nicht oder es wartet darauf, dass Steam schließt.    async Task WaitForBG3()
{
textBox1.Text = "Searching";
await Task.Run(async () =>
{

bool found = false;
while (!found)
{
Process[] ProcessList = Process.GetProcesses();
foreach (Process p in ProcessList)
{
if (p.ProcessName.Contains("bg3"))
{
found = true;
}
}
}
});
textBox1.Text = "found and wait for Exit";
await Task.Run(async () =>
{
bool running = false;
while (!running)
{
Process[] ProcessList = Process.GetProcesses();
foreach (Process p in ProcessList)
{
if (p.ProcessName.Contains("bg3"))
{
running = false;
}
else
{
running = true;
}
}
}
});
textBox1.Text = "Exit";
}
< /code>
Wenn ich es mit Notepad teste, funktioniert es so, wie es sollte, aber wenn ich den Spielnamen verwende, löst er den sonstigen Teil der 2. Aufgabe installisch aus. Auch wenn das Spiel bei der Aufruf von < /p>
läuft, läuft AllReady läuft    private async void buttonStart_Click(object sender, EventArgs e)
{
//LaunchViaSteam();
await WaitForBG3();
//do some mor stuff
}
Irgendeine Idee, warum?

Top