Click here to Skip to main content
15,895,370 members
Home / Discussions / WPF
   

WPF

 
GeneralRe: Problem executing BitmapDecoder.Save() in worker thread Pin
Mark Salsbery1-Jun-09 13:46
Mark Salsbery1-Jun-09 13:46 
General[Message Deleted] Pin
fjparisIII1-Jun-09 14:23
fjparisIII1-Jun-09 14:23 
GeneralRe: Problem executing BitmapDecoder.Save() in worker thread Pin
Mark Salsbery1-Jun-09 14:33
Mark Salsbery1-Jun-09 14:33 
General[Message Deleted] Pin
fjparisIII1-Jun-09 14:43
fjparisIII1-Jun-09 14:43 
GeneralRe: Problem executing BitmapDecoder.Save() in worker thread Pin
Mark Salsbery1-Jun-09 15:23
Mark Salsbery1-Jun-09 15:23 
General[Message Deleted] Pin
fjparisIII1-Jun-09 16:05
fjparisIII1-Jun-09 16:05 
GeneralRe: Problem executing BitmapDecoder.Save() in worker thread Pin
Mark Salsbery1-Jun-09 17:09
Mark Salsbery1-Jun-09 17:09 
GeneralRe: Problem executing BitmapDecoder.Save() in worker thread [modified] Pin
Mark Salsbery1-Jun-09 14:13
Mark Salsbery1-Jun-09 14:13 
Cancellation and other non-Component properties implemented...
//<code>*edit* added synchronization to CancellationPending (maybe overkill)</code>

public class WPFSTABackgroundWorker
{
    private delegate void InvokerDelegate();

    public event DoWorkEventHandler DoWork;
    public event ProgressChangedEventHandler ProgressChanged;
    public event RunWorkerCompletedEventHandler RunWorkerCompleted;

    public bool WorkerReportsProgress { get; set; }
    public bool IsBusy { get; protected set; }
    public bool WorkerSupportsCancellation { get; set; }
    private bool _cancellationPending;
    public bool CancellationPending
    {
        get
        {
            bool ret;
            lock (cancellock)
            {
                ret = _cancellationPending;
            }
            return ret;
        }
        protected set
        {
            lock (cancellock)
            {
                _cancellationPending = value;
            }
        }
    }


    public WPFSTABackgroundWorker()
    {
        IsBusy = false;
        WorkerReportsProgress = false;
        WorkerSupportsCancellation = false;
        CancellationPending = false;
    }

    public void RunWorkerAsync()
    {
        RunWorkerAsync(null);
    }

    public void RunWorkerAsync(Object argument)
    {
        IsBusy = true;
        threadargument = argument;
        thread = new Thread(ThreadProc);
        thread.SetApartmentState(ApartmentState.STA);
        thread.Priority = ThreadPriority.Normal;
        thread.Start(this);
    }

    public void CancelAsync()
    {
        if (!WorkerSupportsCancellation)
            throw new InvalidOperationException("Attempted to CancelAsync when WorkerSupportsCancellation is false.");

        CancellationPending = true;
    }

    public void ReportProgress(int percentProgress)
    {
        ReportProgress(percentProgress, null);
    }

    public void ReportProgress(int percentProgress, Object userState)
    {
        if (!WorkerReportsProgress)
            throw new InvalidOperationException("Attempted to ReportProgress when WorkerReportsProgress is false.");

        ProgressChangedEventHandler progresshandler = ProgressChanged;
        if (progresshandler != null)
        {
            Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (InvokerDelegate)delegate
            {
                progresshandler(this, new ProgressChangedEventArgs(percentProgress, userState));
            });
        }
    }


    Thread thread;
    Object threadargument;

    static void ThreadProc(object context)
    {
        WPFSTABackgroundWorker worker = context as WPFSTABackgroundWorker;

        DoWorkEventHandler workhandler = worker.DoWork;
        if (workhandler != null)
        {
            workhandler(worker, new DoWorkEventArgs(worker.threadargument));
        }

        RunWorkerCompletedEventHandler completedhandler = worker.RunWorkerCompleted;
        if (completedhandler != null)
        {
            Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (InvokerDelegate)delegate
            {
                completedhandler(worker, new RunWorkerCompletedEventArgs(null, null, worker.CancellationPending));
            });
        }

        worker.IsBusy = false;
    }
}


Mark Salsbery
Microsoft MVP - Visual C++

Java | [Coffee]

modified on Monday, June 1, 2009 8:23 PM

General[Message Deleted] Pin
fjparisIII1-Jun-09 16:39
fjparisIII1-Jun-09 16:39 
GeneralRe: Problem executing BitmapDecoder.Save() in worker thread Pin
Mark Salsbery1-Jun-09 17:06
Mark Salsbery1-Jun-09 17:06 
General[Message Deleted] Pin
fjparisIII1-Jun-09 17:56
fjparisIII1-Jun-09 17:56 
General[Message Deleted] Pin
fjparisIII2-Jun-09 4:40
fjparisIII2-Jun-09 4:40 
General[Message Deleted] Pin
fjparisIII2-Jun-09 5:33
fjparisIII2-Jun-09 5:33 
GeneralRe: Problem executing BitmapDecoder.Save() in worker thread [modified] Pin
fjparisIII2-Jun-09 6:24
fjparisIII2-Jun-09 6:24 
GeneralRe: Problem executing BitmapDecoder.Save() in worker thread Pin
Mark Salsbery2-Jun-09 6:36
Mark Salsbery2-Jun-09 6:36 
GeneralRe: Problem executing BitmapDecoder.Save() in worker thread [modified] Pin
fjparisIII2-Jun-09 7:00
fjparisIII2-Jun-09 7:00 
GeneralRe: Problem executing BitmapDecoder.Save() in worker thread Pin
Mark Salsbery2-Jun-09 6:43
Mark Salsbery2-Jun-09 6:43 
GeneralRe: Problem executing BitmapDecoder.Save() in worker thread Pin
Mark Salsbery2-Jun-09 6:45
Mark Salsbery2-Jun-09 6:45 
GeneralRe: Problem executing BitmapDecoder.Save() in worker thread Pin
Mark Salsbery2-Jun-09 6:29
Mark Salsbery2-Jun-09 6:29 
General[Message Deleted] Pin
fjparisIII2-Jun-09 7:52
fjparisIII2-Jun-09 7:52 
GeneralRe: Problem executing BitmapDecoder.Save() in worker thread Pin
Mark Salsbery2-Jun-09 8:07
Mark Salsbery2-Jun-09 8:07 
General[Message Deleted] Pin
fjparisIII2-Jun-09 8:22
fjparisIII2-Jun-09 8:22 
GeneralRe: Problem executing BitmapDecoder.Save() in worker thread Pin
Mark Salsbery2-Jun-09 8:34
Mark Salsbery2-Jun-09 8:34 
General[Message Deleted] Pin
fjparisIII2-Jun-09 9:25
fjparisIII2-Jun-09 9:25 
QuestionRe: Problem executing BitmapDecoder.Save() in worker thread Pin
Mark Salsbery2-Jun-09 11:29
Mark Salsbery2-Jun-09 11:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.