在Form1頂部我所做的:我在一個計時器滴答事件中使用位圖,一切都很慢,我該如何解決它?
private volatile bool _workersEnabled;
private ConcurrentQueue<Bitmap> _imageBuffer;
Thread threadA;
Thread threadB;
然後在構造函數中:
_workersEnabled = false;
_imageBuffer = new ConcurrentQueue<Bitmap>();
threadA = new Thread(CaptureScreensEntryPoint);
threadB = new Thread(ConsumeScreensEntryPoint);
然後在按鈕單擊事件:
private void StartRecording_Click(object sender, EventArgs e)
{
ffmp.Start("test.avi", 25);
_workersEnabled = true;
threadA.Start();
threadA.Start();
//Disable the button, so we eliminate the possibility to start this twice (would throw an exception anyway).
StartRecording.Enabled = false;
}
然後之後我說:
private void CaptureScreensEntryPoint()
{
while(_workersEnabled)
{
Bitmap bitmap = (Bitmap)ScreenCapture.CaptureScreen(true);
//Just add it to the queue.
_imageBuffer.Enqueue(bitmap);
//Wait a bit
Thread.Sleep(40);
}
}
private void ConsumeScreensEntryPoint()
{
while (_workersEnabled)
{
Bitmap workItem = null;
if (_imageBuffer.TryDequeue(out workItem))
{
ffmp.PushFrame(workItem);
workItem.Dispose();
}
//Also wait a bit here. Don't want to eat up the entire processor.
Thread.Sleep(10);
}
}
例外是threadA.Start();
:
ThreadStateException:線程正在運行或終止;它不能重新啓動
System.Threading.ThreadStateException was unhandled
HResult=-2146233056
Message=Thread is running or terminated; it cannot restart.
Source=mscorlib
StackTrace:
at System.Threading.Thread.StartupSetApartmentStateInternal()
at System.Threading.Thread.Start(StackCrawlMark& stackMark)
at System.Threading.Thread.Start()
at ScreenVideoRecorder.Form1.StartRecording_Click(Object sender, EventArgs e) in d:\C-Sharp\ScreenVideoRecorder\ScreenVideoRecorderWorkingVersion\Form1.cs:line 152
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at ScreenVideoRecorder.Program.Main() in d:\C-Sharp\ScreenVideoRecorder\ScreenVideoRecorderWorkingVersion\Program.cs:line 18
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
我怎樣才能解決呢?
是的,在工作線程上執行它。讓它異步推動框架。您可能還想將幀請求推送到工作線程中的隊列中,並讓它執行它的工作(當有事情要做時)。 –
marceln你能告訴我一個例子如何做到背景工作者和隊列或沒有隊列嗎? –
請參閱下面的答案。這不是一個後臺工作者,它更簡單一些。 –