468,554 Members | 1,966 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,554 developers. It's quick & easy.

Problem calling .focu() when in worker thread.

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();
}
Nov 16 '05 #1
4 2210
And what exactly is the problem?

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

"Dean Bortell" <Dean Bo*****@discussions.microsoft.com> wrote in message
news:50**********************************@microsof t.com...
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();
}

Nov 16 '05 #2
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" wrote:
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();
}

Nov 16 '05 #3
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" <De*********@discussions.microsoft.com> wrote in message
news:73**********************************@microsof t.com...
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" wrote:
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();
}

Nov 16 '05 #4
On 02 Jul 2004 10:34, Dean Bortell wrote:
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.
--
Simon Smith
simon dot s at ghytred dot com
www.ghytred.com/NewsLook - NNTP Client for Outlook
Nov 16 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Stephen Engle | last post: by
4 posts views Thread by Madhu Gopinathan | last post: by
3 posts views Thread by bilosta | last post: by
reply views Thread by Anton | last post: by
19 posts views Thread by Larry Smith | last post: by
1 post views Thread by raghudr | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by UniDue | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.