简体   繁体   English

仅发布版本中的C#应用​​程序问题

[英]C# application problem in Release build only

the problem only appears when making Release build and running exe file ( not from visual studio ) in all other combination either it's running from visual studio or running exe everything works fine 仅当从Visual Studio运行或运行exe时,才以所有其他组合制作Release版本并运行exe文件(不是从Visual Studio中运行)时才出现问题。

I'm running Function Load using backgroundWorker 我正在使用backgroundWorker运行功能加载

Load: 加载:

while (!Request.GAMELIST.XMLReceived) ;

GameEngine.ParseGameList( Request.GAMELIST.XML );

Request.GAMELIST.XMLReceived = false;

while loop in this fragment works like delay it should wait till XML is received from server and then continue but it stucks in above specified situation 该片段中的while循环就像延迟一样工作,它应该等到从服务器接收到XML之后再继续,但是在上述指定情况下会卡住

if I'll put MessageBox.show("here we go"); 如果我将MessageBox.show(“我们走到这里”); after while loop messageBox will not appear but if I'll put MessageBox.show("here we go"); 在while循环之后,messageBox将不会出现,但是如果我放MessageBox.show(“ here we go”); before while loop application will receive data until I click messagebox ok and then everything will work fine 在while循环之前,应用程序将接收数据,直到我单击messagebox ok,然后一切正常

here is GAMELIST class implementation 这是GAMELIST类的实现

public class RequestGAMELIST
{
    public string XML;

    public bool XMLReceived = false;

    public void ParseRequest( string request )
    {
        int index = request.IndexOf(':') + 2;
        XML = request.Substring(index, request.Length - index);
        XMLReceived = true;
    }
}

please provide help if you can this is really strange thing which I can't figure out by my self 如果可以的话,请提供帮助。这确实很奇怪,我自己无法解决

Thanks. 谢谢。

Yes, this code has very good odds to hang in the Release build. 是的,此代码有很大的几率挂在Release版本中。 The JIT optimizer doesn't know that the variable might be set to true by code outside of the method. JIT优化器不知道该变量可能被方法外部的代码设置为true。 You need to tell it that, like this: 您需要这样说:

public class RequestGAMELIST
{
    public volatile bool XMLReceived = false;
    // etc..
}

The volatile keyword ensures that the jitter won't store the variable value in a CPU register. volatile关键字可确保抖动不会将变量值存储在CPU寄存器中。

That solves your problem, it is still not the right way to do it. 那可以解决您的问题,但这仍然不是正确的方法。 You should use an AutoResetEvent instead. 您应该改用AutoResetEvent。 It ensures that the thread responds to the variable change is quickly as possible. 它确保线程尽快响应变量更改。 And most importantly, it lets the thread block so it doesn't burn any cpu cycles. 最重要的是,它允许线程阻塞,这样它就不会消耗任何cpu周期。

public class RequestGAMELIST
{
    public AutoResetEvent XMLReceived = new AutoResetEvent();

    public void ParseRequest( string request )
    {
        int index = request.IndexOf(':') + 2;
        XML = request.Substring(index, request.Length - index);
        XMLReceived.Set();
    }

}

In your thread: 在您的线程中:

    XMLReceived.WaitOne();
    GameEngine.ParseGameList( Request.GAMELIST.XML );

This is a bad idea: 这是一个主意:

while (!Request.GAMELIST.XMLReceived) ;

At least you should be doing something like: 至少您应该这样做:

while (!Request.GAMELIST.XMLReceived) {
  System.Threading.Thread.Sleep(100);  // Don't hog the CPU!
}

Your program runs fine in debug mode perhaps due to certain debug routines added inside the while loop which makes it run slower... 您的程序在调试模式下可以正常运行,这可能是由于在while循环中添加了某些调试例程导致它运行速度较慢...

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM