Click here to Skip to main content
15,867,835 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

C#
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");

/////////

}
Posted
Updated 31-Mar-15 3:26am
v2

1 solution

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.
C#
private void form1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar == ' ')
       ps.AddScript("netsh trace stop");
}


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)

C#
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");
}
 
Share this answer
 
v2
Comments
Member 10726551 31-Mar-15 10:31am    
Andy Lanng Thankyou!!

I was working with threading but not background threading, this is a most more elegant solution. I had to make a few changes to your solution to fit my requirements but everything worked as suggested. Now to figure out how to make the key press handler only listen during the background worker process =/

Have a nice day and thankyou so much for your help!
Andy Lanng 31-Mar-15 10:52am    
PS: updated solution ^_^
Member 10726551 31-Mar-15 10:57am    
A more elegant solution to what I had drafted up.

Thankyou again!
Andy Lanng 31-Mar-15 10:47am    
Glad to help. Good luck ^_^

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900