在 VS 2010 中运行和运行构建的 EXE 有什么区别? [英] What is the difference between running in VS 2010 and running a builded EXE?

查看:26
本文介绍了在 VS 2010 中运行和运行构建的 EXE 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为一个学校项目,我们创建了一个 C# XNA 4.0 游戏,它在从 Visual Studio 2010 本身运行(在发布或调试中)时可以完美运行.但是,当它构建时,游戏在某个时刻莫名其妙地崩溃了.

As a school project we've created a C# XNA 4.0 Game that runs perfectly when run (in either Release or Debug) from Visual Studio 2010 itself. However, when it's built, the game inexplicably crashes at a certain point.

负责的代码部分似乎是这样的:

The responsible code portion seems to be this:

        while( true )
        {
            if( Client.readInfo )
            {
                t.Stop();
                t.Dispose();
                // Save last good IP to file
                string toWrite = IPinput.filledIn;
                using( StreamWriter file = new StreamWriter( "multiplayer.dat", false ) )
                {
                    file.WriteLine( toWrite );
                }

                ExitScreen();
                using( LobbyScreen screen = new LobbyScreen( c ) )
                {
                    screenManager.AddScreen( screen );
                }
                break;
            }
            else if( itTookToLong )
            {
                Client.killMe = true;
                IPinput.Text = "Server IP: ";
                IPinput.filledIn = "";
                break;
            }
        }

这一直等到启动的客户端线程使公共静态属性 readInfo 为真(发生这种情况,因为服务器获取客户端)或计时器在 10 秒后用完.当通过 VS 运行时,这两种东西都可以正常工作,但是当以内置 EXE 运行时,游戏就会停止响应.

This waits till the started Client thread makes the public static attribute readInfo true (which happens, because the server obtains the client) or a timer runs out after 10 seconds. Both things work perfectly fine when running through VS, but the game just stops responding when run as built EXE.

这会是什么?!

我似乎已经找到了修复方法,终于.在上述 while(true) 循环的底部添加 Thread.Sleep(1); 似乎解决了所有问题!不知道为什么这在运行 EXE 和从 Visual Studio 运行之间有所不同,但嘿它有效.

I seem to have found the fix already, FINALLY. Adding Thread.Sleep(1); at the bottom of the above while(true) loop seems to fix all problems! Don't know why this is different between running an EXE and running from Visual Studio though, but hey it works.

Edit2: 这个网站解释了我的整个问题:http://igoro.com/archive/volatile-keyword-in-c-memory-model-explained/Thread.Sleep 只是意外修复了所有内容 ;)

This site explains my whole problem: http://igoro.com/archive/volatile-keyword-in-c-memory-model-explained/ Thread.Sleep just accidentally fixed everything ;)

推荐答案

从你发布的代码很难看出,但我相信你需要让 Client.readInfo 字段 volatile.Thread.Sleep 解决了你的问题的原因是它把内存屏障作为副作用.

Hard to tell from the code you have posted, but I believe you need to make Client.readInfo field volatile. The reason that Thread.Sleep fixed your problem is that it puts a memory barrier as a side effect.

这篇关于在 VS 2010 中运行和运行构建的 EXE 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆