[英]Calling Thread must be STA
我試圖模擬一個彈丸,並試圖在定時事件中僅創建一次標簽,因為它在參數中有一個對象,我需要在定時事件中。 但是我運行它時收到此線程錯誤。 請幫助!
我的代碼是:
void onTimedEvent(Object source, ElapsedEventArgs e)
{
particle newProjectile;
newProjectile = new particle();
bool LabelExist = false;
if (LabelExist == false)
{
CreateLabels(newProjectile);
}
}
aTimer = new System.Timers.Timer(200);
aTimer.Elapsed += onTimedEvent;
aTimer.Enabled = true;
void CreateLabels(particle newProjectile)
{
DockPanel panelcontrol_displacement, panelcontrol_CurrentVel, panelcontrol_VerticalVel, panelcontrol_HorizontalVel, panelcontrol_Time;
Label lbl_TimeUnits, lbl_TimeOutput, lbl_Time, lbl_DisplacementOutput, lbl_DisplacementUnits, lbl_Displacement, lbl_CurrentVel, lbl_CurrentVelOutput, lbl_HorizontalVelocityOutputs, lbl_HorizontalVelocityUnits, lbl_VerticalVelocity, lbl_HorizontalVelocity, lbl_CurrentVelUnits, lbl_VerticalVelocityOutput, lbl_VerticalVelocityUnits;
lbl_Time = new Label();
lbl_Time.Content = "Time";
lbl_Time.Height = 30;
lbl_Time.Width = 110;
lbl_TimeOutput = new Label();
lbl_TimeOutput.Content = "20";
lbl_TimeOutput.Height = 30;
lbl_TimeOutput.Width = 100;
lbl_TimeOutput.HorizontalContentAlignment = HorizontalAlignment.Right;
lbl_TimeUnits = new Label();
lbl_TimeUnits.Content = "s";
lbl_CurrentVel = new Label();
lbl_CurrentVel.Content = "Current Velocity";
lbl_CurrentVel.Height = 30;
lbl_CurrentVel.Width = 110;
lbl_CurrentVelOutput = new Label();
lbl_CurrentVelOutput.Content = "20";
lbl_CurrentVelOutput.Height = 30;
lbl_CurrentVelOutput.Width = 100;
lbl_CurrentVelOutput.HorizontalContentAlignment = HorizontalAlignment.Right;
lbl_CurrentVelUnits = new Label();
lbl_CurrentVelUnits.Content = "m/s";
lbl_VerticalVelocity = new Label();
lbl_VerticalVelocity.Content = "Verticle Velocity";
lbl_VerticalVelocity.Height = 30;
lbl_VerticalVelocity.Width = 110;
lbl_VerticalVelocityOutput = new Label();
lbl_VerticalVelocityOutput.Content = "20";
lbl_VerticalVelocityOutput.Height = 30;
lbl_VerticalVelocityOutput.Width = 100;
lbl_VerticalVelocityOutput.HorizontalContentAlignment = HorizontalAlignment.Right;
lbl_VerticalVelocityUnits = new Label();
lbl_VerticalVelocityUnits.Content = "m/s";
lbl_HorizontalVelocity = new Label();
lbl_HorizontalVelocity.Content = "Horizontal Velocity";
lbl_HorizontalVelocity.Height = 30;
lbl_HorizontalVelocity.Width = 110;
lbl_HorizontalVelocityOutputs = new Label();
lbl_HorizontalVelocityOutputs.Content = "20";
lbl_HorizontalVelocityOutputs.Height = 30;
lbl_HorizontalVelocityOutputs.Width = 100;
lbl_HorizontalVelocityOutputs.HorizontalContentAlignment = HorizontalAlignment.Right;
lbl_HorizontalVelocityUnits = new Label();
lbl_HorizontalVelocityUnits.Content = "m/s";
lbl_Displacement = new Label();
lbl_Displacement.Content = "Displacement";
lbl_Displacement.Height = 30;
lbl_Displacement.Width = 110;
lbl_DisplacementOutput = new Label();
lbl_DisplacementOutput.Content = "20";
lbl_DisplacementOutput.Height = 30;
lbl_DisplacementOutput.Width = 100;
lbl_DisplacementOutput.HorizontalContentAlignment = HorizontalAlignment.Right;
lbl_DisplacementUnits = new Label();
lbl_DisplacementUnits.Content = "m/s";
panelcontrol_HorizontalVel = new DockPanel();
panelcontrol_CurrentVel = new DockPanel();
panelcontrol_VerticalVel = new DockPanel();
panelcontrol_displacement = new DockPanel();
panelcontrol_Time = new DockPanel();
panelcontrol_CurrentVel.Children.Add(lbl_CurrentVel);
panelcontrol_CurrentVel.Children.Add(lbl_CurrentVelOutput);
panelcontrol_CurrentVel.Children.Add(lbl_CurrentVelUnits);
panelcontrol_VerticalVel.Children.Add(lbl_VerticalVelocity);
panelcontrol_VerticalVel.Children.Add(lbl_VerticalVelocityOutput);
panelcontrol_VerticalVel.Children.Add(lbl_VerticalVelocityUnits);
panelcontrol_HorizontalVel.Children.Add(lbl_HorizontalVelocity);
panelcontrol_HorizontalVel.Children.Add(lbl_HorizontalVelocityOutputs);
panelcontrol_HorizontalVel.Children.Add(lbl_HorizontalVelocityUnits);
panelcontrol_displacement.Children.Add(lbl_Displacement);
panelcontrol_displacement.Children.Add(lbl_DisplacementOutput);
panelcontrol_displacement.Children.Add(lbl_DisplacementUnits);
panelcontrol_Time.Children.Add(lbl_Time);
panelcontrol_Time.Children.Add(lbl_TimeOutput);
panelcontrol_Time.Children.Add(lbl_TimeUnits);
StkPnl_Inputs.Children.Add(panelcontrol_Time);
StkPnl_Inputs.Children.Add(panelcontrol_CurrentVel);
StkPnl_Inputs.Children.Add(panelcontrol_VerticalVel);
StkPnl_Inputs.Children.Add(panelcontrol_HorizontalVel);
StkPnl_Inputs.Children.Add(panelcontrol_displacement);
//databinding
Binding(newProjectile, lbl_CurrentVelOutput, "CurrentVelocity");
}
您收到此錯誤,是因為不允許它從后台線程訪問UI( System.Timers.Timer.Elapsed
處理程序正在后台線程上運行)。 這是因為在WPF中處理UI的所有線程都應在Single Threaded Apartment(STA)
運行以進行同步。 並且,后台工作者不在STA中運行。
使用DispatcherTimer而不是System.Timers.Timer
。
MSDN頁面的備注部分概述了這兩個計時器之間的區別。
如果在WPF應用程序中使用了System.Timers.Timer,則值得注意的是System.Timers.Timer在不同於用戶界面(UI)線程的線程上運行。 為了訪問用戶界面(UI)線程上的對象,必須使用Invoke或BeginInvoke將操作發布到用戶界面(UI)線程的Dispatcher上。
使用與System.Timers.Timer相對的DispatcherTimer的原因是DispatcherTimer與Dispatcher在同一線程上運行,並且可以在DispatcherTimer上設置DispatcherPriority。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.