동시 대기열에 ~ 300 비트 맵을 저장하고 있습니다. 나는 over-tcp 비디오 스트리밍 프로그램을 위해 이것을하고있다. 서버가 느려지면이 큐에 수신 된 비트 맵을 저장합니다 (버퍼링). 이 테스트를 위해 별도의 프로젝트를 만들었지 만 몇 가지 문제가 있습니다.대기열에서 읽기 및 쓰기
쓰기 쓰레드가 작동 중일 때 (대기열에 쓰는 동안) 그림 상자는 대기열의 이미지를 보여주고 있지만 그 중 많은 수가 건너 뛴 것처럼 보입니다 ("목록"에 방금 추가 된 그림을 읽는 것처럼 보입니다) 쓰기 스레드 (FIFO 동작이 아님). 큐에서 읽은 루프가 여전히 작동하고 있지만 (그림 상자가 큐를 비울 때 큐가 비어 있지는 않지만) 쓰기 스레드가 그림 상자를 완료하면 차단됩니다.
여기에 코드입니다 :
Imports System
Imports System.Drawing
Imports System.IO
Imports System.Threading
Imports System.Collections.Concurrent
Public Class Form1
Dim writeth As New Thread(AddressOf write), readth As New Thread(AddressOf read)
Dim que As New ConcurrentQueue(Of Bitmap), finished As Boolean
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
'Start button
writeth.Start()
readth.Start()
End Sub
Sub draw(ByRef pic As Bitmap)
If PictureBox1.Image IsNot Nothing Then
PictureBox1.Image.Dispose()
PictureBox1.Image = Nothing
End If
PictureBox1.Image = pic
End Sub
Sub read()
Dim bit As Bitmap
While (Not finished Or Not que.IsEmpty)
If que.TryDequeue(bit) Then
draw(bit.Clone)
'Still working after the writing stopped
If finished Then Debug.Print("picture:" & que.Count)
Thread.Sleep(2000) 'Simulates the slow-down of the server
End If
End While
End Sub
Sub write()
Dim count As Integer = 0
Dim crop_bit As New Bitmap(320, 240), bit As Bitmap
Dim g As Graphics = Graphics.FromImage(crop_bit)
For Each fil As String In Directory.GetFiles(Application.StartupPath & "/pictures")
count += 1
Debug.Print(count)
bit = Image.FromFile(fil)
g.DrawImage(bit, 0, 0, 320, 240)
que.Enqueue(crop_bit)
bit.Dispose()
Next
finished = True
'At this point the picture box freezes but the reading loop still works
End Sub
End Class
가 오류가 없습니다. 그림 상자가 고정 된 것처럼 보이므로 대기열에 복사본이있을 수 있습니다. 나는 정수로 같은 코드를 시도하고 완벽하게 작동합니다. 뭐가 문제 야?
보고되지 않은 예외가 발생할 수 있습니다. 4 개체 중 3 개체 만 처분되고있는 것처럼 보이기 때문에 개체가 부족할 때 두려운 일반 GDI 오류 또는 기타 문제가 발생할 수 있습니다. – Plutonix
무엇을 의미합니까? 나는 그림 boc 이미지와 하나의 비트 맵 ("비트") 만 처리합니다. 그걸 의미하니? –