同時キューに最大 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
エラーはありません。キューにコピーがあると思います (画像ボックスがフリーズしているように見えるため)。整数で同じコードを試してみましたが、完全に機能します。どうしたの?