Console.Write 大约写 46774339次后就会抛出
"Must complete Convert() operation or call Encoder.Reset() before calling GetBytes() or GetByteCount(). Encoder 'Chinese Simplified (GB2312)' fallback 'System.Text.InternalEncoderBestFitFallback'."
异常
源代码
Console.Write("Current Frame:{0}\tFPS:{1}\tPerformance:{2}\r", Global.Loop, Global.Loop * 1000 / interval, idleFrame);求解答!!
"Must complete Convert() operation or call Encoder.Reset() before calling GetBytes() or GetByteCount(). Encoder 'Chinese Simplified (GB2312)' fallback 'System.Text.InternalEncoderBestFitFallback'."
异常
源代码
Console.Write("Current Frame:{0}\tFPS:{1}\tPerformance:{2}\r", Global.Loop, Global.Loop * 1000 / interval, idleFrame);求解答!!
{
infos = string.Format("Current Frame:{0}\tFPS:{1}\tPerformance:{2}\r", Global.Loop, Global.Loop * 1000 / interval, idleFrame);
}
catch (System.Exception e)
{
throw;
} try
{
Console.Write(infos); }
catch (System.Exception e)
{
throw;
}测试了 原因在于下面
现在这样测试中
不知道会不会都有问题 [DllImport("Kernel32.dll")]
static extern int WriteConsole
毕竟直接用windows函数不怎么爽(虽然类库内部也是这样搞的!!!)
看了一下.net的源代码,它内部用的一个__ConsoleStream的类,然后用StreamWriter封装上,异常出现在StreamWriter中,我直接用__ConsoleStream就好了
说明他走了下面
TextWriter writer = null;
Stream s;
if (stdout)
s = OpenStandardOutput(_DefaultConsoleBufferSize);
else
s = OpenStandardError(_DefaultConsoleBufferSize);
if (s == Stream.Null) {
#if _DEBUG
if (CheckOutputDebug())
writer = MakeDebugOutputTextWriter((stdout) ? "Console.Out: " : "Console.Error: ");
else
#endif // _DEBUG
writer = TextWriter.Synchronized(StreamWriter.Null);
}
else {
int codePage = (int) Win32Native.GetConsoleOutputCP();
Encoding encoding = Encoding.GetEncoding(codePage);
StreamWriter stdxxx = new StreamWriter(s, encoding, _DefaultConsoleBufferSize, false);
stdxxx.HaveWrittenPreamble = true;
stdxxx.AutoFlush = true;
writer = TextWriter.Synchronized(stdxxx);
}
暂时这样测试一下 Console.Out.Write(aaa, 0, aaa.Length);
因为TextWriter也是这样重载的
只能自己用API了
[DllImport("Kernel32.dll")]
static extern int WriteConsole(
IntPtr hConsoleOutput, char[] lpBuffer, int nNumberOfCharsToWrite, out int lpNumberOfCharsWritten, int lpReserved);其余的都会宕!
感谢各位回复
我现在使用API解决,具体解决办法在这儿 http://www.886s.com/blog/?p=67这是我的blog 欢迎大家来看看