|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace LogViewer
{
public static class ProgressBarManager
{
static long m_intFullProgressBarValue = 100;
public static long FullProgressBarValue
{
get { return ProgressBarManager.m_intFullProgressBarValue; }
set { ProgressBarManager.m_intFullProgressBarValue = value; }
}
static int m_intProgressSteps = 100;
static long m_intIntermediateValue = 0;
static FrmProgressBar m_frm = null;
static int m_intPrevValue = 0;
static ManualResetEvent waitForThreadCreation = new ManualResetEvent(false);
public static void CreateInThread()
{
m_frm = new FrmProgressBar();
m_frm.SetLableText(m_labelText);
m_frm.SetTotalProgressSteps(m_intProgressSteps);
Thread t = new Thread((ThreadStart)delegate
{
Application.Run(m_frm);
});
t.SetApartmentState(ApartmentState.STA);
t.IsBackground = true;
t.Start();
while (m_frm.Visible == false)
Thread.Sleep(50);
}
public static void ShowProgressBar(long intFullProgressBarValue)
{
if (m_frm == null)
CreateInThread();
m_intFullProgressBarValue = intFullProgressBarValue;
m_frm.Invoke((ThreadStart)delegate
{
if (!m_frm.Visible)
m_frm.Show();
m_frm.ProgressBarControl.Value = 0;
});
}
static string m_labelText = "Adding Files";
public static void SetLableText(string text)
{
m_labelText = text;
if (m_frm != null)
{
if (m_frm.InvokeRequired)
{
m_frm.Invoke((ThreadStart)delegate
{
m_frm.SetLableText(text);
});
}
else
{
m_frm.SetLableText(text);
}
}
}
public static void SetProgress(long intermediateValue)
{
int newValue = (int)(((double)intermediateValue / (double)m_intFullProgressBarValue) * (double)m_intProgressSteps);
if (newValue > m_intProgressSteps)
newValue = m_intProgressSteps;
m_intIntermediateValue = intermediateValue;
if (newValue > m_intPrevValue && m_frm != null)
{
if (m_frm.InvokeRequired)
{
m_frm.Invoke((ThreadStart)delegate
{
m_frm.ProgressBarControl.Value = newValue;
m_intPrevValue = m_frm.ProgressBarControl.Value;
m_frm.SetLableText(m_labelText);
m_frm.Invalidate();
m_frm.Refresh();
});
}
else
{
m_frm.ProgressBarControl.Value = newValue;
m_frm.SetLableText(m_labelText);
}
m_intPrevValue = newValue;
Application.DoEvents();
}
}
public static void IncrementProgress(long addition)
{
m_intIntermediateValue += addition;
SetProgress(m_intIntermediateValue);
}
public static void ClearProgress()
{
m_intIntermediateValue = 0;
m_intPrevValue = 0;
SetProgress(m_intIntermediateValue);
}
public static void CloseProgress()
{
Thread t = new Thread((ThreadStart)delegate
{
Thread.Sleep(700);
if (m_frm.Visible)
m_frm.Invoke((ThreadStart)delegate
{
m_frm.Hide();
});
ClearProgress();
});
t.Start();
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I've been all over the coding world since earning my degrees
have worked in c++ and java, finally setteling into c# about 6 years ago, where i spent a good amount of my time in Performance tweaking & memory debugging, as well as designing new solutions and hacking at old ones to stay in line.
Computers never cease to amaze me, and i'm glad to have found a field where i get paid to do what i enjoy.
I have been toying around with the idea of publishing stuff online for years, never actually getting around to it, so i still have a lot of stuff to write up, aside from all the other new stuff i'll get excited about, hope you'll like enjoy reading it as much as i enjoy writing.
linkedin
google plus