如何访问在另一个线程中创建的位图 [英] How to access Bitmap created in another thread

查看:70
本文介绍了如何访问在另一个线程中创建的位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对C#还是相当陌生,尤其是多线程,这确实使我耳目一新.似乎应该是一个简单的答案,但我看不到.

假设我有一个c#库,它在自己的线程中运行某些过程.该线程创建一个位图,我想将其传递回使用该库的应用程序.因此,我将位图传递给委托.现在,此位图可能会显示在主窗体的PictureBox等中,也可能不会显示.如果始终是控件(例如PictureBox),则可以将控件的引用传递给库的线程,并使用Control.Invoke来更新控件,并保持线程安全.

但是,如果它不是窗口控件,该怎么办?如何以我喜欢的方式从主应用程序访问/使用此位图,并且仍然保持线程安全?我不希望库中的其他用户必须处理lock()等.那么这是怎么做的呢?似乎我必须以某种方式阻塞库的线程,直到委托返回.但是如何?

我阅读了我发布的消息,这没有任何意义.我要问的是:
如果我有一个单独的线程通过委托与UI线程通信,那么当该委托未与Control关联时,如何使该委托在UI线程中运行?笨拙地无法调用Control.Invoke.

I''m fairly new to c#, and especially multi-threading, and this is really doing my head in. It seems it should be a simple answer but I don''t see it.

Let''s say I have a c# library that runs certain procedures in it''s own thread. This thread creates a Bitmap which I want to pass back to the application that uses the library. So I pass the Bitmap through a delegate. Now, this Bitmap may be displayed in a PictureBox etc on the main form, or it may not. If it always was a Control (eg, a PictureBox), then I could pass a reference of the control to the library''s thread and use Control.Invoke to update the control, and remain thread-safe.

But what if it wasn''t a windowed control? How do I go about accessing/using this Bitmap in any way I like from the main app and still remain thread-safe? I don''t expect other users of my library to have to deal with lock() etc. So how is this done? It seems I would have to block the library''s thread somehow until the delegate has returned. But how?

I read my posted message and it doesn''t make sense. What I''m asking is:
If I have a separate thread which communicates with the UI thread through a delegate, how can I make that delegate run in the UI thread when it may not be associated with a Control? Obiously Control.Invoke can''t be called.

推荐答案

我不久前回答了这个问题.第一个问题是关于代表的,还提供了有关有关调用的其他信息"的其他信息.建议您完整阅读它,因为它会为您提供一些很好的信息.

PLZ帮助我进行说明 [
I answered this question a while ago. The first question was about delegates and also provided extra information about "Extra information about Invoke". It would be recommended to read it completely because it will give you some good info I think.

PLZ HELP ME EXPLAINING[^]

Good luck!


我的问题的更新.

如前所述,我需要做到通用".换句话说,后台线程不应该知道如何使用它,也不要求将Control传递给线程,以便可以调用Control.Invoke.

答案是直接使用ISynchronizeInvoke接口.我在StackOverflow上找到了一些示例代码,并且正在像这样使用它(一个不同的项目,但是同样的问题/解决方案):

An update to my question.

As explained previously I needed this to be "universal". In other words the background thread shouldn''t have any idea on how it will be used, or require a Control to be passed to the thread so Control.Invoke can be called.

The answer was to use the ISynchronizeInvoke interface directly. I found some sample code at StackOverflow and am using it like this (a different project, but same problem/solution):

private void HandleSyncCallback(Bitmap tile, int left, int top, CancelRenderArgs e)
{
    Delegate[] handlers = _renderArgs.trCallBack.GetInvocationList();
    foreach (var del in handlers)
    {
        ISynchronizeInvoke target = del.Target as ISynchronizeInvoke;
        if ( (target != null))
        {
            if (target.InvokeRequired)
            {
                target.Invoke(del, new object[] {tile, left, top, e});
            }
            else
            {
                del.DynamicInvoke(new object[] {tile, left, top, e});
            }
        }

    }
}



可以治疗.

谢谢,
戴夫.



Works a treat.

Thanks,
Dave.


这篇关于如何访问在另一个线程中创建的位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆