C#等待用户输入然后继续代码 [英] C# Wait for user input then continue with code

查看:327
本文介绍了C#等待用户输入然后继续代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只对C#语言有基本的了解,但我很困扰,我的程序使用Powershell来执行任务。



我的具体问题是我无法附上另一个事件处理程序,可以在用户点击按钮后收听按键,我会解释。



1.用户点击按钮

2.网络跟踪开始使用powershell(用户控制何时停止跟踪)

3.我的临时解决方案是在关闭时显示消息框..

3a(我希望用户按空间继续第4步)

4.停止网络跟踪



是无论如何我可以替换我的消息框,用户可以按空格键来停止跟踪吗?



谢谢你的时间和提前帮助。



I only have a basic understanding of the C# language but I am troubled, my program uses Powershell to perform tasks.

My specific problem is I am unable to attach an another event handler which can listen for a key press after the user has clicked a button, I'll explain.

1. User clicks button
2. Network trace begins using powershell (a user controls when to stop the trace)
3. My temp solution is to display a messagebox, when this is closed..
3a (i would the user to hit space to continue to step 4)
4. Stop the network trace

Is there anyway I can replace my message box where a user can press space to stop the trace?

Thankyou for your time and help in advance.

public void button3_Click(object sender, EventArgs e)
    {

///////////

ps.AddScript("netsh trace start persistent=yes capture=yes tracefile=" + progpath + @"\nettrace.etl");

ps.Invoke();

MessageBox.Show("Press OK to stop the trace");

ps.AddScript("netsh trace stop");

/////////

}

推荐答案

如果这是一个winforms应用程序(MessageBox.Show表示它是),那么命中空格将触发事件取决于哪个控件具有焦点。

if this is a winforms application (the "MessageBox.Show" says that it is) then hitting space will trigger an event depending on which control has focus.
private void form1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar == ' ')
       ps.AddScript("netsh trace stop");
}





问题是你已声明不能接受新事件。我想这是因为button3_Click事件继续运行一段时间,阻止其他事件。



我建议你看一下线程和异步方法。这里有一种方式(未经过全面测试)





The problem is that you have stated that you cannot accept new events. I imagine that this is because the button3_Click event continues to run for some time, blocking other events.

I suggest you look at Threading and Async methods. Here one way (not fully tested)

private PowerShell ps = new PowerShell { };
private BackgroundWorker backgroundWorker = new BackgroundWorker();
private bool isPsRunning = false;

public void button3_Click(object sender, EventArgs e)
{
    backgroundWorker.DoWork +=backgroundWorker_DoWork;
    backgroundWorker.RunWorkerCompleted += backgroundWorker_RunWorkerCompleted;
    backgroundWorker.RunWorkerAsync();

}
void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    isPsRunning = false;
}
void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
    isPsRunning = true;
    ps.AddScript("netsh trace start persistent=yes capture=yes tracefile=" + progpath + @"\nettrace.etl");
}
private void form1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (isPsRunning && e.KeyChar == ' ')
        ps.AddScript("netsh trace stop");
}


这篇关于C#等待用户输入然后继续代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆