开发者

Show data sent to WCF in windows form

I am hosting a WCF service in windows application, the WCF service receives messages from another windows service and i need to show these messages in text box inside the opened form

How can i implement that ? i am using the following code but it doesn't work:

The form code :

public partial class Form1 : Form
{
private ServiceHost Host;

public Form1()
{
    InitializeComponent();          
}  

private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
    Host.Close();
}

public void WriteMessage(string message)
{
    richTextBox1.Text += message;
    richTextBox2.Text = message;
}

private void StartBtn_Click(object sender, EventArgs e)
{
    Host = new ServiceHost(typeof(MonitoringData));
    Host.Open();
    button1.Enabled开发者_StackOverflow = false;
}
}

The WCF service code :

public class MonitoringData : IMonitoringData
{
public void DoWork(string message)
{
    Form1 monitorForm = new Form1();
    monitorForm.WriteMessage(message);            
}
}

The form consists from 2 textboxes and one button that start WCF service

Thanks in advance


I think your problem stems from the DoWork method, particularly this line

Form1 monitorForm = new Form1();

You are instantiating a new Form1 object simply to call the WriteMessage method but you never show the form, it simply falls out of scope. If you have an existing reference to a Form1 class then you should be passing that to call the WriteMessage method on the existing form.


I have solved it, i used the following code in the WCF service and it works correctly :

public class MonitoringData : IMonitoringData
{
   public void DoWork(string message)
   {

       Form1 monitorForm = (Form1)System.Windows.Forms.Application.OpenForms[0];
       monitorForm.WriteMessage(message);            
   }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜