简体   繁体   English

为什么在XNA游戏中以缓慢的FPS将[STAThread]放在Program.Main上会阻止还原到最小化?

[英]Why does putting [STAThread] on Program.Main in XNA game with slow FPS prevents restoring on minimize?

The symptom I'm trying to resolve is that my XNA application will not restore some of the time after being being minimized. 我要解决的症状是,将XNA应用程序最小化后的某些时间将无法恢复。 I click the minimize button, but when I click on my application's entry in the task bar, the application does not resume - it stays minimized. 我单击最小化按钮,但是当我单击任务栏中的应用程序条目时,该应用程序不会继续运行-仍保持最小化。 Furthermore, I hear a Windows "ding" sound effect play. 此外,我听到了Windows“叮”声效果的播放。

If I click on the application repeatedly it sometimes resumes. 如果我反复单击该应用程序,则有时会恢复。 I've found that if my application has less processing time in its every-frame Update call, then the problem is harder to reproduce. 我发现,如果我的应用程序在每帧Update调用中的处理时间更少,那么问题就更难重现了。 If I add this: 如果我添加此:

 System.Threading.Thread.Sleep(100);

then it's almost impossible to have the application resume. 那么恢复应用程序几乎是不可能的。

I've found that if I remove [STAThread] on Program.Main, it always resumes perfectly; 我发现,如果我删除Program.Main上的[STAThread],它总是可以完美恢复; however, I use winforms OpenFileDialog's which require STAThread to be set on the Main method so I can't get rid of that attribute. 但是,我使用winforms OpenFileDialog,它要求在Main方法上设置STAThread,所以我无法摆脱该属性。

Any thoughts as to why this might be happening, and if there's a way to both use STAThread and have the application properly resume? 是否对为什么会发生这种想法有任何想法,是否有办法同时使用STAThread和使应用程序正确恢复?

Edit: Internally my application is using ThreadPool.QueueUserWorkItem, and the presence of that is what is causing this. 编辑:内部我的应用程序正在使用ThreadPool.QueueUserWorkItem,而这的存在是导致此问题的原因。 Does this mean that ThreadPool.QueueUserWorkItem should not be used in a STAThread application? 这是否意味着不应在STAThread应用程序中使用ThreadPool.QueueUserWorkItem?

Do you mean STAThreadAttribute? 您是说STAThreadAttribute吗? If not, put that on on the main method instead. 如果不是,则将其放在main方法上。

I use STAThreadAttribute and ThreadPool.QueueUserWorkItem and do not have this issue. 我使用STAThreadAttribute和ThreadPool.QueueUserWorkItem,并且没有此问题。 Note that Sleep() is specifically stopping the window from responding because it blocks the thread. 请注意,Sleep()专门阻止窗口响应,因为它阻塞了线程。 Ideally you shouldn't be using sleep on your main thread for the game. 理想情况下,您不应该在游戏的主线程上使用睡眠。

I used a different method to get around this. 我使用了另一种方法来解决此问题。 To keep my dialogs and forms from blocking the main thread, I place my code in a separate subroutine and start a new thread: 为了防止对话框和表单阻塞主线程,我将代码放在单独的子例程中并启动一个新线程:

Protected Overrides Sub Update(gametime As GameTime)
    ...
    If (Keyboard.GetState().IsKeyDown(Microsoft.Xna.Framework.Input.Keys.O)) Then ' load a new object
         If loadthreadrunning = False Then
             loadthreadrunning = True
             Dim thread As New Thread(AddressOf BackgroundLoader)
             thread.SetApartmentState(ApartmentState.STA)
             thread.Start()
         End If
    End If
    ...
End Sub

...
Sub BackgroundLoader()
    Dim fd As New OpenFileDialog
    Dim dlgres As DialogResult
    dlgres = fd.ShowDialog()
    ...
    loadthreadrunning = False
End Sub 

Of course, my code is in VB, but the same technique will work for C#. 当然,我的代码在VB中,但是相同的技术也适用于C#。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么Program.Main没有出现在崩溃堆栈中 - Why Program.Main does not appear in the crash stack 为什么我的XNA Game在另一台计算机上运行缓慢? - Why is my XNA Game slow on another computer? 我在我的项目中添加了一个新类,并收到一条错误消息,提示“Program.Main() 有多个条目”。 为什么? - I added a new class to my project and got an error saying "Program.Main() has more than one entry". Why? 如何在 MSTest 项目的 TestMethod 中测试 Program.Main - How to test Program.Main in TestMethod in MSTest project 无法在 program.main Blazor Client wasm 中命中断点 - Can't hit breakpoint in program.main Blazor Client wasm 如何将SetBasePath设置为Program.Main上的dll位置 - How to set SetBasePath to dll location on Program.Main 在 ASP.NET Core 中 Program.Main 中访问环境名称 - Access environment name in Program.Main in ASP.NET Core 为什么我的XNA游戏落后? - Why is my XNA game lagging? 到达Program.Main()时,C#WinForms应用程序不会关闭 - C# WinForms app doesnt close when reaching end of Program.Main() 在IISIntegration上运行ASP.NET Core应用程序时,Program.Main会发生什么情况? - What happens of Program.Main when an ASP.NET Core application is run on IISIntegration?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM