Fix threading bugs
This commit is contained in:
parent
589ffbaffc
commit
47183e055c
|
@ -4,15 +4,15 @@ using System.Threading;
|
||||||
|
|
||||||
namespace ProgressBarLib {
|
namespace ProgressBarLib {
|
||||||
public class ProgressBar {
|
public class ProgressBar {
|
||||||
private volatile bool _shouldStop;
|
private volatile bool _shouldStop;
|
||||||
private volatile List<string> _msgList = new List<string>();
|
private volatile List<string> _msgList = new List<string>();
|
||||||
private volatile List<string> _errList = new List<string>();
|
private volatile List<string> _errList = new List<string>();
|
||||||
private volatile int _lastCurr;
|
private volatile int _lastCurr;
|
||||||
private volatile int _lastTotal;
|
private volatile int _lastTotal;
|
||||||
private volatile string _lastMsg = "";
|
private volatile string _lastMsg = "";
|
||||||
private volatile string _mainMsg = "";
|
private volatile string _mainMsg = "";
|
||||||
private readonly object _lock = new object();
|
private readonly object _lock = new object();
|
||||||
private volatile bool _hasStopped;
|
private volatile bool _hasStopped;
|
||||||
|
|
||||||
public ProgressBar() {
|
public ProgressBar() {
|
||||||
new Thread(() => {
|
new Thread(() => {
|
||||||
|
@ -23,8 +23,8 @@ namespace ProgressBarLib {
|
||||||
ClearCurrentConsoleLine();
|
ClearCurrentConsoleLine();
|
||||||
foreach (var msg in _msgList) {
|
foreach (var msg in _msgList) {
|
||||||
Console.WriteLine(msg.Length < Console.WindowWidth
|
Console.WriteLine(msg.Length < Console.WindowWidth
|
||||||
? msg
|
? msg
|
||||||
: msg.Substring(0, Console.WindowWidth - 1));
|
: msg.Substring(0, Console.WindowWidth - 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
_msgList.Clear();
|
_msgList.Clear();
|
||||||
|
@ -58,8 +58,8 @@ namespace ProgressBarLib {
|
||||||
ClearCurrentConsoleLine();
|
ClearCurrentConsoleLine();
|
||||||
foreach (var msg in _msgList) {
|
foreach (var msg in _msgList) {
|
||||||
Console.WriteLine(msg.Length < Console.WindowWidth
|
Console.WriteLine(msg.Length < Console.WindowWidth
|
||||||
? msg
|
? msg
|
||||||
: msg.Substring(0, Console.WindowWidth - 1));
|
: msg.Substring(0, Console.WindowWidth - 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
_msgList.Clear();
|
_msgList.Clear();
|
||||||
|
@ -81,8 +81,8 @@ namespace ProgressBarLib {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void UpdateMainAdv(int curr, int total, string custCurr, string msg) {
|
public void UpdateMainAdv(int curr, int total, string custCurr, string msg) {
|
||||||
var outMsg = $"({custCurr}/{total}) ";
|
var outMsg = $"({custCurr}/{total}) ";
|
||||||
var progPart = MakeProgressBar(curr, total);
|
var progPart = MakeProgressBar(curr, total);
|
||||||
var msgMaxLength = Console.WindowWidth - outMsg.Length - progPart.Length;
|
var msgMaxLength = Console.WindowWidth - outMsg.Length - progPart.Length;
|
||||||
|
|
||||||
if (msgMaxLength < 0) {
|
if (msgMaxLength < 0) {
|
||||||
|
@ -99,8 +99,8 @@ namespace ProgressBarLib {
|
||||||
outMsg += msg;
|
outMsg += msg;
|
||||||
outMsg += progPart;
|
outMsg += progPart;
|
||||||
|
|
||||||
_lastCurr = curr;
|
_lastCurr = curr;
|
||||||
_lastMsg = msg;
|
_lastMsg = msg;
|
||||||
_lastTotal = total;
|
_lastTotal = total;
|
||||||
|
|
||||||
_mainMsg = outMsg;
|
_mainMsg = outMsg;
|
||||||
|
@ -123,19 +123,21 @@ namespace ProgressBarLib {
|
||||||
|
|
||||||
public void Stop() {
|
public void Stop() {
|
||||||
_shouldStop = true;
|
_shouldStop = true;
|
||||||
while (!_hasStopped) ;
|
while (!_hasStopped) { }
|
||||||
}
|
}
|
||||||
|
|
||||||
private static string MakeProgressBar(int curr, int total) {
|
private static string MakeProgressBar(int curr, int total) {
|
||||||
const string fullChar = "=";
|
if (curr > total)
|
||||||
|
curr = total;
|
||||||
|
const string fullChar = "=";
|
||||||
const string blankChar = "-";
|
const string blankChar = "-";
|
||||||
|
|
||||||
var progStr = "[";
|
var progStr = "[";
|
||||||
|
|
||||||
var fullCharCount = (int) (15d / total * curr);
|
var fullCharCount = (int) (15d / total * curr);
|
||||||
var blankCharCount = 15 - fullCharCount;
|
var blankCharCount = 15 - fullCharCount;
|
||||||
|
|
||||||
for (var i = fullCharCount; i > 0; i--) progStr += fullChar;
|
for (var i = fullCharCount; i > 0; i--) progStr += fullChar;
|
||||||
for (var i = blankCharCount; i > 0; i--) progStr += blankChar;
|
for (var i = blankCharCount; i > 0; i--) progStr += blankChar;
|
||||||
|
|
||||||
progStr += "]";
|
progStr += "]";
|
||||||
|
|
Loading…
Reference in a new issue