C#等待用户输入然后继续代码

编程入门 行业动态 更新时间:2024-10-27 14:22:34
本文介绍了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#等待用户输入然后继续代码

本文发布于:2023-06-08 23:38:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/591043.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:代码   用户

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!