.net - 如何判断流编写器是否已关闭?

标签 .net c++-cli clr backgroundworker streamwriter

我正在使用流编写器和后台工作人员进行日志记录。

因此,我有

System::Void
MyUI::execBWorker_DoWork(System::Object^  sender, System::ComponentModel::DoWorkEventArgs^  e) {

String^ outputPath = _clr::Settings::ApplicationLogPath("_log.txt", true, false);
logfile_ = gcnew StreamWriter(outputPath,true);

DoStuff();
logfile_->Close();
}

DoStuff() 方法中的事情引发了 Progress 事件。
System::Void
MyUI::execBWorker_ProgressChanged(System::Object^  sender, System::ComponentModel::ProgressChangedEventArgs^  e) {
logfile_->WriteLine("something");
}

我觉得这真的很臭。我怎样才能让它更好,或者至少我怎样才能检查日志文件没有关闭?有很多消息,所以我担心不断打开和关闭日志文件。

最佳答案

如果StreamWriter已关闭,BaseStream属性将返回 null .

关于.net - 如何判断流编写器是否已关闭?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11323808/

相关文章:

c# - 在 .NET 中发布会产生 "Application Files\MyApp_SomeVersionNumber"如何获取此数字?

.net - 审计日志方法的提示

c# - == 与 C# 中的等于

通过 Firebreath 框架从 javascript 公开的 C# 类

C++ - 在我关闭程序之前 ofstream 不会输出到文件

c# - C++ Interop : How do I call a C# class from native C++, 类是非静态的吗?

c# - 无法在面向 .NET 3.5 的 Visual Studio 2012 中的 C# 项目中使用 CLI 类库中的 CLR 类型并使用 Windows7.1SDK 工具集进行编译

.net - System.Web.Caching.Cache 在 ASP.Net MVC 应用程序中有意义吗?

c# - 无法理解为什么无法编译对象初始化程序中的数组初始化程序

c# - 用 C++ 托管 CLR 运行时