Click here to Skip to main content
15,891,423 members
Articles / Desktop Programming / WPF

ParallelWork: Feature rich multithreaded fluent task execution library for WPF

Rate me:
Please Sign up or sign in to vote.
4.94/5 (10 votes)
22 Mar 2010CPOL8 min read 49K   335   65  
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)


Written By
Architect BT, UK (ex British Telecom)
United Kingdom United Kingdom

Comments and Discussions