线程在C#中的位图 [英] Threadsafe over Bitmap in C#

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

问题描述

谁能告诉我为什么这个code是错误的,我怎么也得修改,以创建一个简单的位图,其中是存在保证线程安全的访问?

 公共静态类ThreadSafe的
{
    公共静态只读对象_locker =新的对象();
    公共静态位图_snapshot;    公共静态位图快照
    {
        得到
        {
            锁(_locker)
            {
                返回_snapshot;
            }
        }
        组
        {
            锁(_locker)
            {
                _snapshot =价值;
            }
        }
    }}

修改我想怎么使用位图:​​


  • 线程A使用位图是这样的:


  

新位图(ThreadSafe.Snapshot,新System.Drawing.Size(320,240));



  • 线程B使用位图是这样的:


  

ThreadSafe.Snapshot =新位图(目标,新System.Drawing.Size(320,240));



解决方案

从的位图文件;


  

任何公共static这种类型的成员(在Visual Basic中的Shared)是线程安全的。任何实例成员不能保证是线程安全的。


...这应该是接近最低锁定落实;

 公共静态类ThreadSafe的
{
    私人静态只读对象BitmapLock =新的对象();
    私有静态位图_snapshot;    公共静态位图快照
    {
        得到
        {
            锁定(BitmapLock)
                返回新位图(_snapshot);
        }
        组
        {
            位图oldSnapshot;
            位图newSnapshot =新位图(价值,新的大小(320,240));
            锁定(BitmapLock)
            {
                oldSnapshot = _snapshot;
                _snapshot = newSnapshot;
            }
            如果(oldSnapshot!= NULL)
                oldSnapshot.Dispose();
        }
    }}

Can anyone tell me why this code is wrong and how I have to modify it in order to create a simple Bitmap of which is guranteed thread-safe access?

public static class ThreadSafe
{
    public static readonly object _locker = new object();
    public static Bitmap _snapshot;

    public static Bitmap Snapshot
    {
        get
        {
            lock (_locker)
            {
                return _snapshot;
            }
        }
        set
        {
            lock (_locker)
            {
                _snapshot = value;
            }
        }
    }

}

EDIT How I want use that Bitmap:

  • Thread A use the Bitmap in this way:

new Bitmap(ThreadSafe.Snapshot, new System.Drawing.Size(320, 240));

  • Thread B uses the Bitmap in this way:

ThreadSafe.Snapshot = new Bitmap(target, new System.Drawing.Size(320, 240));

解决方案

Following the thread safety guidelines from the Bitmap documentation;

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

...this should be close to the minimum locking implementation;

public static class ThreadSafe
{
    private static readonly object BitmapLock = new object();
    private static Bitmap _snapshot;

    public static Bitmap Snapshot
    {
        get
        {
            lock (BitmapLock)
                return new Bitmap(_snapshot);
        }
        set
        {
            Bitmap oldSnapshot;
            Bitmap newSnapshot = new Bitmap(value, new Size(320, 240));
            lock (BitmapLock) 
            {
                oldSnapshot = _snapshot;
                _snapshot = newSnapshot;
            }
            if (oldSnapshot != null)
                oldSnapshot.Dispose();
        }
    }

}

这篇关于线程在C#中的位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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