csharp控制台进度条。代码属于麻省理工学院许可证:http://opensource.org/licenses/mit(代码片段)

author author     2023-01-04     660

关键词:

using System;
using System.Text;
using System.Threading;

/// <summary>
/// An ASCII progress bar
/// </summary>
public class ProgressBar : IDisposable, IProgress<double> 
	private const int blockCount = 10;
	private readonly TimeSpan animationInterval = TimeSpan.FromSeconds(1.0 / 8);
	private const string animation = @"|/-\";

	private readonly Timer timer;

	private double currentProgress = 0;
	private string currentText = string.Empty;
	private bool disposed = false;
	private int animationIndex = 0;

	public ProgressBar() 
		timer = new Timer(TimerHandler);

		// A progress bar is only for temporary display in a console window.
		// If the console output is redirected to a file, draw nothing.
		// Otherwise, we'll end up with a lot of garbage in the target file.
		if (!Console.IsOutputRedirected) 
			ResetTimer();
		
	

	public void Report(double value) 
		// Make sure value is in [0..1] range
		value = Math.Max(0, Math.Min(1, value));
		Interlocked.Exchange(ref currentProgress, value);
	

	private void TimerHandler(object state) 
		lock (timer) 
			if (disposed) return;

			int progressBlockCount = (int) (currentProgress * blockCount);
			int percent = (int) (currentProgress * 100);
			string text = string.Format("[01] 2,3% 3",
				new string('#', progressBlockCount), new string('-', blockCount - progressBlockCount),
				percent,
				animation[animationIndex++ % animation.Length]);
			UpdateText(text);

			ResetTimer();
		
	

	private void UpdateText(string text) 
		// Get length of common portion
		int commonPrefixLength = 0;
		int commonLength = Math.Min(currentText.Length, text.Length);
		while (commonPrefixLength < commonLength && text[commonPrefixLength] == currentText[commonPrefixLength]) 
			commonPrefixLength++;
		

		// Backtrack to the first differing character
		StringBuilder outputBuilder = new StringBuilder();
		outputBuilder.Append('\b', currentText.Length - commonPrefixLength);

		// Output new suffix
		outputBuilder.Append(text.Substring(commonPrefixLength));

		// If the new text is shorter than the old one: delete overlapping characters
		int overlapCount = currentText.Length - text.Length;
		if (overlapCount > 0) 
			outputBuilder.Append(' ', overlapCount);
			outputBuilder.Append('\b', overlapCount);
		

		Console.Write(outputBuilder);
		currentText = text;
	

	private void ResetTimer() 
		timer.Change(animationInterval, TimeSpan.FromMilliseconds(-1));
	

	public void Dispose() 
		lock (timer) 
			disposed = true;
			UpdateText(string.Empty);
		
	


using System;
using System.Threading;

static class Program 

	static void Main() 
		Console.Write("Performing some task... ");
		using (var progress = new ProgressBar()) 
			for (int i = 0; i <= 100; i++) 
				progress.Report((double) i / 100);
				Thread.Sleep(20);
			
		
		Console.WriteLine("Done.");
	


csharp#004#进度条与timer(代码片段)

  C#实现进度条异常简单,因为所有东西都已经封装好了。只需要简单的拖拽:写两行代码就完工了:privatevoidtimer1_Tick(objectsender,EventArgse)progressBar1.Value+=1;if(progressBar1.Value==100)progressBar1.Value=0;  查看详情

text麻省理工学院许可证(代码片段)

查看详情

c#控制台console进度条(代码片段)

1说明笔者大多数的开发在Linux下,多处用到进度条的场景,但又无需用到图形化界面,所以就想着弄个console下的进度条显示。2步骤清行显示//清行处理操作intcurrentLineCursor=Console.CursorTop;//记录当前光标位置Console.SetCursorPosition(0,Co... 查看详情

python实用工具用python在控制台输出进度条(代码片段)

 进度条在一些计时任务中会经常使用,这里提供两种在控制台输出进度条显示的简单源代码:#进度条1importtimeforiinrange(1,101):print('\\r'+'▋'*i+'当前进度:%'.format(i),end='')time.sleep(0. 查看详情

java控制台示例中的java进度条(代码片段)

查看详情

python文本进度条(代码片段)

文本进度条:编程通过格式化字符串输出和时间延迟实现控制台风格文本进度条绘制效果如上图importtimescale=10print("执行开始".center(scale,'-'))foriinrange(scale+1):a='*'*ib="."*(scale-i)c=(i/sc 查看详情

Laravel 4.2 Artisan 控制台进度条

】Laravel4.2Artisan控制台进度条【英文标题】:Laravel4.2ArtisanConsoleProgressBar【发布时间】:2016-09-2414:58:24【问题描述】:我想问一下是否有人设法在artisan控制台中显示了symfony进度条。我使用的是Windows7和Laravel4.2框架。到目前为止,... 查看详情

学习进度条

 第四周所花时间(小时)8代码量(行)400博客量(篇)2所学到的知识学习《软件需求模式》的第五、六章,学习编写C#控制台程序,对基本的输入输出还有变量、数组的定义有了一定的了解,学习安装Oracle数据库。  查看详情

js控制进度条到达100%跳转界面一

进度条一般在手机上用到的比较广泛,刚好最近的项目也是一直在做手机站,这个特效是手机端的一个界面,现在我把改成pc端了,进度条的快慢速度和样式可自行调节,改动也是很方便的,不多说,看代码:<!DOCTYPEhtml><h... 查看详情

文件上传进度条干扰控制器重定向

】文件上传进度条干扰控制器重定向【英文标题】:FileuploadprogressbarinterfereswithControllerredirect【发布时间】:2021-11-1610:23:43【问题描述】:我有一个文件提交表单和一个跟踪上传进度的进度条。但是,控制进度条的javascript会干扰... 查看详情

如何在 CSharp 中制作自己的许可证密钥

】如何在CSharp中制作自己的许可证密钥【英文标题】:HowmakeOwnlicense-keyinCSharp【发布时间】:2019-05-0112:20:52【问题描述】:我在复制我的程序exe时遇到了一些问题,所以我想为每台PC制作一个代码并注册我的程序!如何在C#中制作... 查看详情

控制台进度条

...理资料的时候,翻出多年前在网上看到的一篇帖子,一个控制台的进度条,非常酷炫,原文出处-传送门。   记得在刚开始接触编程的时候,用控制台写些小工具玩,也喜欢将信息打印到屏幕上,看着不断闪动的屏幕... 查看详情

进度条的美化(代码片段)

这里记录一种用css控制进度条美化的效果css代码:@-webkit-keyframesprogress-bar-stripesfrombackground-position:40px0tobackground-position:00@-moz-keyframesprogress-bar-stripesfrombackground-position:40px0tobackgro 查看详情

控制进度条的粗细

】控制进度条的粗细【英文标题】:ControlThicknessofProgressBar【发布时间】:2015-03-1708:37:17【问题描述】:我有一个进度条,如下所示:我的问题很简单,我将如何控制圆形视图的“厚度”,因为在我看来它似乎太厚了。非常感谢... 查看详情

开源软件许可证严格性比较

...T>BSD>Apache>LGPL>GPL>AGPL1、MIT:MIT许可证之名源自麻省理工学院(MassachusettsInstituteofTechnology,MIT)只有一点限制:所有应用必须包含版权声明和许可声明(自己修改后的声明)2、BSD可以自由的使用,修改源代码,也可以将修... 查看详情

vc6.0里mfc进度条如何使用

参考技术A演练CProgress7.1进度条的主要功能进度条控制(ProgressControl)主要用来进行数据读写、文件拷贝和磁盘格式等操作时的工作进度提示情况,如安装程序等,伴随工作进度的进展,进度条的矩形区域从左到右利用当前活动... 查看详情

ios进度条显示

一、实现下载文件进度控制1.代码示例1#import"YYViewController.h"23@interfaceYYViewController()4@property(nonatomic,strong)NSMutableData*fileData;5@property(nonatomic,strong)NSFileHandle*writeHandle;6@property(nonatomic 查看详情

csharp方法和参数第4条(代码片段)

查看详情