以前用VS2005的时候,就遇到过一些很奇怪的BUG,比如始终报错,然后把项目文件删除,重新创建一个项目文件,就好了。

今天用VS2010测试程序时,又发现一个坑爹的BUG,这绝对不是我的错!

    static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false); string sguid1 = "ZCL0EF1B122B72B439089178236FD6180BEserver";
string sguid2 = "zclttrs"; int ISserver = ; if (ISserver == )
{
if (ISbeginSoft(sguid1)==false)
Application.Run(new ZZserver());
}
else
{
//问题出在这里,ZZclient执行时,第二个实例会正常启动,而不会弹出提示
if (ISbeginSoft(sguid2) == false)
//Application.Run(new QQlocalF.ZZclient());

//这4个类,包括我重新创建的 ZZclienty都得到争取执行,启动唯一保护后,都会弹出提示
//Application.Run(new QQlocal.Form1());
//Application.Run(new QQlocalF.ZZclienty());
//Application.Run(new ZZserver());
//Application.Run(new QQlocal.Ztest()); //不调用ISbeginSoft方法,如下直接写代码,也是可以正确执行的.
//var ISbegin = ZCLX.IO.Usual.MemoryRead(sguid2, null); //if (ISbegin.Item1)
//{
// MessageBox.Show("软件已经启动");
//}
//else
//{
// ZCLX.IO.Usual.MemoryWrite(sguid2, 50, "ISbegin");
// Application.Run(new ZZclient());
//} }
} public static bool ISbeginSoft(string sx)
{
var ISbegin = ZCLX.IO.Usual.MemoryRead(sx, null); if (ISbegin.Item1)
{
MessageBox.Show("软件已经启动");
}
else
{
ZCLX.IO.Usual.MemoryWrite(sx, , "ISbegin");
//Application.Run(new ZZclient());
//如果写在这里,那么是可以得到正确执行的.
} return ISbegin.Item1;
}
}
QQlocalF.ZZclient()是一个Form类,早已经创建,
ISbeginSoft()是一个静态方法,用于标示一个实例是否启动,它读取一个指定的内存文件名字,如果该文件存在,说明已经有实例建立,那么就弹出提示并正常结束;如果内存文件不存在,那么就写入一个内存文件,并且启动实例。

以下是内存文件的具体实现:
        public static MemoryMappedFile MemoryWrite(string Filename, int capacity, string Filedata)
{
var mmf = MemoryMappedFile.CreateOrOpen(Filename, capacity, MemoryMappedFileAccess.ReadWrite); using (var stream = mmf.CreateViewStream()) //使用完立即释放内存
{
var writer = new BinaryWriter(stream);
writer.Write(Filedata);
} return mmf;
} public static Tuple<bool,string> MemoryRead(string Filename,string Error)
{
try
{
using (var mmf = MemoryMappedFile.OpenExisting(Filename, MemoryMappedFileRights.ReadWrite))
{
using (var stream = mmf.CreateViewStream())
{
var reader = new BinaryReader(stream);
return new Tuple<bool, string>(true, reader.ReadString());
}
}
}
catch(Exception ex)
{
return new Tuple<bool, string>(false, ex.ToString());
}
}
ZZserver执行正常,

//问题出在这里,ZZclient执行时,第二个实例会正常启动,而不会弹出提示
if (ISbeginSoft(sguid2) == false)
//Application.Run(new QQlocalF.ZZclient());//我怎么都无法找到代码BUG,就这么几行代码,到底哪里出了问题?
断点调试时:
            if (ISbegin.Item1) //这里的值总是为false,也就是即使创建了一个实例,要么内存文件没有建立,要么无法读取到内存文件.
{
MessageBox.Show("软件已经启动");
}
                    //这4个类,包括我重新创建的 ZZclienty都得到正确执行,启动唯一保护后,都会弹出提示
//Application.Run(new QQlocal.Form1());
//Application.Run(new QQlocalF.ZZclienty());
//Application.Run(new ZZserver());
//Application.Run(new QQlocal.Ztest()); 尝试解决的方法: 重新生成,清理,重新生成,关闭VS重新生成,关闭电脑重新打开.问题依然存在,并且仅仅只是ZZclient出错,仅仅只是
if (ISbeginSoft(sguid2) == false)  //仅仅只是调用这个方法后,如果不调用,那么也可以正确执行
//Application.Run(new QQlocalF.ZZclient()); //仅仅只是代码写在这里,如果把Application.Run(new QQlocalF.ZZclient());写在ISbeginSoft()方法中,也是可以正确执行
恰好是ZZclient类,恰好是先调用ISbeginSoft(sguid2) == false方法执行,恰好运行代码写在方法之后,才会出现这个BUG,无法读取内存文件,调试结果为内存文件不存在.
问题的解决: 我创建一个新的Form类,然后我把ZZclient中的所有控件和方法移植到新的Form控件中,我起名为ZZclienty,
结果:ZZclienty执行正确!
我只想说: 这个坑遇到的概率究竟是多少,倒这么大的霉气,日后是不是该撞大运啊!
分析: 我感觉当创建一个Form类后,VS会为它创建一个唯一值定位,而错误就发生在这个唯一值定位上有问题,所以即使我把它的空间名字更改,把类名字更改,都无法解决,唯有新建一个Form类,才彻底解决问题!

vs2010 坑爹的BUG的更多相关文章

  1. VS2010出现FileTracker : error FTK1011编译错误的解决办法

    VS2010出现FileTracker : error FTK1011不知道是不是vs2010的一个bug,反正有人提交了. FileTracker : error FTK1011编译错误的解决办法有 ...

  2. libevent evbuffer bug

    今天发现 libevent 2.0.22 一个坑爹的bug,导致消息混乱.查找问题浪费一天,复现代码如下 #include <event2/buffer.h> #include <s ...

  3. FFmpeg与VS2010

    编译FFmpeg是一件痛苦的事情,一般都直接使用Zeranoe FFmpeg Builds. 如果使用这个版本,需要注意ffmpeg的帮助里的一段话: To create import librari ...

  4. safari 与 chrome 的小区别大BUG

    safari 与 chrome 的小区别大BUG 时间:2016-11-01 17:33:19 作者:zhongxia 原文地址:https://github.com/zhongxia245/blog ...

  5. [开发工具]_[VS2010]_[vs2010的一个bug-使用stringstream时出现]

    1. 注冊Microsfot之后想提交bug, 发现有这个提示, 所以提交不了bug, 有能提交的提交下吧. You are not authorized to submit the feedback ...

  6. 【学习篇:他山之石,把玉攻】jquery实现调用webservice

    1.webservice端 using System; using System.Collections.Generic; using System.Web; using System.Web.Ser ...

  7. Echarts显示全球dns server物理位置

    今天YY给了我一大串dns server的ip,然后提出将这些ip物理位置显示在世界地图上(仅仅显示每个地区有几个dns server就好),苦逼了一下午,总算告一段落.把里面关键的点贴上来以后看.. ...

  8. 解说cocos2d-x几种画图方法的用法与思考

    CCRenderTexture 自己的理解 CCRenderTexture类似一张空白的“画布“,用户通过自定义笔刷(CCSprite*),在touch事件中把笔刷的移动痕迹“记录”起来,从而“画”出 ...

  9. jquery实现调用webservice

    1.webservice端 using System; using System.Collections.Generic; using System.Web; using System.Web.Ser ...

随机推荐

  1. 例子:两个表根据productID合并

  2. weblogic启动后 登陆控制台特别慢的问题

    weblogic官方文档给出的问题原因: 江湖偏方: 修改jdk:修改$JAVA_HOME/jre/lib/security/java.security文件,替换securerandom.source ...

  3. proc_create的使用方法

    proc_create的使用方法 proc文件系统是个有用的东东.创建一个proc虚拟文件,应用层通过读写该文件,即可实现与内核的交互.proc虚拟文件是如何创建的呢? 先看看比较简单的,创建proc ...

  4. BootLoader与Linux内核的参数传递【转】

    本文转载自:http://blog.sina.com.cn/s/blog_476d8cf30100rttx.html 在嵌入式系统中,BootLoader 是用来初始化硬件,加载内核,传递参数.因为嵌 ...

  5. js操作创建和操作外部样式的例子

    兼容IE8及以上的IE浏览器1. [代码][HTML]代码 <!DOCTYPE html><html lang="en">    <head>  ...

  6. poj 1325 Machine Schedule 解题报告

    题目链接:http://poj.org/problem?id=1325 题目意思:有 k 个作业,机器A有 n 个模式:0 ~ n-1,机器B 有 m 个模式:0~ m-1.每一个作业能运行在 A 的 ...

  7. AJAX 用户验证方法

    JSP <td width="10%" class="main_matter_td">真实姓名</td> <td width=&q ...

  8. Oracle:ORA-01790: expression must have same datatype as corresponding expression

    今天有现场报sql错误,错误sql语句 类似  select * from  tableA  where (exists  条件A   or exists 条件B), 单独执行  select * f ...

  9. CentOS 7中ip命令将逐渐取代 ifconfig

    首先看下图: 要安装ip,请点击这里下载iproute2套装工具 .不过,大多数Linux发行版已经预装了iproute2工具. 你也可以使用git命令来下载最新源代码来编译: $ git clone ...

  10. windows下关闭指定端口服务,解决tomcat端口占用问题

    http://blog.aizhet.com/Server/640.html 在windows下做java EE的开发时:搭建 Eclipse+tomcat的java web开发环境:在应用之中经常遇 ...