BackgroundTaskManager<TArg, TResult> example

Aug 13, 2010 at 12:04 PM

Hi,

Can you please explain how to use BackgroundTaskManager<TArg, TResult>, V1 had only one argument, i don't know how to use the V2 one. can you provide one example too,

Regards

Vilsad

Aug 13, 2010 at 4:48 PM
Edited Aug 13, 2010 at 4:53 PM
Everything you need to know is right there is the summary comments of the class. But here it is again.
    //Have a property for the BackgroundTaskManager in the ViewModel
    public BackgroundTaskManager<Int32,Int32> BgWorker
    {
       get { return bgWorker; }
       set
       {
         bgWorker = value;
         OnPropertyChanged();
       }
    }

    //Then wire it up, somewhere in your VM
    bgWorker = new BackgroundTaskManager<Int32,Int32>(
            (argument) =>
                {
                    Int32 innerCount = 0;
                    for (innerCount = 0; innerCount < argument; innerCount++)
                    {
    
                    }
	            //this must be same type as TResultfor BackgroundTaskManager<TArg,TResult>
		    //Int32 in this case
                    return innerCount;  
                },
            (result) =>
                {
                   Count = result;
                });
         
    //Some ViewModel method, that needs threading as it takes a long time
    public void Test()
    {
	//this must be same type as TArg for BackgroundTaskManager<TArg,TResult>
	//Int32 in this case 
        bgWorker.WorkerArgument=15; 
        bgWorker.RunBackgroundTask();
    }
Basically the TArg generic is the argument (WorkerArgument above) to the internal BackGroundWorker, and the TResult is what you want the results (result in example above) back as from the internal BackGroundWorker.DoWork method
Aug 13, 2010 at 6:30 PM

thanks sacha. i had been using caliburn, trying this for the last few days.