Actually, I cannot run the long running process in the Background thread. The long running process gets its input directly from the UI and then it has to save any modifications in the database. So, this long running process is unable to access the input in the background thread despite being written inside the Dispatch code(given below):

  this.Dispatcher.Invoke((Action)(() =>
   {
       ...//The long running process inside background thread.
   }));

My basic is to prevent the user from clicking anywhere else while the long running process is running. So, is there any other way by which this can be done?

有帮助吗?

解决方案

I would do it this way:

  • Make a "global" (seen for UI thread and background thread) object of one of concurrent types. You can make it as singleton or pass an object to both threads
  • All the long running thread does is reading data from concurrent object and process them.
  • If main thread (or any other) need to do anything in background simply adds it to the concurrent collection.

This kind of approach in concurrent programming is called Producer-consumer problem

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top