並行キューに〜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
エラーはありませんが。私はキュー内にコピーがあるかもしれないと思います(なぜなら、画像ボックスはフリーズしているように見えるからです)。私は整数で同じコードを試して、それは完全に動作します。どうしたの?
あなたが報告されていない例外を取得していることも可能です。それは多分、恐ろしいジェネリックGDIエラーが配置されているだけで3〜4のオブジェクトのように見えたり、オブジェクト – Plutonix
を使い果たしたときに、いくつかの他のがスローされているので、あなたは何を意味するのですか?私はピクチャのbocイメージと1つのビットマップ( "ビット")のみを配置します。あなたはそれらを意味しますか? –