C# 跨线程操作无效,调用语句无效
我有一个Mainform,上面有很多控件。现在我正试图从线程访问这些控件。我应该为每个指令使用invoke语句吗?!!!还是有别的办法?我的一段代码是:C# 跨线程操作无效,调用语句无效,c#,invoke,C#,Invoke,我有一个Mainform,上面有很多控件。现在我正试图从线程访问这些控件。我应该为每个指令使用invoke语句吗?!!!还是有别的办法?我的一段代码是: switch (currentSetting.CurrentFFMDisplayMode) { case FFM_DisplayMode.Polar: this.display.Visible
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;
您可以在匿名方法中包围整个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));
顺便说一句,您可以通过选择性地迭代表单的控件来减少case语句的长度(以及一致的可读性),如图所示 您可以在匿名方法中包围整个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));
顺便说一句,您可以通过选择性地迭代表单的控件来减少case语句的长度(以及一致的可读性),如图所示 您可以在匿名方法中包围整个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));
顺便说一句,您可以通过选择性地迭代表单的控件来减少case语句的长度(以及一致的可读性),如图所示 您可以在匿名方法中包围整个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));
顺便说一句,您可以通过选择性地迭代表单的控件来减少case语句的长度(以及一致的可读性),如图所示 这有帮助。谢谢你,穆奇!这有帮助。谢谢你,穆奇!这有帮助。谢谢你,穆奇!这有帮助。谢谢你,穆奇!