Multithreading with TPL – access to internal class properties

I use the TPL library to parallelize a 2D grid operation. I extracted a simple example from my current code to illustrate what I'm doing. I get the results I want and my calculation times are accelerated by the number of processors on my laptop (12).

I'd like to get advice or opinions on my code when it comes to how my properties are declared. Again it works as expected, but I wonder if the design could be better. Thank you in advance.

My simplified code:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Gridding
{


    public abstract class Base
    {
        /// 
        /// Values representing the mesh values where each node in the gridis assigned som value. 
        /// 
        public float(,) Values { get; set; }
        public abstract void Compute();
    }


    public class Derived : Base
    {

        /// 
        /// Make the mesh readonly.  Is this necessary?
        /// 
        readonly Mesh MyMesh;

        Derived(Mesh mesh)
        {
            MyMesh = mesh;
        }


        public override void Compute()
        {
            Values = new float(MyMesh.NX, MyMesh.NY);

            double() xn = MyMesh.GetXNodes();
            double() yn = MyMesh.GetYNodes();

            /// Paralellize the threads along the columns of the grid using the Task Paralllel Library (TPL).
            Parallel.For(0, MyMesh.NX, i =>
            {
                Run(i, xn, yn);
            });
        }


        private void Run(int i, double() xn, double() yn)
        {
            /// Some long operation that parallelizes along the columns of a mesh/grid
            double x = xn(i);
            for (int j = 0; j < MyMesh.NY; j++)
            {

                /// Again, longer operation here
                double y = yn(j);

                double someValue = Math.Sqrt(x * y); 
                Values(i, j) = (float)someValue;
            }
        }


        static void Main(string() args)
        {
            int nx = 100;
            int ny = 120;
            double x0 = 0.0;
            double y0 = 0.0;
            double width = 100;
            double height = 120;

            Mesh mesh = new Mesh(nx, ny, x0, y0, width, height);

            Base tplTest = new Derived(mesh);
            tplTest.Compute();

            float(,) values = tplTest.Values;

            Console.Read();


        }

        /// 
        /// A simple North-South oriented grid.
        /// 
        class Mesh
        {
            public int NX { get; } = 100;
            public int NY { get; set; } = 150;
            public double XOrigin { get; set; } = 0.0;
            public double YOrigin { get; set; } = 0.0;
            public double Width { get; set; } = 100.0;
            public double Height { get; set; } = 150.0;
            public double DX { get; }
            public double DY { get; }

            public Mesh(int nx, int ny, double xOrigin, double yOrigin, double width, double height)
            {
                NX = nx;
                NY = ny;
                XOrigin = xOrigin;
                YOrigin = yOrigin;
                Width = width;
                Height = height;
                DX = Width / (NX - 1);
                DY = Height / (NY - 1);
            }

            public double() GetYNodes()
            {
                double() yNodeLocs = new double(NY);
                for (int i = 0; i < NY; i++)
                {
                    yNodeLocs(i) = YOrigin + i * DY;
                }
                return yNodeLocs;
            }

            public double() GetXNodes()
            {
                double() xNodeLocs = new double(NX);
                for (int i = 0; i < NX; i++)
                {
                    xNodeLocs(i) = XOrigin + i * DX;
                }
                return xNodeLocs;
            }
        }
    }
}

async await – TPL data flow – timer action block

I'm new to TPL Dataflow, and I've been looking for an action block that can move an object to a timer-specifically, to create a heartbeat at each interval. I could not find anything out of the box, so I decided to create a packaging class on action block that would give me the desired functionality. I also wanted to include an async / wait pattern in the implementation.

Given the following heartbeat object:

public class Heartbeat
{
    public DateTime HearbeatTimeUtc { get; set; }
}

The wrapper class:

public class TimerActionBlock
{
    private readonly ActionBlock _actionBlock;

    public TimerActionBlock(Func func,
        Func excptionFunc)
    {
        _actionBlock = new ActionBlock(async input =>
        {
            try
            {
                await func(input);
            }
            catch (Exception e)
            {
                await excptionFunc(input, e);
            }
        });
    }

    public void Start(Func> inputFunc, 
        TaskScheduler scheduler,
        TimeSpan dueTime,
        TimeSpan period,
        CancellationToken cancellationToken,
        Action onError)
    {
        Task
            .Factory
            .StartNew(async () =>
                {
                    await Task.Delay(dueTime);

                    while (true)
                    {
                        var input = await inputFunc();

                        await _actionBlock.SendAsync(input);

                        var task = Task.Delay(period,
                            cancellationToken);

                        try
                        {
                            await task;
                        }
                        catch (TaskCanceledException)
                        {
                            return;
                        }
                    }
                }, 
                cancellationToken, 
                TaskCreationOptions.LongRunning, 
                scheduler)
            .ContinueWith(task => { onError(task.Exception); }, TaskContinuationOptions.OnlyOnFaulted);
    }
}

Calling the code would look something like this:

var timerActionBlock = new TimerActionBlock(heartbeat =>
    {
        Console.WriteLine(JsonConvert.SerializeObject(heartbeat));
        return Task.CompletedTask;
    },
    (heartbeat, exception) =>
    {
        Console.WriteLine(exception);
        return Task.CompletedTask;
    });

timerActionBlock
    .Start(() => Task.FromResult(new Heartbeat
        {
            HearbeatTimeUtc = DateTime.UtcNow
        }), 
        TaskScheduler.Default, 
        TimeSpan.FromSeconds(10), 
        TimeSpan.FromMilliseconds(1000), 
        CancellationToken.None, 
        Console.WriteLine);

Using TPL when unpacking files

I've been working on an implementation of my own collection of Astron libraries from scratch, and I wanted to check my TPL usage because I'm unfamiliar with this technology: /

My app is currently unpacking .d2p files, a custom file format from a French game called Dofus. It is simply an archive of other inflated archives of another custom .dlm file format. Here is the latest issue of my app:

application edition
The progress bars came from here.

So everything seems to work as expected, files are processed at the same time, 308 million DLL files are analyzed in 10 seconds. That's exactly what I want, but I may have abused the TPL. The full project can be found at this address, but the part of the code that I want to check comes from src / Astron.Unpacker / Managers / D2PManager.cs :

public class D2PManager: BaseFileManager
{
private read-only ILogger _logger;
private read-only string _dlmFilesFolder;
public D2PManager (IContainer container): base (container)
{
_logger = ServiceLocator.Logger;
_dlmFilesFolder = container.GetInstance() .DlmOutputFolder;
}

public async Task UnpackAll (string[] file spath)
{
_logger.Log(LogLevel.Info, $ "attempt to unpack {filesPath.Length} d2p files ...");

var tasks = new list(FilesPath.Length);
tasks.AddRange (filesPath.Select (d2PFilePath => UnpackD2PFile (d2PFilePath)));

wait Task.WhenAll (tasks) .ConfigureAwait (false);
}

public async Task UnpackD2PFile (string path)
{
var d2PFile = new FileAccessor (path);
var metaUnpacker = new D2PFileMetadataUnpacker (_binaryFactory, _serDes);
metaUnpacker.Unpack (d2PFile);

var archiveUnpacker = new DlmArchivesUnpacker (_binaryFactory, _serDes, metaUnpacker.Value);
archiveUnpacker.Unpack (d2PFile);

var progressCount = 1;
var progressBar = new ProgressBar (PbStyle.SingleLine, archiveUnpacker.Values.Count);
progressBar.Refresh (0, Path.GetFileName (d2PFile.FullPath));
wait for Task.Delay (10); // does not print like this
foreach (var archive in archiveUnpacker.Values)
{
var filePath = (_dlmFilesFolder + archive.RelativePath) .Replace (& # 39; / & # 39 ;, & # 39; \ & # 39;);
var fileDirectory = Path.GetDirectoryName (filePath);
using var decompressedData = new MemoryStream ();
with var deflatedStream = new DeflateStream (new MemoryStream (archive.CompressedData),
CompressionMode.Decompress);

wait for deflatedStream.CopyToAsync (decompressedData);
if (! Directory.Exists (fileDirectory)) Directory.CreateDirectory (fileDirectory);

File.WriteAllBytes (filePath, decompressedData.GetBuffer ());
progressBar.Refresh (progressCount, filePath);
progressCount ++;
}
}
}

Here are my questions:

  • If I do not add that Task.Delay () Immediately after initializing the process bar, the files appear to be processed synchronously (the progress bar is displayed when the last operation is completed). Why is this happening?
  • Is it correct to use .ConfigureAwait (false) on Task.WhenAll () ?
  • Do I start all tasks correctly? Should not use Task.Run () instead with Task.WaitAll () ?

7 – "Line style output" views – tpl / theme file causes the page navigation to be paused or hidden

I've used the following in a view so I can swap the order of the fields / lines for every other line:

Content: The output of the field.
* - $ field-> raw: The raw data for the field, if any. This is NOT reliable.
* - $ field-> class: The secure class ID to use.
* - $ field-> handler: The object handler object Views, which controls this field. Do not use
* var_export to back up this object because the recursion can not be processed.
* - $ field-> inline: Whether the field should be inline or not.
* - $ field-> inline_html: either div or span based on the above flag.
* - $ field-> wrapper_prefix: A complete wrapper containing the inline_html to use.
* - $ field-> wrapper_suffix: The closing tag for the wrapper.
* - $ field-> separator: an optional delimiter that can precede a field.
* - $ field-> label: The wrap-label text to use.
* - $ field-> label_html: The full HTML code of the label to use, including
* configured element type
* - $ row: The raw result object of the query with all retrieved data.
*
* @ ingroup views_templates
* /
?>
row_index% 2 == 0):

//Image
$ Print fields['field_image']-> wrapper_prefix;
$ Print fields['field_image']-> content;
$ Print fields['field_image']-> wrapper_suffix;

//Body
$ Print fields['body']-> wrapper_prefix;
$ Print fields['body']-> content;
$ Print fields['body']-> wrapper_suffix;

// cell phone, mobile phone
$ Print fields['nothing']-> wrapper_prefix;
$ Print fields['nothing']-> content;
$ Print fields['nothing']-> wrapper_suffix;


otherwise:

//Body
$ Print fields['body']-> wrapper_prefix;
$ Print fields['body']-> content;
$ Print fields['body']-> wrapper_suffix;

//Image
$ Print fields['field_image']-> wrapper_prefix;
$ Print fields['field_image']-> content;
$ Print fields['field_image']-> wrapper_suffix;

// cell phone, mobile phone
$ Print fields['nothing']-> wrapper_prefix;
$ Print fields['nothing']-> content;
$ Print fields['nothing']-> wrapper_suffix;

endif; ?>

Filename: views-view-fields – founders.tpl.php

But now, when I click on the "Review template files" button in the view, and sometimes just by chance, my main navigation disappears and the "View, Edit" tabs are converted to a bulleted list. Is this the right way to achieve this desired end result? Is there a gross omission in my tpl file code?

Enter the image description here

Many Thanks!