Please login or register.

Login with username, password and session length

Author Topic: C# sample sdk program error  (Read 9332 times)

grasshopah

  • Newbie
  • Helpful Post Rating: 0
  • Posts: 3
C# sample sdk program error
« on: August 07, 2012, 09:40:14 PM »


I am a C# newbie.

When I run the sample C# SDK program I get the error: Cross-thread operation not valid: Control 'StatusTextBox' accessed from a thread other than the thread it was created on.

I did look at the MS documentation but all I could understand was I had to make the  control tread safe.

Has anyone else encountered this and knows how to resolve this

Thanks

Grasshopah
Logged

Tuicemen

  • Administrator
  • Hero Member
  • ****
  • Helpful Post Rating: 283
  • Posts: 10509
  • I don't work for X10, I use it successfuly!
Re: C# sample sdk program error
« Reply #1 on: August 08, 2012, 06:29:02 PM »

Running the sample program in debug mode should cause the problem line of code to be stopped at.
Once you know exactly where (what line of code) in the code is causing this error you can add a try catch statement or remove the line of code if it isn't needed.
Logged
Please Read Topic:
General Forum Etiquette
Before you post!

grasshopah

  • Newbie
  • Helpful Post Rating: 0
  • Posts: 3
Re: C# sample sdk program error
« Reply #2 on: August 09, 2012, 07:41:43 PM »


The error/warning happens in the OnActiveHomeRecv function at the line  StatusTextBox.AppendText(strMsg);

The error happens when the CM15a receives an event and the program tries to add the test to the Status box. C# halts because a thread other than the one that created the form element is trying to update the form element.

So the questions are 1) How do I make a program a single threaded program or how do I modify the program to allow any thread to update the status box.

I have never programmed a multi-threaded app , so this is new territory for me
Logged

localuser

  • Full Member
  • ***
  • Helpful Post Rating: 4
  • Posts: 31
Re: C# sample sdk program error
« Reply #3 on: August 13, 2012, 01:37:03 AM »

That is some pretty old code and due to security issues with the .net 1.1 framework, microsoft no longer allows threads to communicate with the form controls.  You will need to replace the code with something like this:

Code: [Select]
        delegate void SetOnActiveHomeRecv(object strAction, object Address, object Command, object Reserved1, object Reserved2, object Reserved3, object Reserved4);
        private void OnActiveHomeRecv(object strAction, object Address, object Command, object Reserved1, object Reserved2, object Reserved3, object Reserved4)
        {
           
            if (this.StatusTextBox.InvokeRequired) {
            SetOnActiveHomeRecv d = new SetOnActiveHomeRecv(OnActiveHomeRecv);

            this.Invoke(d, new object[] { strAction, Address, Command, Reserved1, Reserved2, Reserved3, Reserved4 });

            return;
            }
           
           
            String strMsg = "";

            if (((String)strAction).ToUpper() == "RECVPLC")
                strMsg += "Recieved Powerline Command:";

            strMsg += " " + Address.ToString().ToUpper();
            strMsg += " " + Command.ToString().ToUpper();

            if (Reserved1.ToString().Length > 0)
                strMsg += " " + Reserved1.ToString().ToUpper();

            if (Reserved2.ToString().Length > 0)
                strMsg += " " + Reserved2.ToString().ToUpper();

            if (Reserved3.ToString().Length > 0)
                strMsg += " " + Reserved3.ToString().ToUpper();

            if (Reserved3 != null && Reserved3.ToString().Length > 0)
                strMsg += " " + Reserved3.ToString().ToUpper();

            StatusTextBox.AppendText(strMsg + "\r\n");
           
        }
Logged

grasshopah

  • Newbie
  • Helpful Post Rating: 0
  • Posts: 3
Re: C# sample sdk program error
« Reply #4 on: August 19, 2012, 10:58:21 AM »

Thank you localuser #:)

With that bit of code, the program worked.
Logged
 

X10.com | About X10 | X10 Security Systems | Cameras| Package Deals
© Copyright 2014-2016 X10.com All rights reserved.