streamWriterBufferedDataLost MDA

The streamWriterBufferedDataLost مدارة تصحيح الأخطاء assistant (MDA) هو activated when a StreamWriter هو written إلى, but the Flush أو Close أسلوب ليس subsequently called قبل the مثيل of the StreamWriter هو destroyed. When this MDA هو ممكّن, the وقت التشغيل determines whether أي buffered بيانات still موجود within the StreamWriter. If buffered بيانات does exist, the MDA هو activated. Calling the Collect و WaitForPendingFinalizers وظائف can force finalizers إلى تشغيل. Finalizers will otherwise run at seemingly arbitrary مرة/مرات, و possibly not at الجميع تشغيل عملية خروج. بوضوح تشغيل finalizers مع this MDA ممكّن will تعليمات إلى المزيد reliably reproduce this نوع of problem.

الأعراض

A StreamWriter does not write the أخير 1–4 KB of بيانات إلى a ملف.

السبب

The StreamWriter buffers بيانات internally, which يتطلب that the Close أو Flush أسلوب be called إلى write the buffered بيانات إلى the underlying بيانات store. إذا Closeأو Flushهو استدعاء غير مناسب، البيانات مؤقتاً في StreamWriterالمثيل قد لا يمكن كتابتها كما هو متوقع.

The following هو an مثال of poorly written تعليمات برمجية that this MDA should catch.

// Poorly written code.
void Write() 
{
    StreamWriter sw = new StreamWriter("file.txt");
    sw.WriteLine("Data");
    // Problem: forgot to close the StreamWriter.
}

The preceding تعليمات برمجية will تنشيط this MDA المزيد reliably if a garbage مجموعة هو triggered و then معلق until finalizers have finished. إلى مقطع صوتي أسفل this نوع of problem, you can إضافة the following تعليمات برمجية إلى the إنهاء of the preceding أسلوب في a يصحح بنية. This will تعليمات إلى reliably تنشيط the MDA, but of course it does not fix the cause of the problem.

    GC.Collect();
    GC.WaitForPendingFinalizers();

الدقة

Make sure you يتصل Close أو Flush تشغيل the StreamWriter قبل closing an تطبيق أو أي تعليمات برمجية حظر that has an مثيل of a StreamWriter. واحد of the best mechanisms for achieving this هو creating the مثيل مع a C# using حظر (Using في Visual أساسى), which will ensure the Dispose أسلوب for the writer هو invoked, resulting في the مثيل being correctly مغلق.

using(StreamWriter sw = new StreamWriter("file.txt")) 
{
    sw.WriteLine("Data");
}

The following تعليمات برمجية shows the same الحل, using try/finally instead of using.

StreamWriter sw;
try 
{
    sw = new StreamWriter("file.txt"));
    sw.WriteLine("Data");
}
finally 
{
    if (sw != null)
        sw.Close();
}

If لا هذا ولا ذاك of these solutions can be used (for مثال, if a StreamWriter هو stored في a ثابت متغير و you cannot بسهولة run تعليمات برمجية at the إنهاء of its مدة بقاء), then calling Flush تشغيل the StreamWriter بعد its أخير استخدم أو إعداد the AutoFlush خاصية إلى true قبل its أول استخدم should avoid this problem.

private static StreamWriter log;
// static class constructor.
static WriteToFile() 
{
    StreamWriter sw = new StreamWriter("log.txt");
    sw.AutoFlush = true;

    // Publish the StreamWriter for other threads.
    log = sw;
}

التأثير تشغيل وقت التشغيل

This MDA has بلا تأثير تشغيل the وقت التشغيل.

الإخراج

A رسالة indicating that this violation occurred.

التكوين

<mdaConfig>
  <assistants>
    <streamWriterBufferedDataLost />
  </assistants>
</mdaConfig>

راجع أيضًا:

المرجع

StreamWriter

المبادئ

تشخيص الأخطاء مع المساعدين التصحيح مدارة