0

I have a Mainform with many controls on it.Now I'm trying to access this controls from a thread.Should I use invoke statement for each instruction?!!! or there is another way? a piece of my code is:

     switch (currentSetting.CurrentFFMDisplayMode)
                    {
                        case FFM_DisplayMode.Polar:
                            this.display.Visible = false;
                            this.gauge1.init(10000, 450);
                            this.gauge1.Visible = true;
                            this.AzimuthWaterfall.Visible = false;
                            this.FreqWaterfall.Visible = false;
                            this.histogram1.Visible = false;
                            this.LevelSlider.Visible = true;
                            this.QualitySlider.Visible = true;
                            this.LevelSlider.Visible = true;
                            this.QualitySlider.Visible = true;
                            this.textBoxLevel.Visible = true;
                            this.textBoxQuality.Visible = true;
                            this.textBoxAzimuth.Visible = true;
                            this.textBoxPrevious.Visible = true;
                            this.textBoxfloatE.Visible = true;
                            this.labelPrevious.Visible = true;

                            this.labelLevelErr.Visible = false;
                            this.labelQualityErr.Visible = false;
                            this.LevelLine.Visible = false;
                            this.Levelarraw.Visible = false;
                            this.freqLine.Visible = false;
                            this.spectVLine.Visible = false;
                            this.labelSNR.Visible = false;
                            this.button_dec_freq_spec.Visible = false;
                            this.button_inc_freq_spec.Visible = false;
                            this.combobox_Span.Visible = false;
                            this.label_Spectrum.Visible = false;
                            this.FreqLable.Visible = false;

                            label1.Visible = true;
                            label2.Visible = true;
                            label3.Visible = true;
                            label4.Visible = true;
                            label5.Visible = true;
                            label6.Visible = true;
                            label7.Visible = true;
                            label8.Visible = true;
                            label9.Visible = true;
                            label10.Visible = true;
                            label11.Visible = true;
                            label12.Visible = true;
                            l20.Visible = true;
                            LevelTHlabel.Visible = true;
                            qTHlabel.Visible = true;
Fateme
  • 21
  • 7

1 Answers1

3

You could surround the entire switch statement in an anonymous method like so:

this.Invoke(new Action(() => 
{
     switch (currentSetting.CurrentFFMDisplayMode) 
     {
          case FFM_DisplayMode.Polar:
              this.display.Visible = false;
              ...
     }

}));

But to avoid excessive nesting I would bring the switch statement out into another method:

private void DoSomething()
{
    switch (currentSetting.CurrentFFMDisplayMode) 
    {
        case FFM_DisplayMode.Polar:
            this.display.Visible = false;
             ...
    }
}
...

this.Invoke(new Action(DoSomething));

By the way, you may well be able to reduce the length (and consistently, the readability) of the case statement by selectively iterating the controls of your form as demonstrated here.

Community
  • 1
  • 1
User 12345678
  • 7,714
  • 2
  • 28
  • 46