form1 top で私がした:
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:
どうすれば解決できますか?