X10 Community Forum

🖥️ActiveHome Pro => SDK => Topic started by: grasshopah on August 07, 2012, 09:40:14 PM

Title: C# sample sdk program error
Post by: grasshopah 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
Title: Re: C# sample sdk program error
Post by: Tuicemen 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.
Title: Re: C# sample sdk program error
Post by: grasshopah 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
Title: Re: C# sample sdk program error
Post by: localuser 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");
           
        }
Title: Re: C# sample sdk program error
Post by: grasshopah on August 19, 2012, 10:58:21 AM
Thank you localuser #:)

With that bit of code, the program worked.