Welcome to the new FlexRadio Community! Please review the new Community Rules and other important new Community information on the Message Board.
If you are having a problem, please refer to the product documentation or check the Help Center for known solutions.
Need technical support from FlexRadio? It's as simple as Creating a HelpDesk ticket.

VS Examples

Mark Erbaugh
Mark Erbaugh Member ✭✭
edited February 2020 in SmartSDR API
With the FlexLib download is a ComPTT project, which I assume is an example on how to use the library, but it's not enough for me to get started.

I tried to do something simple, like set the title of the main window to the radio's Nickname. In the API_RadioAdded event handler, I added the line Text = radio.nickname, but when I run it, I get an exception about cross-thread operation not valid.

This may be a windows programming thing, rather than a FlexLib thing, but for a newbie to Visual Studio programming a more complete example would be helpful. Maybe someone could even give step by step instructions on how to get started with FlexLib and VS.

One post I found in the forum said to search the forum and there were lots of examples, but I haven't been able to find any.

Answers

  • Pete - W6OP
    Pete - W6OP Member ✭✭
    edited February 2020
    I haven't looked at that program but typical in Windows is having to marshal information back to the GUI thread. The GUI is single threaded so you would typically create a new thread for other processes to run on. When you try to update the GUI from that thread you get the cross thread error.

    I have a little method I use in all my GUI programs to marshal the information back to the GUI.

    private void UpdateLabel(string message)        {
                if (InvokeRequired)
                {
                    this.BeginInvoke(new Action<string>(this.UpdateLabel), message);
                    return;
                }

                LabelProgress.Enabled = true;
                LabelProgress.Visible = true;
                LabelProgress.Text = message;
            }

    Just put your Text = radio.nickname in there instead of what I have.

    73,
    Pete
  • Mark Erbaugh
    Mark Erbaugh Member ✭✭
    edited August 2019
    Pete,

    Thanks for the reply. I found some stuff on MS' website that used a delegate. Is that different from the Action you used? Does the Action have to be allocated each time, or could you allocate it once and re-use it?
  • Pete - W6OP
    Pete - W6OP Member ✭✭
    edited August 2019
    A don't believe a delegate is what you want. With the method I provided you need to call it every time you update the GUI. If you have multiple labels you want to update you can always put a switch statement in it. You can change the method signature as needed.

     private void UpdateListViewLoad(string message, string status, bool clear)

            {

                if (InvokeRequired)

                {

                    this.BeginInvoke(new Action<string, string, bool>(this.UpdateListViewLoad), message, status, clear);

                    return;

                }

    // syntax approximate - I switch between Swift and C# all day and its hard to keep it 

    // straight sometimes

    switch (status) { 

    case "active":

    ... do something

    case "inactive":

    ... do something

    }

    Actually I would use an enum for status.

  • Pete - W6OP
    Pete - W6OP Member ✭✭
    edited August 2019
    I'm not sure you need to remove the property change handler. C# does a pretty good job of cleaning up after itself. Probably the property changed after the handler was removed. You may need to disconnect or dispose of the radio object or whatever is changing the property. If I have time I'll look at the code Flex provided.
  • Mark_WS7M
    Mark_WS7M Member ✭✭✭
    edited August 2019
    I just posted in another thread about this.  It depends completely on your use.  If the event handler remains alive during the entire life of your app then not removing it is fine.  

    But if you open a window, then close it and the open sets an event handler but does not remove it.  I have seen some VERY STRANGE behavior with this scenario.
  • Mark_WS7M
    Mark_WS7M Member ✭✭✭
    edited August 2019
    Mark,

    Feel free to email me at ws7m@arrl.net if you need some help at times.
  • Pete - W6OP
    Pete - W6OP Member ✭✭
    edited August 2019
    Yes, I should have given a more comprehensive explanation.
  • Mark Erbaugh
    Mark Erbaugh Member ✭✭
    edited August 2019
    What's the difference between Action and delegate. If you do a Peek definition on Action, it displays code from System.Core that just says public delegate void Action()

    Secondly, your code calls new every time you call this.BeginInvoke. Would it be more resource efficient to create a variable of type Action<string,string,bool> and do the new once in the object's constructor and then just reference that object?

  • Pete - W6OP
    Pete - W6OP Member ✭✭
    edited August 2019
    You know, I’ve been using that code for so long I have never thought much about it. That would be a good question to post on stackoverflow. As far as efficiency, if it’s not in a loop it really doesn’t matter much.

Leave a Comment

Rich Text Editor. To edit a paragraph's style, hit tab to get to the paragraph menu. From there you will be able to pick one style. Nothing defaults to paragraph. An inline formatting menu will show up when you select text. Hit tab to get into that menu. Some elements, such as rich link embeds, images, loading indicators, and error messages may get inserted into the editor. You may navigate to these using the arrow keys inside of the editor and delete them with the delete or backspace key.