showing percentage in .net console application - c#

I have a console app that performs a lengthy process.
I am printing out the percent complete on a new line, for every 1% complete.
How can I make the program print out the percent complete in the same location in the console window?

Print \r to get back to the start of the line (but don't print a newline!)
For example:
using System;
using System.Threading;
class Test
{
static void Main()
{
for (int i=0; i <= 100; i++)
{
Console.Write("\r{0}%", i);
Thread.Sleep(50);
}
}
}

You may use:
Console.SetCursorPosition();
To set the position appropriately.
Like so:
Console.Write("Hello : ");
for(int k = 0; k <= 100; k++){
Console.SetCursorPosition(8, 0);
Console.Write("{0}%", k);
System.Threading.Thread.Sleep(50);
}
Console.Read();

Related

How to print thread code in command prompt

what is this called "the thread 0x2ef0" and how can I display it in the command prompt? I tried executing the code and it was only display in the output box or the debugger code, is there a possible way to print this hex decimal thread code? to the command prompt? i was using an windows application with windows property but when i tried in windows application with console property the "the thread 0x2ef0" is not showing off so i want to display it in command prompt.
Probably one of these:
using System;
using System.Diagnostics;
using System.Threading;
namespace Demo
{
internal static class Program
{
private static void Main()
{
Console.WriteLine("0x{0:x4}", Thread.CurrentThread.ManagedThreadId);
Console.WriteLine("0x{0:x4}", Thread.CurrentThread.GetHashCode());
Console.WriteLine("0x{0:x4}", AppDomain.GetCurrentThreadId());
Console.WriteLine("0x{0:x4}", Process.GetCurrentProcess().Threads[0].Id);
}
}
}
Try using the CurrentThread property in order to get the data you are looking for.
Task<Double> t = Task.Run( () => { ShowThreadInformation("Main Task(Task #" + Task.CurrentId.ToString() + ")");
for (int ctr = 1; ctr <= 20; ctr++)
tasks.Add(Task.Factory.StartNew(
() => { ShowThreadInformation("Task #" + Task.CurrentId.ToString());
long s = 0;
for (int n = 0; n <= 999999; n++) {
lock (rndLock) {
s += rnd.Next(1, 1000001);
}
}
return s/1000000.0;
} ));
Task.WaitAll(tasks.ToArray());
Double grandTotal = 0;
Console.WriteLine("Means of each task: ");
foreach (var child in tasks) {
Console.WriteLine(" {0}", child.Result);
grandTotal += child.Result;
}
Console.WriteLine();
return grandTotal / 20;
} );
Console.WriteLine("Mean of Means: {0}", t.Result);

Why my C# Code isn't Printing the Required Output

I attempting to create a program that uses multiple methods that would print out base numbers, exponents, and their resulting solutions. I am trying to get it to run and it's nearly completed, but I am encountering a couple issues. The code itself seems to run, but doesn't appear to print out on Visual Studio. I did run it on an online compiler and got this as an output:
It seems I am missing something in my code, but I am unclear as to what I may be missing. This is the code I have created for the project:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Project
{
class Program
{
static void Main(string[] args)
{
//Our initialized variables.
int intMinBase = 1;
int intMaxBase = 100;
int intMinExpo = 1;
int intMaxExpo = 10;
//Our arrays for the project, all at a length of 5.
long[] baseNumbers = new long[5];
long[] exponents = new long[5];
long[] results = new long[5];
//Randomize the baseNumbers and exponents!
Random randInt = new Random();
for (long i = 0; i < 5; i++)
{
baseNumbers[i] = randInt.Next(intMinBase, intMaxBase);
exponents[i] = randInt.Next(intMinExpo, intMaxExpo);
}
PrintArrays(baseNumbers, exponents, results);
}
//This is potentially experimental code for the Power Method.
public static int Power(int baseNum, int exponent)
{
int answer;
if (exponent == 1)
{
answer = 1;
}
else
{
answer = baseNum * Power(baseNum, exponent - 1);
}
return answer;
}
//The new method to be printed. Is this the correct manner to display this?
public static void PrintArrays(long[] baseNum, long[] exponent, long[] result)
{
Console.WriteLine($"Base\tExponent\tResult");
for (int print = 0; print < result.GetUpperBound(0); print++)
{
Console.WriteLine(baseNum[print]+"\t"+exponent[print]+"\t"+result[print]);
}
}
}
}
My question is mainly am I missing something and why isn't it appearing to print in Visual Studio yet it's appearing on an online compiler? I suspect the answer to the first part of the question has to do with the methods I used, but I am unsure.
First error: Nowhere is the method Power called and nowhere is the array results filled.
Solution example:
for (long i = 0; i < 5; i++)
{
baseNumbers[i] = randInt.Next(intMinBase, intMaxBase);
exponents[i] = randInt.Next(intMinExpo, intMaxExpo);
results[i] = Power(baseNumbers[i], exponents[i]);
}

How to make an efficient Console Updater

Currently i wanna display the following variable
Total Item
Total Execution
Finish Status
using System;
namespace ConsoleTest
{
class Program
{
static void Main(string[] args)
{
int TotalValue = 250; // Total Item Example
int TotalExecution = 0;
bool Finish_Status = false;
for (int i = 0; i < TotalValue; ++i)
{
//Do Work Here
System.Threading.Thread.Sleep(10); // Example Work
TotalExecution++;
if (TotalValue - TotalExecution == 0)
{
Finish_Status = true;
}
Console.Clear();
Console.Write("Progression Info\n Total Item : {0}\n Execution Total : {1}\n Remaining : {2}\n Finish_Status : {3}", TotalValue,TotalExecution, TotalValue - TotalExecution, Finish_Status); // Display Information To Console
}
Console.ReadLine();
}
}
}
The result is good,however i was wondering if theres a much more efficient way of doing this,preferably updating it without using Console.Clear();
You can use Console.SetCursorPosition to move the cursor around the console buffer for each write, rather than clearing the console each time.
For example:
using System;
namespace ConsoleTest
{
class Program
{
static void Main(string[] args)
{
int TotalValue = 250; // Total Item Example
int TotalExecution = 0;
bool Finish_Status = false;
Console.Write("Progression Info\n Total Item : \n Execution Total : \n Remaining : \n Finish_Status : ");
for (int i = 0; i < TotalValue; ++i)
{
//Do Work Here
System.Threading.Thread.Sleep(10); // Example Work
TotalExecution++;
if (TotalValue - TotalExecution == 0)
{
Finish_Status = true;
}
Console.SetCursorPosition(26, 1);
Console.Write(TotalValue);
Console.SetCursorPosition(31, 2);
Console.Write(TotalExecution);
Console.SetCursorPosition(25, 3);
Console.Write(TotalValue - TotalExecution);
Console.SetCursorPosition(29, 4);
Console.Write(Finish_Status);
}
Console.ReadLine();
}
}
}
Disclaimer: Obviously the above is quick 'n' dirty, and would benefit from substantial refinement, but you get the idea.
Welcome kepanin_lee
I think you are looking for something like this.
//Console.Clear()
Console.Write(vbCr & "Progression Info\n...
Just start with vbCr, by this way you force to start on the beginning of the same line, so you only overwrite the last line, without clear all the screen.

How to display the output window in windows application format with C#?

I'm not being able to display the output window, can anybody tell me what I am missing here? I'm trying to display the # symbol in pyramid form.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace variable
{
class Program
{
static void Main()
{
for (int row = 0; row < 6; row++)
{
// Counting backwards here!
for (int spaces = 6 - row; spaces > 0; spaces--)
{
Console.Write(" ");
}
for (int column = 0; column < (2 * row + 1); column++)
{
Console.Write("#");
}
Console.WriteLine();
}
Console.Read();
}
}
}
I'm assuming you have configured the project as a windows forms project.
So you don't have any console.
You have three options:
Set the project type to Console Application. This is most likely not feasable as it will require too many changes for your application.
Attach a console using P/Invoke. See Show Console in Windows Application? . The advantage over option 3. is that you can display this console in release as well.
Use Trace.Write / Debug.Write to write to Visual Studio's output window.
Remove one } after Console.Read();
Looks like a compile error and its working other than that. You will get the output in the console
using System.IO;
using System;
class Program
{
static void Main()
{
// Read in every line in the file.
for (int row = 0; row < 6; row++)
{
// Counting backwards here!
for (int spaces = 6 - row; spaces > 0; spaces--)
{
Console.Write(" ");
}
for (int column = 0; column < (2 * row + 1); column++)
{
Console.Write("#");
}
Console.WriteLine();
}
Console.Read();
}
}
Output:
#
###
#####
#######
#########
###########

do while loop using an if statement to print odd numbers

At the moment I am learning loops. I am trying to create a console application which uses a do while loop to print all odd integers between 20 and 0.
Why when I uncomment if statement below my code does not print anything and never finishes?
using System;
class Program
{
static void Main(string[] args)
{
int i = 20;
do
{
// if (i%2 !=0)
{
Console.WriteLine(
"When counting down from 20 the odd values are: {0}", i--);
}
} while (i >=0);
}
}
I think the main issue you're having is that the decrement (i--) only occurs inside the if block. That means when the condition fails, you will enter an infite loop. You can move the decrement outside the if block to fix that. Try this:
Console.Write("When counting down from 20 the odd values are: ");
do
{
if (i % 2 != 0)
{
Console.Write(" {0}", i);
}
i--;
} while (i >= 0);
I also moved the first Console.Write outside the loop to reduce redundancy in the output. This will produce:
When counting down from 20 the odd values are: 19 17 15 13 11 9 7 5 3 1
A for loop may be easier to follow:
Console.WriteLine("When counting down from 20 the odd values are: ");
for( int i = 20; i >= 0; i--)
{
if (i%2 !=0)
{
Console.Write(" " + i);
}
}
Sorry, I know this is really old and the initial question was for a DO not a FOR, but I wanted to add what I did to accomplish this outcome. When I Googled the question initially, this is what was returned first despite my query being for a FOR loop. Hopefully someone down the road will find this helpful.
The below will print the odd number index of an array - in this case, Console App args.
using System;
namespace OddCounterTest
{
class Program
{
static void Main(string[] args)
{
for (int i = 0; i < args.Length; i++)
{
Console.WriteLine(i);
i++;
}
}
}
}
Output with 6 arguments will be:
1
3
5
Moving i++ to the first step in the for loop will get you the even numbers.
using System;
namespace EvenCounterTest
{
class Program
{
static void Main(string[] args)
{
for (int i = 0; i < args.Length; i++)
{
i++;
Console.WriteLine(i);
}
}
}
}
Output will be:
2
3
4
This is setup so you can get the also actual values of the args too rather than simply the count and print of the args index. Just create a string and set the string to args[i]:
string s = args[i];
Console.WriteLine(s);
If you need to count and exclude "0" in the event your are printing numbers like the question initially asks, then setup your for loop like so:
for (int i = 1; i <= args.Length; i++);
Notice how "i" is initially set to 1 in this example and i is less than or equal to the array length rather than simply less than. Be mindful of your less than and less than/equal to's or you will get OutOfRangeExceptions.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Do_while_Test
{
class Program
{
static void Main(string[] args)
{
int i = 20;
Console.WriteLine();
Console.WriteLine("A do while loop printing odd values from 20 - 0 ");
do
{
if (i-- %2 !=0)
{
Console.WriteLine("When counting down from 20 the odd values are: {0}", i--);
}
} while (i >=0);
Console.ReadLine();
}
}
}

Categories