使增量同时完成

时间:2017-06-13 09:53:44

标签: c# increment

我有一个5个数字的数组,按降序排序:{289,151,69,27,6}:

<ul>
    <li class="menu-item">
        <a class="level2" href="//test.com/products/iphone/iphone-onepage-32gb"><span>iPhone 32GB</span></a>
    </li>
    <li class="menu-item">
      <a class="level2" href="//test.com/products/iphone/iphone-onepage-64gb"><span>iPhone 64GB</span></a>
    </li>
</ul>



$('$('a[href*="onepage"]')').each(function() {
        var text = $(this).text();
        $(this).text(text.replace(new RegExp("onepage","g"), "#");
});

表单上有5个进度条。 每个进度条的最大值= sortedArray [x]

例如:

$('$('a[href *= "onepage"] ')').each(function() {
      var text = $(this).text();
      $(this).text(text.replace(new RegExp("onepage", "g"), "#");
      });

我想要实现的是使所有进度栏增加到各自的MaxValue ,但是让它们同时完成

我开始为数组的每个元素创建一个新线程。但是我无法找到让它们一起完成的算法。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
  <li class="menu-item">
    <a class="level2" href="//test.com/products/iphone/iphone-onepage-32gb"><span>iPhone 32GB</span></a>
  </li>
  <li class="menu-item">
    <a class="level2" href="//test.com/products/iphone/iphone-onepage-64gb"><span>iPhone 64GB</span></a>
  </li>
</ul>

我想过用TimeSpan实现一些东西。例如:

所有进度条都需要在5秒内达到MaxValue ..

我该怎么做?

5 个答案:

答案 0 :(得分:2)

当你试图在一段时间内递增它们时,你需要做的是计算出一个介于0和1之间的因子,表明你完成的距离(根据你自开始以来经过的时间)< / p>

某处记录开始时间。 var startTime = DateTime.UtcNow;

然后在您的更新代码中执行此操作

var elapsedTime = DateTime.UtcNow - startTime; //How long as elapsed
int progress = elapsedTime.TotalSeconds / 5.0; //Small number / a large number is always 0..1
if (progress > 1) 
  progress = 1;

然后进度条根据0..MaxValue而不是0..1的比例显示进度,因此您只需将当前进度因子乘以progressBar.MaxValue

someProgressBar.Value = (int)Math.Ceiling(someProgressBar.MaxValue * progress);

这与计算百分比(每百个)完全相同,你只需将0..1因子乘以100.这就是你如何算出一个百分比(smallNumber / bigNumber)* 100。

PS:如果按照我的建议执行并使用绝对时间,那么进度条将始终在正确的时间完成。在循环中添加小增量(例如在计时器中)的问题是,如果应用程序繁忙/滞后,那么您的计时器将完成得太晚。

答案 1 :(得分:1)

这只是按比例增加它们的情况。

因此,我们可以说,为了论证,它们都会增加5倍。只需将每个最大值除以5,然后将每个步长增量除以该数量。

int[] sortedArray = new int[] { 289, 151, 69, 27, 6 };
var incrementCount = 5.0;
int incrementAmount = sortedArray.Select(x => Math.Ceil((float)x/incrementCount )).ToArray();

现在,您有一个数组,其中包含每个进度条的相应增量步骤。

答案 2 :(得分:0)

你需要找一些地方来编写代码来增加进度条的值。

计时器是一个很好的选择,Thread您无法定期更新进度条。

现在让我们创建一个每秒钟滴答的计时器,并在计时器滴答时更新进度条。我们选择System.Windows.Forms.Timer,因此Tick事件处理程序在UI线程上运行。

System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer();
myTimer.Interval = 1000;
myTimer.Tick += (sender, e) => UpdateProgressBars();
myTimer.Start();

下一步是计算每个进度条的值。

对于第一个ProgressBar,在第一个刻度线,值为289/5;在第二个刻度,它将是(289/5 * 2)。

因此UpdateProgressBars方法看起来像这样

int counter = 0; //the counter for tick event
private void UpdateProgressBars(object sender, EventArgs e)
{
    counter++;
    progressbar1.Value = sortedArray[0] * counter / 5; 
    progressbar2.Value = sortedArray[1] * counter / 5;
    ...
}

答案 3 :(得分:0)

我这里只考虑一个值,但解决方案很容易扩展到多个值。

输入:

  • 最大值:V_max
  • Timespan,最后进度值应该等于V_maxtimespan

进展值:

  • V_progress

助手:

  • StopWatch跟踪已用时间:stopwatch
  • System.Timers.Timer每10分钟执行一次定期更新:timer

当秒表的经过时间达到输入时间时,请停止更新并将V_progress设置为V_max。否则,根据已用时间将V_progress计算为V_max的一小部分。

var V_max = 254;
var V_progress = 0.0;
var timespan = new TimeSpan(0, 0, 0, 5, 0);
var stopwatch = new Stopwatch();
var timer = new System.Timers.Timer(10);
timer.Elapsed += (sender, e) =>
{
    var elapsedMs = stopwatch.Elapsed.TotalMilliseconds;
    var percentage = stopwatch.Elapsed.TotalMilliseconds / timespan.TotalMilliseconds;
    if (percentage >= 1.0)
    {
        timer.Stop();
        stopwatch.Stop();
        percentage = 1.0;
    }
    // probably dispatch this assignment to the UI thread for the actual code
    V_progress = percentage * V_max;
    // test output
    Console.WriteLine(V_progress);
};


stopwatch.Start();
timer.Start();

答案 4 :(得分:0)

让自己轻松一点 - 使用Microsoft的Reactive Framework(NuGet“System.Reactive.Windows.Forms”(假设这是Windows Forms))。然后你可以这样做:

ProgressBar[] progressBars = new ProgressBar[] { /* pbs here */ };
int[] sortedArray = new int[] { 289, 151, 69, 27, 6 };

var updates = 100;
Observable
    .Interval(TimeSpan.FromSeconds(5.0 / updates))
    .Take(updates)
    .Select(n => sortedArray.Select(x => x * ((int)n + 1) / updates).ToArray())
    .ObserveOn(this)
    .Subscribe(x =>
    {
        for (var i = 0; i < sortedArray.Length; i++)
        {
            progressBars[i].Value = x[i];
        }
    });

我已经测试过这段代码,就我所知,它的工作正常。