跨线程操作无效和Invoke语句

时间:2014-08-02 12:26:16

标签: c# invoke

我有一个Mainform,上面有很多控件。现在我试图从一个线程中访问这个控件。我应该为每条指令使用invoke语句吗?!!!还是有另一种方式?我的一段代码是:

     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;

1 个答案:

答案 0 :(得分:3)

可以以匿名方法

包围整个switch语句。

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

}));

但为了避免过度嵌套,我会将switch语句带入另一种方法:

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

this.Invoke(new Action(DoSomething));

顺便说一下,您可以通过选择性地迭代窗体的控件来显示here,从而减​​少case语句的长度(以及一致性,可读性)。