HTTP server error: The I/O operation has been aborted because of either a thread exit

image

HTTP server error: The I/O operation has been aborted because of either a thread exit or an application request.

สาเหตุ:
ข้อผิดพลาดนี้เกิดขึ้นเมื่อ HttpListener ถูกหยุด (Stop) หรือปิด (Close) ในขณะที่ยังมีการดำเนินการ I/O (เช่น การตอบสนองคำขอ HTTP) ที่ยังไม่เสร็จสมบูรณ์ ซึ่งเป็นพฤติกรรมปกติเมื่อคุณหยุดเซิร์ฟเวอร์ HTTP ในขณะที่ยังมีคำขอที่กำลังดำเนินการอยู่
วิธีแก้ไข:
คุณสามารถจัดการข้อผิดพลาดนี้ได้โดยการจับข้อยกเว้น (Exception) และตรวจสอบว่าเซิร์ฟเวอร์ถูกหยุดอย่างตั้งใจหรือไม่ เพื่อหลีกเลี่ยงการแสดงข้อความแสดงข้อผิดพลาดที่ไม่จำเป็น

private void StartHttpServer(string filePath)
{
    if (httpListener != null && httpListener.IsListening)
    {
        MessageBox.Show("HTTP server is already running.\", \"Info\", MessageBoxButtons.OK, MessageBoxIcon.Information);
        return;
    }

    httpListener = new HttpListener();
    httpListener.Prefixes.Add("http://localhost:8080/");
    httpServerThread = new Thread(() =>
    {
        try
        {
            httpListener.Start();

            // Update tb1.Text safely on the UI thread
            UpdateTextBoxSafe("http://localhost:8080");

            while (httpListener.IsListening)
            {
                try
                {
                    var context = httpListener.GetContext();
                    var response = context.Response;

                    byte[] buffer = File.ReadAllBytes(filePath);
                    response.ContentType = "image/webp";
                    response.ContentLength64 = buffer.Length;
                    response.OutputStream.Write(buffer, 0, buffer.Length);
                    response.OutputStream.Close();
                }
                catch (HttpListenerException ex)
                {
                    // Handle the case where the listener is stopped
                    if (httpListener == null || !httpListener.IsListening)
                    {
                        break; // Exit the loop if the listener is stopped
                    }
                    else
                    {
                        MessageBox.Show($"HTTP server error: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show($"HTTP server error: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show($"HTTP server error: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    });
    httpServerThread.IsBackground = true;
    httpServerThread.Start();
}

การเปลี่ยนแปลง:

  1. จับ HttpListenerException:
    • เพิ่ม catch (HttpListenerException ex) เพื่อจัดการข้อผิดพลาดที่เกิดจากการหยุดเซิร์ฟเวอร์ (Stop หรือ Close).
  2. ตรวจสอบสถานะของ httpListener:
    • หาก httpListener ถูกหยุด (httpListener == null หรือ !httpListener.IsListening), ให้ออกจากลูป while.
  3. หลีกเลี่ยงการแสดงข้อความแสดงข้อผิดพลาดที่ไม่จำเป็น:
    • หากเซิร์ฟเวอร์ถูกหยุดอย่างตั้งใจ จะไม่แสดงข้อความแสดงข้อผิดพลาด.

ผลลัพธ์:
• เมื่อคุณหยุดเซิร์ฟเวอร์ HTTP โดยใช้ปุ่ม “Stop HTTP Server”, ข้อผิดพลาดนี้จะไม่แสดงอีก.
• เซิร์ฟเวอร์จะหยุดทำงานอย่างปลอดภัยโดยไม่มีข้อความแสดงข้อผิดพลาดที่ไม่จำเป็น.