Click here to Skip to main content
15,897,371 members
Articles / Programming Languages / Visual Basic

Generic Wrapper for Easy Multithreaded Programming

Rate me:
Please Sign up or sign in to vote.
4.94/5 (34 votes)
2 Jun 2014CPOL14 min read 75.6K   710   126  
Event-based, generic wrapper and manager to implement multithreading in your applications

using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using Asynchronous = SIGLR.Async.GenericWrapper;

internal class Worker2 : Asynchronous.Worker.WorkerBase<Worker2Input, Worker2Progress, Worker2Result>
{

    #region "Fields"

    private Asynchronous.AsyncManager<int, SubWorker1Result> withEventsField_aAsyncManagerForSubWorker;
    private Asynchronous.AsyncManager<int, SubWorker1Result> aAsyncManagerForSubWorker
    {
        get { return withEventsField_aAsyncManagerForSubWorker; }
        set
        {
            if (withEventsField_aAsyncManagerForSubWorker != null)
            {
                withEventsField_aAsyncManagerForSubWorker.WorkerDone -= aAsyncManagerForSubWorker_WorkerDone;
            }
            withEventsField_aAsyncManagerForSubWorker = value;
            if (withEventsField_aAsyncManagerForSubWorker != null)
            {
                withEventsField_aAsyncManagerForSubWorker.WorkerDone += aAsyncManagerForSubWorker_WorkerDone;
            }
        }

    }

    private Worker2Progress progresData;
    #endregion

    #region "Constructor"


    public Worker2(Worker2Input inputParams)
        : base(inputParams)
    {

    }

    #endregion

    #region "Subs and Functions"

    protected override Worker2Result DoWork(Worker2Input inputParams)
    {

        Worker2Result result = new Worker2Result();
        progresData = new Worker2Progress();

        int number = 0;

        aAsyncManagerForSubWorker = new Asynchronous.AsyncManager<int, SubWorker1Result>("Id3", new SubWorker1(new SubWorker1Input("Enfant3", 100)));

        try
        {
            aAsyncManagerForSubWorker.StartWorker(base.IsAsynchronous);

            while (!base.InterruptionRequested)
            {
                number = number + 1;
                progresData.Number = number;
                progresData.Status = number.ToString();
                System.Threading.Thread.Sleep(150);

                base.WorkerProgressSignal(progresData);

                //To test an exception, uncomment the following line
                //Throw New Exception("This is an exception from the worker 2")

                if (number > 40)
                {
                    break; // TODO: might not be correct. Was : Exit Do
                }
            }

            if (base.InterruptionRequested)
            {
                result.ResultText = "Interrupted";
                aAsyncManagerForSubWorker_WorkerDone(this, aAsyncManagerForSubWorker.StopWorkerAndWait());
            }
            else
            {
                result.ResultText = "Finished";
            }

            if (aAsyncManagerForSubWorker.IsWorkerBusy)
            {
                aAsyncManagerForSubWorker_WorkerDone(this, aAsyncManagerForSubWorker.WaitForWorker());
            }

        }
        catch (Exception ex)
        {
            if (aAsyncManagerForSubWorker.IsWorkerBusy)
            {
                aAsyncManagerForSubWorker_WorkerDone(this, aAsyncManagerForSubWorker.StopWorkerAndWait());
            }
            throw;
        }

        return result;

    }

    #endregion

    #region "SubWorker 1 Events"


    private void aAsyncManagerForSubWorker_WorkerDone(object sender, Asynchronous.Events.WorkerDoneEventArgs<SubWorker1Result> e)
    {
        if (e.Exception == null)
        {
            if (e.Interrupted)
            {
                progresData.SubWorker1Status = e.Result.ResultText;
            }
            else
            {
                progresData.SubWorker1Status = e.Result.ResultText + " Some total: " + e.Result.SomeTotal;
            }
        }
        else
        {
            progresData.SubWorker1Status = "Exception " + e.Identity + ":" + e.Exception.ToString();
        }

        base.WorkerProgressSignal(progresData);

    }

    #endregion

}

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 Services Informatiques GLR Inc.
Canada Canada
I'm an independant software architect working as consultant for various companies in the region of Quebec City, Canada.

I have close to 20 years of experience in development of applications and systems of various scales, in many different lines of business.

Since 2003, I have exclusively worked in .NET projects, switching roles from senior designer to lead designer and software architect. I have also contributed to the development of the Web Service Software Factory (WSSF) from Microsoft's Patterns and Practices team, as adviser.

Comments and Discussions