本文整理汇总了C#中System.IO.StreamReader.?.Dispose方法的典型用法代码示例。如果您正苦于以下问题:C# StreamReader.?.Dispose方法的具体用法?C# StreamReader.?.Dispose怎么用?C# StreamReader.?.Dispose使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.IO.StreamReader
的用法示例。
在下文中一共展示了StreamReader.?.Dispose方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ForceDisposeStreamReader
/// <summary>
/// In some scenarios, a StreamReader will be blocked on read() when trying to dispose it.
/// On OS X under mono, read() will block any close() syscalls on the file descriptor for the stream.
/// Therefore, we write a byte to unblock the read() and allow close() to succeed.
/// Callers to this function should document why read() might be blocked.
/// </summary>
/// <param name="reader">The StreamReader to forcibly dispose</param>
protected static void ForceDisposeStreamReader(StreamReader reader)
{
try
{
reader?.BaseStream.WriteByte(0);
}
catch
{
}
reader?.Dispose();
}
示例2: FromFileTail
/// <summary>
/// Observe String-Line from file like tail -f.
/// </summary>
/// <param name="encoding">If null, use Encoding.UTF8</param>
public static IObservable<string> FromFileTail(string filePath, bool readFromFirstLine = false, Encoding encoding = null)
{
return Observable.Defer(() =>
{
encoding = encoding ?? Encoding.UTF8;
var subject = new Subject<string>();
var fi = new System.IO.FileInfo(filePath);
var stream = fi.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
var streamReader = new StreamReader(stream, encoding);
try
{
var firstSource = new List<string>();
if (readFromFirstLine)
{
while (!streamReader.EndOfStream)
{
firstSource.Add(streamReader.ReadLine());
}
}
else
{
stream.Seek(fi.Length, SeekOrigin.Current);
}
var readingLock = new object();
var fsw = new FileSystemWatcher(fi.DirectoryName, fi.Name);
fsw.NotifyFilter = NotifyFilters.Size;
fsw.Changed += (sender, e) =>
{
try
{
lock (readingLock)
{
string s;
while ((s = streamReader.ReadLine()) != null)
{
if (s != "")
{
subject.OnNext(s);
}
}
}
}
catch (Exception ex)
{
subject.OnError(ex);
}
};
fsw.EnableRaisingEvents = true;
return firstSource.ToObservable().Concat(subject).Finally(() =>
{
fsw.EnableRaisingEvents = false;
fsw.Dispose();
streamReader.Dispose();
stream.Dispose();
});
}
catch
{
streamReader?.Dispose();
stream?.Dispose();
throw;
}
});
}
示例3: HandleClientAsync
//.........这里部分代码省略.........
if (switched)
{
var setCurrent = Desktop.SetCurrent(inputDesktop);
if (setCurrent)
{
Console.WriteLine(
$"Desktop switched from {LastDesktop} to {inputDesktop.DesktopName}");
LastDesktop = inputDesktop.DesktopName;
lastDesktopInput = inputDesktop;
}
else
{
lastDesktopInput.Close();
}
}
}
else
{
inputDesktop.Close();
}
var endpoint = await sr.ReadLineAsync();
if (string.IsNullOrEmpty(endpoint))
{
break;
}
string[] endpointArgs = { };
if (endpoint.Contains("|"))
{
var splitPoint = endpoint.Split('|');
endpointArgs = splitPoint[1].Split(',');
endpoint = splitPoint[0];
}
string response = null;
switch (endpoint)
{
case "fullframe":
var frameData = SendFullFrame();
response = frameData.Length == 0 ? "n" : Convert.ToBase64String(frameData);
break;
case "cleanframe":
var cleanFrameData = SendCleanFrame();
response = cleanFrameData.Length == 0 ? "n" : Convert.ToBase64String(cleanFrameData);
break;
case "ctrlaltdel":
HandleCtrlAltDel();
break;
case "rightdown":
HandleRightDown();
break;
case "rightup":
HandleRightUp();
break;
case "mousemove":
int x = Convert.ToInt16(endpointArgs[0], CultureInfo.InvariantCulture);
int y = Convert.ToInt16(endpointArgs[1], CultureInfo.InvariantCulture);
MoveMouse(x, y);
break;
case "mousedown":
HandleMouseDown();
break;
case "mousescroll":
HandleMouseScroll(endpointArgs[0]);
break;
case "mouseup":
HandleMouseUp();
break;
case "leftclick":
break;
case "rightclick":
HandleRightClick();
break;
case "keydown":
KeyDown(endpointArgs);
break;
case "keyup":
Keyup(endpointArgs);
break;
}
if (string.IsNullOrEmpty(response))
{
break;
}
await sw.WriteLineAsync(response);
await sw.FlushAsync();
await Task.Yield();
}
}
catch (Exception aex)
{
var ex = aex.GetBaseException();
Console.WriteLine("Client error: " + ex.Message);
}
finally
{
sr?.Dispose();
sw?.Dispose();
}
}