TAN WhoAMI

TAN WhoAMI

  • NA
  • 291
  • 0

How to Update a TextBox (in WPF) From a WCF Service?

Jul 16 2011 10:28 AM
I understand that to update a control from a non-GUI thread, I need to do some kind of thread-marshal that data from a worker thread to the UI thread, so as to avoid any deadlock. But I am not sure in detail how to do it.
I am able to show the MessageBox to display the data each time the data is being updated. But I want to display all those data in a TextBox.

I understand that  [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single,

UseSynchronizationContext = false)]

Must be set and I need to do my own marshalling.

The code is below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ServiceModel;
//using SendingBytesServiceContract;
using System.Windows.Threading;
using System.ComponentModel;
namespace SubscriberONE_GUI
{
  /// <summary>
  /// Interaction logic for MainWindow.xaml
  /// </summary>
  [ServiceContract]
  public interface ISendingBytes
  {
  [OperationContract(IsOneWay = true)]
  void Sending_ByteArray(byte[] data); 
  }
  public class SendingBytesImpl : ISendingBytes
  { 
  public void Sending_ByteArray(byte[] data)
  {
  string output_byte = Conversion.ByteArrayToHexString(data);
  MessageBox.Show(output_byte);
  //textBox1.Text = Conversion.ByteArrayToHexString(data);
  } 
  }
  [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, UseSynchronizationContext = false)]
  public partial class SubscriberONE : Window, ISendingBytes
  {
  ServiceHost svc = new ServiceHost(typeof(SendingBytesImpl));
  public SubscriberONE()
  {
  InitializeComponent(); 
  svc.Open();
  }
 
  private void Window_Loaded(object sender, RoutedEventArgs e)
  { 
  }
 
  private void Window_Closed(object sender, EventArgs e)
  {
  svc.Close();
  } 
  }
}


Answers (1)