|
-
August 16th, 2007, 11:35 AM
#1
New to C#, having trouble with an event not firing...
I am trying to write a basic SSH terminal, and am using the SSH class available on code guru. I am also very new to C# so my structure and organzation might be a bit screwy. Anyway, I have this multi-threaded set of functions using the BackgroundWorker, and I can't get a certain event to fire (or so it seems). Here are the code segments:
Code:
protected BackgroundWorker SSHSession = new BackgroundWorker();
protected SshShell shell = null;
public AutoTerm()
{
InitializeComponent();
InitSSHSessionWorker();
}
private void InitSSHSessionWorker()
{
SSHSession.DoWork += new DoWorkEventHandler(SSHSession_DoWork);
SSHSession.ProgressChanged += new ProgressChangedEventHandler(SSHSession_ProgressChanged);
}
private void buttonGo_Click(object sender, EventArgs e)
{
buttonGo.Enabled = false;
SSHSession.RunWorkerAsync();
}
private void TermBox_KeyPress(object sender, KeyPressEventArgs e)
{
if (null != shell)
{
if (true == shell.Connected)
{
shell.Write(e.KeyChar.ToString());
}
}
}
private void RunSSH(BackgroundWorker Worker)
{
shell = new SshShell("x.x.x.x", "cat");
shell.Password = "dog";
shell.Connect();
string Input;
while (true == shell.Connected)
{
Input = shell.Expect("\n");
Worker.ReportProgress(5, Input);
}
}
// This event handler updates the progress bar.
private void SSHSession_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
TermBox.Text += e.UserState as string;
}
private void SSHSession_DoWork(object sender, DoWorkEventArgs e)
{
RunSSH(sender as BackgroundWorker);
}
Now what happens is that everything seems fine until the line in RunSSH() gets to "Worker.ReportProgress(5, Input);"
At that line, Input has actual text in it that looks right. When I execute the line, nothing happens. Specifically the break point I put on "TermBox.Text += e.UserState as string;" never gets hit.
So it seems that the event for ProgressReport never fires even though I am calling the method on Worker. What am I doing wrong?
-
August 17th, 2007, 01:24 AM
#2
Re: New to C#, having trouble with an event not firing...
Hi,
If you r using some of control properties inside the background worker thread DoWork event, It won't work properly. I faced same type of issues using other control properties inside DoWork Event.
For Example If you want using TextBox.Text inside DoWorkEvent Try this
approach
Code:
private int m_lTemp="";
button_Click(object sender, EventArgs e)
{
m_lTemp=Convert.ToInt32(TextBox.Text);
backgroundWorker1.RunWorkerAsync();
}
void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
If(m_lTemp>10)
//Do Some thing
}
Avoid usng control properties directly in thread events
Regards
Ravi.Battula
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|