Click here to Skip to main content
12,509,269 members (58,272 online)
Click here to Skip to main content

Stats

35.1K views
303 downloads
64 bookmarked
Posted

ParallelWork: Feature rich multithreaded fluent task execution library for WPF

, 22 Mar 2010 CPOL
ParallelWork is an open source free helper class that lets you run multiple work in parallel threads, get success, failure and progress update on the WPF UI thread, wait for work to complete, abort all work (in case of shutdown), queue work to run after certain time, chain parallel work one after an
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Utilities
{
    using System;
    using System.Runtime.InteropServices;

    public class Weak<T> : IDisposable        
    {
        private GCHandle handle;
        private bool trackResurrection;

        public Weak(T target)
            : this(target, false)
        {
        }

        public Weak(T target, bool trackResurrection)
        {
            this.trackResurrection = trackResurrection;
            this.Target = target;
        }

        ~Weak()
        {
            Dispose();
        }

        public void Dispose()
        {
            handle.Free();
            GC.SuppressFinalize(this);
        }

        public virtual bool IsAlive
        {
            get { return (handle.Target != null); }
        }

        public virtual bool TrackResurrection
        {
            get { return this.trackResurrection; }
        }

        public virtual T Target
        {
            get
            {
                object o = handle.Target;
                if ((o == null) || (!(o is T)))
                    return default(T);
                else
                    return (T)o;
            }
            set
            {
                if (handle != null)
                    if (handle.IsAllocated)
                        handle.Free();

                handle = GCHandle.Alloc(value,
                  this.trackResurrection ? GCHandleType.WeakTrackResurrection : GCHandleType.Weak);
            }
        }

        public static implicit operator Weak<T>(T obj)  
        {
            return new Weak<T>(obj);
        }

        public static implicit operator T(Weak<T> weakRef)  
        {
            return weakRef.Target;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author


You may also be interested in...

Pro
Pro
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160927.1 | Last Updated 22 Mar 2010
Article Copyright 2010 by Omar Al Zabir
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid