Problem calling .focu() when in worker thread.

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am writing a multi-threaded program with a worker thread. I want the rich text box to auto scroll down as the text is added.
Here is what I want to happen:
From the worker thread:
1. Add the text to a richtextbox named txtMessageBody.
2. Make sure that the scroll is to the bottom of the box.
3. Put the focus back on the box where the user enters text

This is from a basic messenger I am writing.

The problem only occurs when trying to do this from the worker thread. Please help!

Here is a section of my code:

this.txtMessageBody.Invoke(new workerFillMessages(this.updateMessageBox),new object [] {s1});
this.txtMessage.Invoke(new workerFocusTxtMessage(this.focusTxtMessage),new object [] {});
}

}
this.tvContacts.Invoke(new workerFillContacts(this.fillContacts));
}
}

//worker thread functions
public delegate void workerFillContacts();

public delegate void workerFillMessages(string s);

public delegate void workerSwitchTabs(int i);

public delegate void workerFillMembers();

public delegate void workerFocusTxtMessage();

//the functions

public void updateMessageBox(string s)
{
this.txtMessageBody.Focus();
this.txtMessageBody.Text += s;
this.txtMessageBody.ScrollToCaret();
}

private void focusTxtMessage()
{
this.txtMessage.Focus();
}
 
And what exactly is the problem?

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

Dean Bortell said:
I am writing a multi-threaded program with a worker thread. I want the
rich text box to auto scroll down as the text is added.
Here is what I want to happen:
From the worker thread:
1. Add the text to a richtextbox named txtMessageBody.
2. Make sure that the scroll is to the bottom of the box.
3. Put the focus back on the box where the user enters text

This is from a basic messenger I am writing.

The problem only occurs when trying to do this from the worker thread. Please help!

Here is a section of my code:

this.txtMessageBody.Invoke(new
workerFillMessages(this.updateMessageBox),new object [] {s1});
this.txtMessage.Invoke(new workerFocusTxtMessage(this.focusTxtMessage),new object [] {});
}

}
this.tvContacts.Invoke(new workerFillContacts(this.fillContacts));
}
}

//worker thread functions
public delegate void workerFillContacts();

public delegate void workerFillMessages(string s);

public delegate void workerSwitchTabs(int i);

public delegate void workerFillMembers();

public delegate void workerFocusTxtMessage();

//the functions

public void updateMessageBox(string s)
{
this.txtMessageBody.Focus();
this.txtMessageBody.Text += s;
this.txtMessageBody.ScrollToCaret();
}

private void focusTxtMessage()
{
this.txtMessage.Focus();
}
 
The problem is that the richtext box scroll doesn't go to the bottom and the .focus() event does not work. No exceptions are thrown, but it doesn't work. I have done this code on the parent thread and it works perfectly. The problem is ONLY when using a non-parent thread. The problem is very frustrating.
 
Hi Dean,

Does it reach updateMessageBox method at all?
Did you try to set breakpoint there.
Another simple test would be to check the NeedInvoke (just to be on the safe
side) property when in updateMessageBox method.

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com


Dean Bortell said:
The problem is that the richtext box scroll doesn't go to the bottom and
the .focus() event does not work. No exceptions are thrown, but it doesn't
work. I have done this code on the parent thread and it works perfectly. The
problem is ONLY when using a non-parent thread. The problem is very
frustrating.
Dean Bortell said:
I am writing a multi-threaded program with a worker thread. I want the rich text box to auto scroll down as the text is added.
Here is what I want to happen:
From the worker thread:
1. Add the text to a richtextbox named txtMessageBody.
2. Make sure that the scroll is to the bottom of the box.
3. Put the focus back on the box where the user enters text

This is from a basic messenger I am writing.

The problem only occurs when trying to do this from the worker thread. Please help!

Here is a section of my code:

this.txtMessageBody.Invoke(new workerFillMessages(this.updateMessageBox),new object [] {s1});
this.txtMessage.Invoke(new workerFocusTxtMessage(this.focusTxtMessage),new object [] {});
}

}
this.tvContacts.Invoke(new workerFillContacts(this.fillContacts));
}
}

//worker thread functions
public delegate void workerFillContacts();

public delegate void workerFillMessages(string s);

public delegate void workerSwitchTabs(int i);

public delegate void workerFillMembers();

public delegate void workerFocusTxtMessage();

//the functions

public void updateMessageBox(string s)
{
this.txtMessageBody.Focus();
this.txtMessageBody.Text += s;
this.txtMessageBody.ScrollToCaret();
}

private void focusTxtMessage()
{
this.txtMessage.Focus();
}
 
The problem is that the richtext box scroll doesn't go to the bottom and
the .focus() event does not work. No exceptions are thrown, but it doesn't
work. I have done this code on the parent thread and it works perfectly.
The problem is ONLY when using a non-parent thread. The problem is very frustrating.


That's because the Windows GUI is not thrtead safe and any instructions/changes
to a control must be issued from the Thread which created the control:
in fact, your process has one thread only (the STAThread) which can safely
play with controls.
Look into the Control.Invoke method for cross-thread access to controls.
 
Back
Top