Hi People

I have a strange problem while using a delegate to update a textbox. I am using an SDK from Swyx, which is a VOIP (telephone software) package.

The activeX triggers an onChnage event which has an event handler attached. I can change properties within my class using the delegate and even call functions, I cannot however change anything related to a UI component. It's very strange as only this SDK seems to have the probelm.

OnSwyxEvent is the method which is causing the problem.

Does anyone know of any reason why this would happen? Could the ActiveX I am using not have access to the front end for some reason?

I have included my code below:

namespace swyxApp
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        Swyx.swyxIt SwyxInstance = new Swyx.swyxIt();
        //UltraSpace.UltraControl UltraInstance = new UltraSpace.UltraControl();

        public ClientLineMgrClass clmgr;

        public String myVar = "hello";

        public Window1()
        {
            InitializeComponent();
            clmgr = new ClientLineMgrClass();
            myVar = "mat";
            
            IClientLineMgrEventsPub_PubOnLineMgrNotificationEventHandler EvtHndlr = new IClientLineMgrEventsPub_PubOnLineMgrNotificationEventHandler(OnSwyxEvent);
            clmgr.PubOnLineMgrNotification += EvtHndlr;
   
            /*  foreach (ListViewItem item in SwyxInstance.getUsers("matthew")){
                  listBox1.Items.Add(item);
              }

              foreach (ListViewItem item in SwyxInstance.getNumbers(90)){
                  listBox2.Items.Add(item);
              }

               foreach (ListViewItem item in SwyxInstance.getPublicNumbers(87)){
                   listBox3.Items.Add(item);
               }*/
        }

        public void tryThis()
        {
            //MessageBox.Show("hello");
            this.textBox1.Text = "hrhrh";
        }

        public void OnSwyxEvent(int msg, int param)
        {
            tryThis();
            this.textBox1.Text = "don't care"; // does not work
            myVar = "new val"; // works
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            //UltraInstance.disconnect();
            textBox1.Text = myVar;
        }
    }
}

Thanks

Matthew

Have you tried anything like this? If the library executes the event on a different thread you will get an error when you try to access UI components as they are on a different thread.

public void OnSwyxEvent(int msg, int param)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new Action<int, int>(OnSwyxEvent), msg, param);
            }
            else
            {
                tryThis();
                this.textBox1.Text = "don't care";
                myVar = "new val";
            }
        }

Only use Invoke on UI threads otherwise use BeginInvoke.

commented: you got it! +6

Have you tried anything like this? If the library executes the event on a different thread you will get an error when you try to access UI components as they are on a different thread.

public void OnSwyxEvent(int msg, int param)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new Action<int, int>(OnSwyxEvent), msg, param);
            }
            else
            {
                tryThis();
                this.textBox1.Text = "don't care";
                myVar = "new val";
            }
        }

Only use Invoke on UI threads otherwise use BeginInvoke.

Thanks for the code, this.Invoke is not recognised by my application. Is this due to using WPF? There is a Dispatcher.Invoke

I'm new to using Threads so am trying to be careful!

I'm not that familiar with WCF but It looks like you are looking in the right direction.

This tutorial appears to cover what you are looking at:
http://www.switchonthecode.com/tutorials/working-with-the-wpf-dispatcher

At a glance I think it is nearly exactly the same you just use Dispatcher.Invoke instead of this.Invoke and Dispatcher.CheckAccess() instead of this.InvokeRequired but the tutorial is still worth a read.

I'm not that familiar with WCF but It looks like you are looking in the right direction.

This tutorial appears to cover what you are looking at:
http://www.switchonthecode.com/tutorials/working-with-the-wpf-dispatcher

At a glance I think it is nearly exactly the same you just use Dispatcher.Invoke instead of this.Invoke and Dispatcher.CheckAccess() instead of this.InvokeRequired but the tutorial is still worth a read.

I managed to get it working by starting a new thread as below, however the Dispatcher methods look to be spot on.

Thanks for the advice, much appreciated!

public void OnSwyxEvent(int msg, int param)
      {
          System.Threading.Thread thread = new System.Threading.Thread(
        new System.Threading.ThreadStart(delegate()
       {
           textBox1.Dispatcher.Invoke(
             System.Windows.Threading.DispatcherPriority.Normal,
             new Action(
               delegate()
               {
                   textBox1.Text = "hello from threading";
               }
           ));
       }
   ));
          thread.Start();
          thread.Abort();
      }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.