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();
}
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();
}