Skip to main content

HOW TO APPLY FALLING TEXT EFFECT IN A CONSOLE PROGRAM WITH THREADING IN C#

"A thread is defined as the execution part of a program. Each thread defines a unique flow of control. If your application involves complicated and time consuming operations then it is often helpful to set different execution paths or threads, with each thread performing a particular job.", says the smart guys behind Tutorials Point.Well, our program is not going to be complicated and we are not going to be performing any time consuming task, we are rather going be creating a camouflage application that gives us the effect similar to what you get when you ping a site or an IP address with the "-t" option in cmd or the effect you see in movies like Blacklist, 24, Person Of Interest and the likes.Now let's stop the ranting and get started.We are going be building this application upon our existing code, one we started in the previous article "How to grammatically Change the background and foreground color of a console Application".Now open up the code in Visual Studio and add a class named ThreadMantis to the project then modify the class to look like the following. class ThreadMantis    {         private int sleep;        private double index;         public ThreadMantis(int sleep, double index)        {            this.sleep = sleep;            this.index = index;        }         public void TheEffect()        {            try            {                 Console.WriteLine("Tricks starts");                for (int counter = 0; counter <= index; counter++)                {                    Random rand = new Random();                    int a=rand.Next(0,2);                    int b=rand.Next(0,2);                    int c = rand.Next(0, 2);                    int d = rand.Next(0, 2);                    int e=rand.Next(0,2);                    int f=rand.Next(0,2);                    int g = rand.Next(0, 2);                    int h = rand.Next(0, 2);                    int i = rand.Next(0, 2);                    int j = rand.Next(0, 2);                    int k = rand.Next(0, 2);                    int l = rand.Next(0, 2);                    int m = rand.Next(0, 2);                    int n = rand.Next(0, 2);                    int o = rand.Next(0, 2);                    int p = rand.Next(0, 2);                    int q = rand.Next(0, 2);                    int r = rand.Next(0, 2);                    int s = rand.Next(0, 2);                    int t = rand.Next(0, 2);                    int u = rand.Next(0, 2);                    int v = rand.Next(0, 2);                    int w = rand.Next(0, 2);                    int x = rand.Next(0, 2);                    int y=rand.Next(0,2);                    int z = rand.Next(0, 2);                     Thread.Sleep(sleep);                    Console.WriteLine(a.ToString()+b.ToString()+c.ToString()+d.ToString()+e.ToString()+f.ToString()+g.ToString()+                        i.ToString()+j.ToString()+l.ToString()+m.ToString()+n.ToString()+o.ToString()+p.ToString()+q.ToString()+r.ToString()                        +s.ToString()+t.ToString()+u.ToString()+v.ToString()+w.ToString()+x.ToString()+y.ToString()+z.ToString()+                         i.ToString()+j.ToString()+l.ToString()+m.ToString()+n.ToString()+o.ToString()+p.ToString()+q.ToString()+r.ToString()                        +s.ToString()+t.ToString()+u.ToString()+v.ToString()+w.ToString()+x.ToString()+y.ToString()+z.ToString()+                        i.ToString()+j.ToString()+l.ToString()+m.ToString()+n.ToString()+o.ToString()+p.ToString()+q.ToString()+r.ToString()                        +s.ToString()+t.ToString()+u.ToString()+v.ToString()+w.ToString()+x.ToString()+y.ToString()+z.ToString()+                         i.ToString()+j.ToString()+l.ToString()+m.ToString()+n.ToString()+o.ToString()+p.ToString()+q.ToString()+r.ToString()                        +s.ToString()+t.ToString()+u.ToString()+v.ToString()+w.ToString()+x.ToString()+y.ToString()+z.ToString());                 }            }            catch (ThreadAbortException e)            {                Console.WriteLine("Aborted");            }            finally            {                Console.WriteLine("Couldn't catch the thread exception");            }        }    }Such as amateur code.Now that the ThreadMantis class is created, let's now use it in our program. Go to Program class and modify the existing code to look like the code below:    class Program    {        public static int sleep;        public static double index;         public static ThreadMantis thMantis;        static void Main(string[] args)        {            Console.WriteLine("Enter the speed\n>>");             sleep = Convert.ToInt32(Console.ReadLine());            Console.WriteLine("Enter the the number of times you want this to be executed\n>>");            index = Convert.ToInt32(Console.ReadLine());                thMantis= new ThreadMantis(sleep, index);             Console.WindowWidth = 50;            Console.WindowHeight = 50;            Console.ForegroundColor = ConsoleColor.DarkBlue;            Console.BackgroundColor = ConsoleColor.Black;             ThreadStart ts = new ThreadStart(thMantis.TheEffect);            Console.WriteLine("In Main: Create the child thread");            Thread t = new Thread(ts);            t.Start();            Console.WriteLine("I am green");            Console.ReadKey();                    }    }Now that's it, you can run the program now and see how it works. I am not going to explain the code because it seems simple, if there is any part of it you don't understand, please feel free to ask me in the comment section.I added some functionality that allows the user of our program to specify how fast or slow the effect should run and how many times the effect should be executed.In the screenshots below, I entered 10 as the speed and 1000,000 as the number of time the effect should repeat.And hitting the enter key just changes the color and starts running our effect.isn't that impressive?Thanks for following up in this walk-through, to get the whole idea of what just happened here, you might want to read more about Multi-Threading in C#.The program can be stopped by pressing Ctrl+C. You can add more functionality such as allowing the user of our program to change the background and foreground color of the program by entering the color they want (Use the switch case statement to achieve this.).Thank you once again for following up.

Comments

Popular posts from this blog

HOW TO WRITE, COMPILE AND RUN C++ CODE ON LINUX KALI

This article is for hackers who want to develop a c++ program on Kali Linux without having to install any additional software. While some would tell you that you need to install an additional software or a compiler in order to develop a simple program in c++ on Kali Linux, I am going to show you how to develop a c++ program on this distro right out of box. Your Linux Kali comes pre-installed with a c++ compiler called g++ so we are going to write a C++ hello word code and compile it with this compiler. Before we get started, first open up terminal and run to verify if this compiler is installed on you machine: g++ -v if the compiler is pre-installed, you should get the version information of the compiler, otherwise, you should get an error. Now let's jump right in. In your terminal window, type in: nano MyCpp.cpp to create a c++ file and lunch it in nano editor for editing. When nano opens, type in the following c++ code and press Ctrl+x then y and then R

LOAN MANAGEMENT SYSTEM (My Project Idea)

There are many companies that specializes on giving loan to people; they make their profits by collecting interest on any money they lend out, and before a loan can be given to any customer, the customer must provide a collateral and some details such as:  contact details and a reference/guarantor. When the loan is finally given to the customer, they charge interest based on the the amount given to the customer for a given period of time which could be daily, weekly, monthly or yearly.  For example, a company could be collecting interest based on a particular amount for a given period of time like: $200,000 loan would have an interest tag of $2,000  per month until the money is returned, and $100,000 would have an interest of $1,000 per month until the money is returned.  In  addition, the customers need to know when the time for them to pay their interest comes e.g.  at the end of the month or the timing model with which the calculation is being made. Usually, these companies sends ou

How to execute eject command on an ATM Machine using CoreXfs lib on .NET

 In this tutorial, I'm going to demonstrate how to run an eject command on an ATM using CoreXfs, a library I developed.  You can download the library from  here  and follow up with tutorial. First we need to add reference to the downloaded dll. Screen One Once the reference to the dll has been added, you can proceed to coding the application. What I did in the screen below is very simple, I opened the device on the atm using it's logical name so that I can pass in commands. Screen Two So, that's it. thanks for dropping by. You can watch the video below to see how I coded it. Note that this code can only run on an ATM machine. NCR, WINCOR, DIEBOLD, HYOSUNG