简体   繁体   English

OnRender(DrawingContext drawingContext) C#

[英]OnRender(DrawingContext drawingContext) C#

I'm overriding OnRender(DrawingContext drawingContext) in an Adorner class in C#我在 C# 的装饰器 class 中覆盖 OnRender(DrawingContext drawingContext)

In this Method I'm trying to draw images stored in my imageCache Dictionary.在这个方法中,我试图绘制存储在我的 imageCache 字典中的图像。 The problem is OnRender Method gets called before the Thread that stores my images in the dictionary finishes.问题是在将我的图像存储在字典中的线程完成之前调用 OnRender 方法。

I tried to Thread.Sleep(60) this works but my program got really slow.我尝试了 Thread.Sleep(60) 这可行,但我的程序变得非常慢。

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.如果是这样,我建议您在图像存储线程完成之前不要在OnRender中执行任何操作。

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.例如,您可以引用带有旧图像的字典以进行渲染,然后在您的工作线程构建新字典后,您可以使用lock并为该引用分配一个新字典。

(That's all I can suggest with so little input) (这就是我可以用很少的输入提出的全部建议)

You can lock and synchronize the threads by using Mutex.您可以使用 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.这是MSDN教程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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