涉及到自动编译的时候,比如使用XmlSerializer Xml的序列化和反序列化或codeDom的自动编译的方法(CompileAssemblyFromBatch()等)使用的时候,会启动C#的编译器csc.exe, 由于在亚洲版的win xp操作系统中,特有的ConIme.exe,所以csc内部中有启动ConIme的功能,为了其支持输入如汉字,和日本语,韩国语的字符。但是启动过程中会有ConIme启动失败的时候,此处原因未明(ms也没有个说法),会在此处等上10分钟,然后csc才会接到timeout的异常而返回。
根据ms的说法,解决方法是,一个是不用csc如果xml序列化的时候,可以不用call csc.exe but 如果真的要自动编译一个C#文件,那就逃也逃不掉csc了。这时候要做的就是屏蔽ConIme,不让其启动,这样csc就可以摆脱那个万一的10分钟的deadLock了。
大致代码如下:
1
try
2
{
3
RegistryKey hkcu = Registry.CurrentUser ;
4
RegistryKey skconsole = hkcu.OpenSubKey ( "Console",true ) ;
5
6
if (skconsole.GetValue("LoadConIme").Equals(1) )
7
{
8
skconsole.SetValue("LoadConIme",0);
9
}
10
hkcu.Close();
11
skconsole.Close();
12
// コンパイル
13
CompilerResults cret = compiler.CompileAssemblyFromFileBatch( cprm, filenames);
14
15
return cret;
16
17
} //end try
18
catch(Exception e)
19
{
20
throw e;
21
}
22
finally
23
{
24
RegistryKey hkcu = Registry.CurrentUser ;
25
RegistryKey skconsole = hkcu.OpenSubKey ( "Console",true ) ;
26
if (skconsole.GetValue("LoadConIme").Equals(0) )
27
{
28
skconsole.SetValue("LoadConIme",1);
29
}
30
hkcu.Close();
31
skconsole.Close();
32
33
}
try2
{3
RegistryKey hkcu = Registry.CurrentUser ;4
RegistryKey skconsole = hkcu.OpenSubKey ( "Console",true ) ;5
6
if (skconsole.GetValue("LoadConIme").Equals(1) )7
{8
skconsole.SetValue("LoadConIme",0);9
}10
hkcu.Close();11
skconsole.Close();12
// コンパイル13
CompilerResults cret = compiler.CompileAssemblyFromFileBatch( cprm, filenames);14
15
return cret;16

17
} //end try 18
catch(Exception e)19
{20
throw e;21
}22
finally23
{24
RegistryKey hkcu = Registry.CurrentUser ;25
RegistryKey skconsole = hkcu.OpenSubKey ( "Console",true ) ;26
if (skconsole.GetValue("LoadConIme").Equals(0) )27
{28
skconsole.SetValue("LoadConIme",1);29
}30
hkcu.Close();31
skconsole.Close();32

33
}代码简单说明如下:
1.opensubkey("Console",true)的第二个参数要是真,否则不能修改其keyvalue.
2.修改了keyvalue后,要close()这样才能有效。总开着也觉得不舒服吧?
3.要finally中恢复你修改的keyvalue.这是因为将loadConIme的value改为0之后,再Cmd Console中中文输入法等亚洲系列的ConIme将无法启动,所以只在你需要的时候屏蔽一下,用完了总是要还的。(出来混总是要还的。)


浙公网安备 33010602011771号