hi, im new here.
i have a thread, with textboxes, i have invoker which invokes when it is required.
when it invokes, the text was able to show, but if it doesn't require invoke, i just wrote
textbox1.Text += "thestring";
but the textbox was unable to show, anyone know how to solve it?
my textbox code it's under a function, where my thread calls it.
public void updateit(string message)
{
if (txtrecieve.IsDisposed) return;
if (txtrecieve.InvokeRequired)
{
invoker = new GeneralDelegate(MethodRunningInMainThread);
txtrecieve.Invoke(invoker, new object[] { message });
}
else
{
txtrecieve.Text += message;
}
}
my thread
private void Listen()
{
Chat_Listen cl = new Chat_Listen(l2);
while (true)
{
myport = cl.returnport();
string message = cl.Listen(pass, salt, init);
string[] part = message.Split(';');
if (part[0] == "sendmsg2")
{
string username = part[2].ToString() + ":" + part[3].ToString();
string msgRecieve = " Says: " + part[1].ToString();
string message1 = username + msgRecieve;
updateit(message1);
}
}
}
the one that activates the thread
private void Chat_Load(object sender, EventArgs e)
{
if (!this.IsHandleCreated)
{
lock (this)
{
this.CreateHandle();
this.CreateControl();
this.CreateGraphics();
}
}
dp_local.Image = local;
//dp_remote.Image = remoteimg;
this.Text = "Talking to: " + remoteNickname;
Sender = new Thread(new ThreadStart(Send));
Listener = new Thread(new ThreadStart(Listen));
//Listener.IsBackground = true;
Listener.Start();
}
thanks.
i have a thread, with textboxes, i have invoker which invokes when it is required.
when it invokes, the text was able to show, but if it doesn't require invoke, i just wrote
textbox1.Text += "thestring";
but the textbox was unable to show, anyone know how to solve it?
my textbox code it's under a function, where my thread calls it.
public void updateit(string message)
{
if (txtrecieve.IsDisposed) return;
if (txtrecieve.InvokeRequired)
{
invoker = new GeneralDelegate(MethodRunningInMainThread);
txtrecieve.Invoke(invoker, new object[] { message });
}
else
{
txtrecieve.Text += message;
}
}
my thread
private void Listen()
{
Chat_Listen cl = new Chat_Listen(l2);
while (true)
{
myport = cl.returnport();
string message = cl.Listen(pass, salt, init);
string[] part = message.Split(';');
if (part[0] == "sendmsg2")
{
string username = part[2].ToString() + ":" + part[3].ToString();
string msgRecieve = " Says: " + part[1].ToString();
string message1 = username + msgRecieve;
updateit(message1);
}
}
}
the one that activates the thread
private void Chat_Load(object sender, EventArgs e)
{
if (!this.IsHandleCreated)
{
lock (this)
{
this.CreateHandle();
this.CreateControl();
this.CreateGraphics();
}
}
dp_local.Image = local;
//dp_remote.Image = remoteimg;
this.Text = "Talking to: " + remoteNickname;
Sender = new Thread(new ThreadStart(Send));
Listener = new Thread(new ThreadStart(Listen));
//Listener.IsBackground = true;
Listener.Start();
}
thanks.
AlanPosted Oct 8, 2008, 5:46 AM
If the problem appears to be that the textbox handle hasn't yet been created then, if InvokeRequired returns false, I'd first check that this is in fact the case by calling the textbox's IsHandleCreated property.
If this is false, you can then force the handle to be created by simply accessing it:
IntPtr hWnd = textbox1.Handle;
It's then safe to set the textbox's Text property using Invoke or BeginInvoke.
Wallance TohPosted Oct 10, 2008, 5:30 AM
i close the form(like you said, it's not been disposed), so i thought that during invoke false when i invoke the message inside my textbox, it should be invoked to the old object, since each time i show the form, i used, form1 f = new form1();
AlanPosted Oct 10, 2008, 5:21 AM
If the form is modal (i.e. a dialog) then, when you close it with the X button, it doesn't actually dispose of the form but just hides it. You can then reshow it by calling ShowDialog() again.
However, if you call Dispose() on the dialog, it will destroy the form object (when the GC gets around to it) and so you'll need to create another instance of the dialog before reshowing it.
If the form is modeless, then closing it also disposes of it.
Wallance TohPosted Oct 10, 2008, 4:28 AM
AlanPosted Oct 9, 2008, 10:16 AM
Wallance TohPosted Oct 8, 2008, 8:30 PM
example
user 1 chats with user 2, user 2, chat window pop up.(invoke require = true)
user 2 closes the chat window.
user 1 chats with user 2 again, user 2 chat window pop up (invoke require = false)
and handles been force created, and after force created, i invoke the textbox, yet the textbox don show any text.
im still figuring how to solve this, help will be appreciate..
Wallance TohPosted Oct 7, 2008, 7:51 PM
thanks,
AlanPosted Oct 7, 2008, 7:09 AM
I don't know why InvokeRequired should return false when it really should be true unless there's some problem with the textbox handle.
However, it will do no harm to always use Invoke when changing the state of a UI element from another thread, which is what I tend to do, whether it's needed or not.