Gui & Thread & Invoke = Nothing

G

Guest

Hi,

I have problems to update Label.Text from an event which is fired from
worker thread which is created in an instance of an object . I'm using
form.Invoke() !

Here's my code:


------------ Class Code ---------------
public delegate void DataSetEventHandler (System.Data.DataSet dataset);
public event DataSetEventHandler ReceivingDataSetFromClient;

//Construktor
public Server()
{
Thread listenThread = new Thread(new ThreadStart(Listen));
listenThread.Start();
}

//Method
protected void Listen()
{
while (this.clientSockStream != null)
{
if (this.clientSockStream.DataAvailable)
{
if (this.clientStreamReader != null)
{
string xmlString = this.clientStreamReader.ReadLine();
if (xmlString != null)
{
byte[] buffer = System.Text.ASCIIEncoding.ASCII.GetBytes(xmlString);

XmlTextReader xmlTextReader = new XmlTextReader(new
MemoryStream(buffer, 0, buffer.Length));
DataSet ds = new DataSet();
ds.ReadXml(xmlTextReader);

//Fire Event
this.ReceivingDataSetFromClient(ds);
}
}
}
else
{
Thread.Sleep(50);
}
}
}
-------------------------------------



-------------- Form Code -------------
private string str ="";

//Eventhandler
private void server_ReceivingDataSetFromClient (DataSet dataset)
{
this.str = DateTime.Now.ToLongTimeString()+": Neue Aufträge empfangen.\r\n";
this.Invoke(new EventHandler(AppendLog));
}

//Function
public void AppendLog(object sender, EventArgs e)
{
this.lblStatus.Text = this.str;
this.lblStatus.Refresh();
}
 
A

Alex Feinman [MVP]

Nothing except I would use Application.DoEvents() instead of Label.Refresh()
Of course you did not mention what exactly is the problem
 
G

Guest

My exactly problem:
The label get's not updated (breakpoint at "this.lblStatus.Text = this.str;"
will not be reached) and the thread is going to freeze or stop or locked or
what ever after calling "this.Invoke(new EventHandler(AppendLog));". None
Execption is thrown!

If I do "MessageBox.Show();" instread of "this.Invoke();", the thread works
as I will!

Application.DoEvents(); will not work.




Alex Feinman said:
Nothing except I would use Application.DoEvents() instead of Label.Refresh()
Of course you did not mention what exactly is the problem

WebJumper said:
Hi,

I have problems to update Label.Text from an event which is fired from
worker thread which is created in an instance of an object . I'm using
form.Invoke() !

Here's my code:


------------ Class Code ---------------
public delegate void DataSetEventHandler (System.Data.DataSet dataset);
public event DataSetEventHandler ReceivingDataSetFromClient;

//Construktor
public Server()
{
Thread listenThread = new Thread(new ThreadStart(Listen));
listenThread.Start();
}

//Method
protected void Listen()
{
while (this.clientSockStream != null)
{
if (this.clientSockStream.DataAvailable)
{
if (this.clientStreamReader != null)
{
string xmlString = this.clientStreamReader.ReadLine();
if (xmlString != null)
{
byte[] buffer = System.Text.ASCIIEncoding.ASCII.GetBytes(xmlString);

XmlTextReader xmlTextReader = new XmlTextReader(new
MemoryStream(buffer, 0, buffer.Length));
DataSet ds = new DataSet();
ds.ReadXml(xmlTextReader);

//Fire Event
this.ReceivingDataSetFromClient(ds);
}
}
}
else
{
Thread.Sleep(50);
}
}
}
-------------------------------------



-------------- Form Code -------------
private string str ="";

//Eventhandler
private void server_ReceivingDataSetFromClient (DataSet dataset)
{
this.str = DateTime.Now.ToLongTimeString()+": Neue Aufträge
empfangen.\r\n";
this.Invoke(new EventHandler(AppendLog));
}

//Function
public void AppendLog(object sender, EventArgs e)
{
this.lblStatus.Text = this.str;
this.lblStatus.Refresh();
}
 
G

Guest

Once again I solved my problem myself:
Installing "Microsoft .Net Compact Framework 1.0 SP3" has solved all my
problems.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top