質問

I have a C# program that is checking for the existence of files and registry keys and using AppendText() on a Form.Textbox for every file and registry key it is looking for.

This is running in an asynchronous BackgroundWorker and now my background worker is actually processing so quickly that my UI thread gets clogged up with AppendText commands and the program crashes. How can I synchronize the BackgroundWorker and UI thread so that the BackgroundWorker doesn't start processing again until the UI thread is ready?

役に立ちましたか?

解決

You have a BackgroundWorker, so you have everything you need. The progress event is called on the UI thread, so if the worker reports progress in the standard way (by calling ReportProgress(int32, object), and passing the string to be appended as the second parameter, the form's progress handler can call AppendText. The background thread will never outrun the UI that way.

I suspect you're calling BeginInvoke to update your UI. Another way to solve the problem would be to call Invoke.

You can do much better if you have your background worker save up, say, 10 lines, and then pass them all to the progress event at once. That'll reduce the overhead of updates and cause your background thread to process faster.

他のヒント

One way that comes to mind is queuing the commands and allowing the UI thread to handle them at its own speed :: IE create a generic list (let's call it appendtext) of the arguments, the background worker adds to the list and the UI thread reads, calls, then deletes from the list

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top