Thread-based Parallelism in C#

karthikeya Boyini
Updated on 22-Jun-2020 10:15:20

1K+ Views

In C#, Task parallelism divide tasks. The tasks are then allocated to separate threads for processing. In .NET, you have the following mechanisms to run code in parallel: Thread, ThreadPool, and Task. For parallelism, use tasks in C# instead of Threads.A task will not create its own OS thread, whereas they are executed by a TaskScheduler.Let us see how to create tasks. Use a delegate to start a task −Task tsk = new Task(delegate { PrintMessage(); }); tsk.Start();Use Task Factory to start a task −Task.Factory.StartNew(() => {Console.WriteLine("Welcome!"); });You can also use Lambda −Task tsk = new Task( () => PrintMessage() ... Read More

Find Frequency of Each Word in a String in C#

Samual Sam
Updated on 22-Jun-2020 10:14:23

814 Views

To find the frequency of each word in a string, you can try to run the following code −Example Live Demousing System; class Demo {    static int maxCHARS = 256;    static void calculate(String s, int[] cal) {       for (int i = 0; i < s.Length; i++)       cal[s[i]]++;    }    public static void Main() {       String s = "Football!";       int []cal = new int[maxCHARS];       calculate(s, cal);       for (int i = 0; i < maxCHARS; i++)       if(cal[i] > ... Read More

Sum of Two Binary Numbers Using C#

karthikeya Boyini
Updated on 22-Jun-2020 10:13:48

2K+ Views

To find the sum of two binary numbers, firstly set them.val1 = 11110; val2 = 11100;Now call the displaySum() method, which created to display the sumL.sum = displaySum(val1, val2);We have set a new array in the method to display each bit of the binary number.long[] sum = new long[30];Now let us see the complete code to calculate the sum of binary numbers as shown in the code below −Example Live Demousing System; class Demo {    public static void Main(string[] args) {       long val1, val2, sum = 0;       val1 = 11110;       val2 ... Read More

Get nth Value of Fibonacci Series Using Recursion in C#

Chandu yadav
Updated on 22-Jun-2020 10:13:04

1K+ Views

Create a method to get the nth value with recursion.public int displayFibonacci(int n)Call the method −displayFibonacci(val)On calling, the displayFibonacci() meyhod gets called and calculate the nth value using recursion.public int displayFibonacci(int n) {    if (n == 0) {       return 0;    }    if (n == 1) {       return 1;    } else {       return displayFibonacci(n - 1) + displayFibonacci(n - 2);    } }Let us see the complete code −Example Live Demousing System; public class Demo {    public static void Main(string[] args) {       Demo d = ... Read More

Find Current Context ID of the Thread in C#

Samual Sam
Updated on 22-Jun-2020 10:11:35

745 Views

To create a new thread.Thread thread = Thread.CurrentThread; thread.Name = "My new Thread”;To get the current context id, use the ContextID property.Thread.CurrentContext.ContextIDLet us see the complete code −Example Live Demousing System; using System.Threading; namespace Demo {    class MyClass {       static void Main(string[] args) {          Thread thread = Thread.CurrentThread;          thread.Name = "My new Thread";          Console.WriteLine("Thread Name = {0}", thread.Name);          Console.WriteLine("current Context id: {0}", Thread.CurrentContext.ContextID);          Console.ReadKey();       }    } }OutputThread Name = My new Thread current Context id: 0

What is Virtualization

David Meador
Updated on 22-Jun-2020 10:11:02

4K+ Views

Virtualization in operating system changes a normal operating system so that it can run different types of applications that may be handled on a single computer system by many users. The operating system may appear different to each user and each of them may believe they are interacting with the only operating system i.e. this does not interfere with user experience.Operating system virtualization can also be used to migrate a process from one instance of the operating system to another. However, all the processes in the system are isolated and there operations are strictly monitored so there are no discrepancies ... Read More

Find Power of a Number Using Recursion in C#

George John
Updated on 22-Jun-2020 10:09:37

271 Views

To find the power of a number, firstly set the number and the power −int n = 15; int p = 2;Now create a method and pass these values −static long power(int n, int p) {    if (p != 0) {       return (n * power(n, p - 1));    }    return 1; } Above, the recursive call gave us the results −n * power(n, p - 1)The following is the complete code to get the power of a number −Example Live Demousing System; using System.IO; public class Demo {    public static void Main(string[] args) { ... Read More

Calculate the Execution Time of a Method in C#

Samual Sam
Updated on 22-Jun-2020 10:09:06

828 Views

Use Stopwatch class to measure time of execution of a method in .NET −Stopwatch s = Stopwatch.StartNew();Now set a function and use the ElapsedMilliseconds property to get the execution time in milliseconds −s.ElapsedMillisecondsLet us see the complete code −Example Live Demousing System; using System.IO; using System.Diagnostics; public class Demo {    public static void Main(string[] args) {       Stopwatch s = Stopwatch.StartNew();       display();       for (int index = 0; index < 5; index++) {          Console.WriteLine("Time taken: " + s.ElapsedMilliseconds + "ms");       }       ... Read More

Find Free Disk Space Using Chash

Ankith Reddy
Updated on 22-Jun-2020 10:08:39

1K+ Views

Firstly, create an instance of DriveInfo −DriveInfo dInfo = new DriveInfo("E");Display free space −Console.WriteLine("Disk Free space = {0}", dInfo.AvailableFreeSpace); Now, use AvailableFreeSpace property and get the percentage of free space −Double pc = (dInfo.AvailableFreeSpace / (float)dInfo.TotalSize) * 100;Here, you will get the percentage of free size in comparison with the total disk space −Console.WriteLine(" Free space (percentage) = {0:0.00}%.", pc);

Check if a String is a Valid Keyword in C#

karthikeya Boyini
Updated on 22-Jun-2020 10:08:25

2K+ Views

To check if a string is a valid keyword, use the IsValidIdentifier method.The IsValidIdentifier method checks whether the entered value is an identifier or not. If it’s not an identifier, then it’s a keyword in C#.Let us see an example, wherein we have set the CodeDomProvider and worked with the IsValiddentifier method −CodeDomProvider provider = CodeDomProvider.CreateProvider("C#");Let us see the complete codeLExample Live Demousing System; using System.IO; using System.CodeDom.Compiler; namespace Program {    class Demo {       static void Main(string[] args) {              string str1 = "amit";          string str2 = ... Read More

Advertisements