VS Examples

  • 2
  • Question
  • Updated 2 months ago
  • (Edited)
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.
Photo of Mark Erbaugh

Mark Erbaugh

  • 512 Posts
  • 43 Reply Likes

Posted 2 months ago

  • 2
Photo of Pete - W6OP

Pete - W6OP

  • 38 Posts
  • 13 Reply Likes
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
Photo of Mark Erbaugh

Mark Erbaugh

  • 512 Posts
  • 43 Reply Likes
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?
Photo of Pete - W6OP

Pete - W6OP

  • 38 Posts
  • 13 Reply Likes
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.

Photo of Mark Erbaugh

Mark Erbaugh

  • 512 Posts
  • 43 Reply Likes
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?
Photo of Pete - W6OP

Pete - W6OP

  • 38 Posts
  • 13 Reply Likes
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.
Photo of Pete - W6OP

Pete - W6OP

  • 38 Posts
  • 13 Reply Likes
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.
Photo of Mark - WS7M

Mark - WS7M

  • 1338 Posts
  • 501 Reply Likes
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.
Photo of Pete - W6OP

Pete - W6OP

  • 38 Posts
  • 13 Reply Likes
Yes, I should have given a more comprehensive explanation.
Photo of Mark - WS7M

Mark - WS7M

  • 1338 Posts
  • 501 Reply Likes
Mark,

Feel free to email me at ws7m@arrl.net if you need some help at times.