C# .NET Mutex operations

Started by
4 comments, last by Telastyn 12 years ago
I am having issues with the GUI Window of my program not responding.

I have a thread pool running doing calculations in the background and when they send their results to a middle ground they call "mutex.waitOne()" then they add their results to a middle ground and then "mutex.release()" upon completion.

I also have the same 2 lines in the mainGUI's code ( waitOne() and release() ) during a timer event that checks if more results have been added to the middle ground.

The problem is when the threads add their results to the middle ground it has to be sorted with the other data in the middle ground and it takes a while to release the mutex. While they are uploading their data the main GUI tries to access it at the same time and has to wait because of the mutex. Which is almost how I want it to work because 2 threads can not access the same data at the same time.

How can I make this more effecient?

I do not want the Window to stop responding at random and having to wait on the other threads.

Is it somehow possible that when the Main GUI thread trys to check the middle ground that the other threads could pause until it's done?

Any ideas welcome.

Thanks

CoderWalker
If this post was helpful please +1 or like it !

Webstrand
Advertisement
Why don't you wait until all of the processing/sorting is done before writing it to the location that the UI picks it up?

Why don't you wait until all of the processing/sorting is done before writing it to the location that the UI picks it up?


The problem is the middleground contains basically a tree of data. When a thread is done making it's calculations it has to go threw the trees and add it's results in the proper places. If something new was added it adds that result and location to a queue. When the GUI thread checks for new data it checks the queue.
So basically as of right now the function for adding the threads results and the function for checking for new results both access the same queue.
If this post was helpful please +1 or like it !

Webstrand
So use a ConcurrentQueue and be done with it.

So use a ConcurrentQueue and be done with it.


Would that create alot of overhead? I assume it has mutex objects in every call?
If this post was helpful please +1 or like it !

Webstrand
Nope. It's a .NET implementation of a lockless queue.

This topic is closed to new replies.

Advertisement