简体   繁体   中英

OnRender(DrawingContext drawingContext) C#

I'm overriding OnRender(DrawingContext drawingContext) in an Adorner class in C#

In this Method I'm trying to draw images stored in my imageCache Dictionary. The problem is OnRender Method gets called before the Thread that stores my images in the dictionary finishes.

I tried to Thread.Sleep(60) this works but my program got really slow.

Can you guys give me ideas how I can overpass this issue.

Thank you.

Does you thread which stores images in a dictionary run once? If so, I suggest you to do nothing in your OnRender until your image-storing thread finishes.

But if your image-storing thread runs for every render or frequently, you might need to implement some thread syncronization (well you'll need it for the fist case too). For example, you can have reference to a dictionary with old images for rendering purposes, then after your worker thread built a new dictionary, you can use lock and assign a new dictionary to that reference.

(That's all I can suggest with so little input)

You can lock and synchronize the threads by using Mutex.

private static Mutex mut = new Mutex();

and then you can lock and release the methods you want by using

mut.WaitOne();
mut.ReleaseMutex();

and here is the MSDN tutorial.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM