Skip to content

Thread Library

Thread


Description

Exposes function that create and control threads, parallel tasks and asynchronous functions

Methods

RunAsync(Func[int, void])

Description

Queues the specified work to run on the thread pool and executes it asynchronously

Return Type : void

Static : Yes

Namespace : ThreadLib.Thread

Parameters

Name Data Type Description
Body Func A void Func containing the code to run asynchronously

Syntax

ThreadLib.Thread.RunAsync(x => {
    var users = Domain.ApplicationUser.GetAll();

    foreach Domain.ApplicationUser u in users {
        u.AccessFailedCount = 0;
        u.Save();
    }
});



ParallelTask


Description

Represents Tasks (functionality) that will run in a parallel and asynchronus mode, utilizing all cores in a multi-core CPU server

Generic Parameters

  • T

Methods

ForEach(Collection[T], Func[T, int, void])

Description

Partitions a collection and schedules work to run on multiple threads

Return Type : void

Static : No

Namespace : ThreadLib.ParallelTask

Parameters

Name Data Type Description
Items Collection A collection of instances
Body Func A void Func containing the code that handles each collection instance

Syntax

ThreadLib.ParallelTask[Domain.ApplicationUser] task;

var users = Domain.ApplicationUser.GetAll();

task.ForEach(users, (user, x) => {
    user.AccessFailedCount = 0;
    user.Save();
});



ConcurrentDictionary


Description

Represents a thread-safe Dictionary that can be operated by multiple threads in a concurrent, parallel fashion

Generic Parameters

  • K
  • S

Methods

HasKey(K)

Description

Determines whether the Concurrent Dictionary [K, S] contains the specified key

Return Type : bool

Static : No

Namespace : ThreadLib.ConcurrentDictionary

Parameters

Name Data Type Description
Key K The key, of type K, to locate in the Concurrent Dictionary [K, S]

Syntax

ThreadLib.ConcurrentDictionary[long, string] threadSafeDictionary;
threadSafeDictionary.Add(1, "One");
threadSafeDictionary.Add(2, "Two");
threadSafeDictionary.Add(3, "Three");
if(threadSafeDictionary.HasKey(1) == false){
    DebugLib.Logger.WriteErrorLine("Dictionary doesn't contain an entry with key 1");
}



Get(K)

Description

Retrieves the value given to the specified key, from the Concurrent Dictionary [K, S]

Return Type : S

Static : No

Namespace : ThreadLib.ConcurrentDictionary

Parameters

Name Data Type Description
Key K The key, of type K, of which the value is to be retrieved from the Concurrent Dictionary [K, S]

Syntax

ThreadLib.ConcurrentDictionary[long, string] threadSafeDictionary;
threadSafeDictionary.Add(1, "One");
threadSafeDictionary.Add(2, "Two");
threadSafeDictionary.Add(3, "Three");
if(threadSafeDictionary.HasKey(1) == true){
    string value = threadSafeDictionary.Get(1);
    DebugLib.Logger.WriteInfoLine("Value of entry with Key 1 = " + value);
}



Set(K, S)

Description

Sets a specific value, of type S, to the specified Key in the Concurrent Dictionary [K, S]

Return Type : S

Static : No

Namespace : ThreadLib.ConcurrentDictionary

Parameters

Name Data Type Description
Key K The key, of type K, whose value is to be set
Value S The value, of type S, to set to the found key

Syntax

ThreadLib.ConcurrentDictionary[long, string] threadSafeDictionary;
threadSafeDictionary.Add(1, "One");
threadSafeDictionary.Set(1, "Uno");



Add(K, S)

Description

Adds a key-value entry to the Concurrent Dictionary [K, S]

Return Type : void

Static : No

Namespace : ThreadLib.ConcurrentDictionary

Parameters

Name Data Type Description
Key K The key, of type K, to be added
Value S The value, of type S, to be assigned to the added key

Syntax

ThreadLib.ConcurrentDictionary[long, string] threadSafeDictionary;
threadSafeDictionary.Add(1, "One");



OrderByValue()

Description

Sorts the elements of the Concurrent Dictionary [K, S] in ascending order according to their values

Return Type : Dictionary

Static : No

Namespace : ThreadLib.ConcurrentDictionary

Syntax

ThreadLib.ConcurrentDictionary[long, string] threadSafeDictionary;
threadSafeDictionary.Add(1, "One");
threadSafeDictionary.Add(2, "Two");
threadSafeDictionary.Add(3, "Three");
threadSafeDictionary.OrderByValue();
/*
Will be ordered like so: 
threadSafeDictionary.Add(1, "One");
threadSafeDictionary.Add(3, "Three");
threadSafeDictionary.Add(2, "Two");
*/



OrderByKey()

Description

Sorts the elements of the Concurrent Dictionary [K, S] in ascending order according to their keys

Return Type : Dictionary

Static : No

Namespace : ThreadLib.ConcurrentDictionary

Syntax

ThreadLib.ConcurrentDictionary[long, string] threadSafeDictionary;
threadSafeDictionary.Add(3, "Three");
threadSafeDictionary.Add(1, "One");
threadSafeDictionary.Add(2, "Two");
threadSafeDictionary.OrderByValue();
/*
Will be ordered like so: 
threadSafeDictionary.Add(1, "One");
threadSafeDictionary.Add(2, "Two");
threadSafeDictionary.Add(3, "Three");
*/



OrderByValueDescending()

Description

Sorts the elements of the Concurrent Dictionary [K, S] in descending order according to their values

Return Type : Dictionary

Static : No

Namespace : ThreadLib.ConcurrentDictionary

Syntax

ThreadLib.ConcurrentDictionary[long, string] threadSafeDictionary;
threadSafeDictionary.Add(3, "Three");
threadSafeDictionary.Add(1, "One");
threadSafeDictionary.Add(2, "Two");
threadSafeDictionary.OrderByValue();
/*
Will be ordered like so: 
threadSafeDictionary.Add(2, "Two");
threadSafeDictionary.Add(3, "Three");
threadSafeDictionary.Add(1, "One");
*/



OrderByKeyDescending()

Description

Sorts the elements of the Concurrent Dictionary [K, S] in descending order according to their keys

Return Type : Dictionary

Static : No

Namespace : ThreadLib.ConcurrentDictionary

Syntax

ThreadLib.ConcurrentDictionary[long, string] threadSafeDictionary;
threadSafeDictionary.Add(3, "Three");
threadSafeDictionary.Add(1, "One");
threadSafeDictionary.Add(2, "Two");
threadSafeDictionary.OrderByValue();
/*
Will be ordered like so: 
threadSafeDictionary.Add(3, "Three");
threadSafeDictionary.Add(2, "Two");
threadSafeDictionary.Add(1, "One");
*/



GetKeys()

Description

Gets a collection containing all keys in the Concurrent Dictionary [K, S]

Return Type : Collection

Static : No

Namespace : ThreadLib.ConcurrentDictionary

Syntax

ThreadLib.ConcurrentDictionary[long, string] threadSafeDictionary;
threadSafeDictionary.Add(1, "One");
threadSafeDictionary.Add(2, "Two");
threadSafeDictionary.Add(3, "Three");
Collection[long] keys = threadSafeDictionary.GetKeys(); //[1, 2, 3]



GetValues()

Description

Gets a collection containing all values in the Concurrent Dictionary [K, S]

Return Type : Collection

Static : No

Namespace : ThreadLib.ConcurrentDictionary

Syntax

ThreadLib.ConcurrentDictionary[long, string] threadSafeDictionary;
threadSafeDictionary.Add(1, "One");
threadSafeDictionary.Add(2, "Two");
threadSafeDictionary.Add(3, "Three");
Collection[string] values = threadSafeDictionary.GetValues(); // ["One", "Two", "Three"]



Order(Func[K, S, int])

Description

Sorts the elements of the Concurrent Dictionary [K, S] in ascending order according to specified Lambda

Return Type : Dictionary

Static : No

Namespace : ThreadLib.ConcurrentDictionary

Parameters

Name Data Type Description
LambdaExpression Func A Func containing the custom ordering code applied on each dictionary key and value pair. It must return an int value

Syntax

ThreadLib.ConcurrentDictionary[long, string] threadSafeDictionary;
threadSafeDictionary.Add(2, "Two");                                
threadSafeDictionary.Add(1, "One");                                
threadSafeDictionary.Add(3, "Three");
threadSafeDictionary.Order(x, y => {
    return x;
}); // "One", "Two", "Three"



Clear()

Description

Removes all keys and values from the Concurrent Dictionary [K, S]

Return Type : void

Static : No

Namespace : ThreadLib.ConcurrentDictionary

Syntax

ThreadLib.ConcurrentDictionary[long, string] threadSafeDictionary;
threadSafeDictionary.Add(1, "One");  //Dictionary has one entry
threadSafeDictionary.Clear(); //Dictionary is now empty