1

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..
  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");

/////////

}

2 Answers 2

1

Could you have the button act as a toggle? Something like:

private static bool tracing = false;

public void button3_Click(object sender, EventArgs e)
{
    if (tracing)
    {
        ps.AddScript("netsh trace stop");
        button3.Text = "Begin trace";
        ps.Invoke();
    }
    else
    {
        ps.AddScript("netsh trace start persistent=yes capture=yes tracefile=" + 
            progpath + @"\nettrace.etl");
        button3.Text = "Stop trace";
    }     

    tracing = !tracing;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Hi Rufus, Thats certainly an approach I hadn't considered and does indeed do the job! Thankyou for your assistance :)
Hi Rufus, I've unmarked your answer. I would like to go with my original plan! sorry.
1

Answer provided by an Andy Lanng over at code project; http://www.codeproject.com/Questions/892139/Csharp-Wait-for-user-input-then-continue-with-code

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. Hide Copy Code

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)

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

    public void button3_Click(object sender, EventArgs e)
    {

        backgroundWorker.DoWork +=backgroundWorker_DoWork;

        backgroundWorker.RunWorkerAsync();

    }
    void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
    {
        ps.AddScript("netsh trace start persistent=yes capture=yes tracefile=" + progpath + @"\nettrace.etl");

        ps.Invoke();
    }
    private void form1_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar == ' ')
            ps.AddScript("netsh trace stop");
    }

You will need to make a few changes for this to work such as adjusting if statements and where ps is declared.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.