Hello!
I have a problem - I'm trying to create an application in J# (Visual Studio 2005) - it's connected with socket communication - 2 aplications - client and server.
In console I didn't have any problems and I think that I understand the mechanism. But now I'd like to create my application in graphical environment (GUI). And here I have a problem.
So what's the point - I have 3 classes:
1. Form1
2. Server (it extends Form1)
3. Program (it initializes class 1 and 2)
In Server class, there is a constructor in which is a method that is waiting for client connection.Also in Server class there is internal class which has 2 threads - sending thread and listening thread.
Now, what I want to, is to set_Text in textBox1 (initialized in class Form1) with the help of a line ex. textBox1.set_Text("bla bla"); from one of threads or from constructor of Server class. But it doesn't work properly - it doesn't change the text of textBox. Do you have any ideas
I need this - it will help me to put received messages in listening thread from client in textBox etc. Generally I'm wondering how to put received messages from stream (listening thread) and send messages from textBox (sending thread).
See you!
new
Thread() //it's from internal class of Server class{
public void run()
{
while(isconnected){
try
{
while ((message = fromClient.readLine()) != null)
{
textBox1.AppendText(message + "\n");
//System.out.println(message); - that was in console
}
}
}catch (IOException ioe) {};
}
}.start();
new
Thread(){
public void run()
{
while ((out_message = textBox1.get_Text())!=null) //keyboard.readLine()) != null)//"Message from Server: " + out_message);{
toClient.println(
toClient.flush();
textBox1.set_Text(
"");}
}}.start();

[J#] Problem with changing text in textBox & socket communication
GROTH
Hi,
In .net 2.0, one thread can't change the text property of a UI element created by another thread. For doing so, the other thread need to call the BeginInvoke() method on that UI element.
For details please refer this link. It has a sample code where one thread manipulates the UI controls created by another thread.
Please post back if you face any issues.
Thanks.
jenniMD
I have the same problem but I can't make it work. What am I doing wrong
private delegate void GUIDelegate(OrderEvent event);
public void onOrderUpdated(OrderEvent event)
{
if (get_InvokeRequired()) {
GUIDelegate dlgt = new GUIDelegate(onOrderUpdatedOnGUI);
BeginInvoke(dlgt, new Object[] { event });
}
else
onOrderUpdatedOnGUI(event);
}
public void onOrderUpdatedOnGUI(OrderEvent event) {
this.set_Text("Order updated");
}
The error I get is :
Cannot find method 'BeginInvoke(GUIDelegate, Object[])' in 'JSharpInvoke.Form1' C:\develop\projects\JSharpInvoke\Form1.jsl 40 4 JSharpInvoke
It seems as if the delegate isn't a subclass of System.Delegate...
groberts1980
Try
/** @delegate*/
private delegate void GUIDelegate(OrderEvent event);if you specify @delegate attribute then GUIDelegate will be derived from System.MulticastDelegate otherwise it will be com.ms.lang.Delegate.
Thanks,
Jani N
So finally I did it!
it's the magic key:
/** @delegate */
private delegate void AddMessageDelegate(String message);
public void AddMessage(String message)< xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
{
if (label2.get_InvokeRequired())
{
AddMessageDelegate addHandler = new AddMessageDelegate(AddMessage);
label2.BeginInvoke(addHandler, new Object[] { message });
}
else
{
label2.set_Text(message);
}
}
We create delegate in Form1 - and now we can manipulate controls from the other thread than they were made with the help of method AddMessage(String message). For example in thread which we can use to send messages in socket communicator.