本文整理汇总了C#中SafeFileHandle.SetHandleAsInvalid方法的典型用法代码示例。如果您正苦于以下问题:C# SafeFileHandle.SetHandleAsInvalid方法的具体用法?C# SafeFileHandle.SetHandleAsInvalid怎么用?C# SafeFileHandle.SetHandleAsInvalid使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SafeFileHandle
的用法示例。
在下文中一共展示了SafeFileHandle.SetHandleAsInvalid方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetStandardFile
private static Stream GetStandardFile(int stdHandleName, FileAccess access, int bufferSize) {
// We shouldn't close the handle for stdout, etc, or we'll break
// unmanaged code in the process that will print to console.
// We should have a better way of marking this on SafeHandle.
IntPtr handle = Win32Native.GetStdHandle(stdHandleName);
SafeFileHandle sh = new SafeFileHandle(handle, false);
// If someone launches a managed process via CreateProcess, stdout
// stderr, & stdin could independently be set to INVALID_HANDLE_VALUE.
// Additionally they might use 0 as an invalid handle.
if (sh.IsInvalid) {
// Minor perf optimization - get it out of the finalizer queue.
sh.SetHandleAsInvalid();
return Stream.Null;
}
// Check whether we can read or write to this handle.
if (stdHandleName != Win32Native.STD_INPUT_HANDLE && !ConsoleHandleIsValid(sh)) {
//BCLDebug.ConsoleError("Console::ConsoleHandleIsValid for std handle "+stdHandleName+" failed, setting it to a null stream");
return Stream.Null;
}
//BCLDebug.ConsoleError("Console::GetStandardFile for std handle "+stdHandleName+" succeeded, returning handle number "+handle.ToString());
Stream console = new __ConsoleStream(sh, access);
// Do not buffer console streams, or we can get into situations where
// we end up blocking waiting for you to hit enter twice. It was
// redundant.
return console;
}
示例2: GetStandardFile
private static Stream GetStandardFile(int stdHandleName, FileAccess access, int bufferSize)
{
IntPtr stdHandle = Win32Native.GetStdHandle(stdHandleName);
SafeFileHandle safeFileHandle = new SafeFileHandle(stdHandle, false);
if (safeFileHandle.IsInvalid)
{
safeFileHandle.SetHandleAsInvalid();
return Stream.Null;
}
if (stdHandleName != -10 && !Console.ConsoleHandleIsValid(safeFileHandle))
{
return Stream.Null;
}
return new __ConsoleStream(safeFileHandle, access);
}