Domanda

Voglio correre un processo programaticaly in C #. con Process.Start posso farlo. ma come posso prommt utente quando il processo richiede un certo input dell'utente in mezzo e continuare ancora dopo aver fornito l'ingresso.

È stato utile?

Altri suggerimenti

Ecco un articolo bene su esecuzione di un processo in modo sincrono e asincrono da c #.

È possibile aggiungere un gestore di eventi per l'evento OutputDataReceived. Questo viene chiamato ogni volta che il processo scrive alcuni dati al suo flusso di output reindirizzato.

private StreamWriter m_Writer;

public void RunProcess(string filename, string arguments)
{
    ProcessStartInfo psi = new ProcessStartInfo();
    psi.FileName = filename;
    psi.Arguments = arguments;
    psi.RedirectStandardInput = true;
    psi.RedirectStandardOutput = true;
    psi.UseShellExecute = false;

    Process process = Process.Start(psi);
    m_Writer = process.StandardInput;
    process.EnableRaisingEvents = true;
    process.OutputDataReceived += new DataReceivedEventHandler(OnOutputDataReceived);
    process.BeginOutputReadLine();
}

protected void OnOutputDataReceived(object sender, DataReceivedEventArgs e)
{
    // Data Received From Application Here
    // The data is in e.Data
    // You can prompt the user and write any response to m_Writer to send
    // The text back to the appication
}

In aggiunta v'è anche un evento Process.Exited per rilevare se le vostre uscite di processo.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top