这是我到目前为止所做的,但是对于我改变进度条的数字并不重要。值也改变了,我仍然得到相同的结果0.我猜这个问题在于时间表可变,但我无法弄清楚。
import { InMemoryDbService } from 'angular-in-memory-web-api';
export class InMemoryDataService implements InMemoryDbService {
createDb() {
let videos = [
{
"kind": "youtube#searchResult",
"etag": "\"m2yskBQFythfE4irbTIeOgYYfBU/QkoKhBZWzraVqpegM1PMjSXcsNk\"",
"id": {
"kind": "youtube#video",
"videoId": "10V6xet5ODk"
},
"snippet": {
"publishedAt": "2017-02-03T14:38:23.000Z",
"channelId": "UCNqFDjYTexJDET3rPDrmJKg",
"title": "Electro Swing Radio 2017 | 24/7 Music Live Stream | Gaming Music",
"description": "Electro Swing Radio 2017 | 24/7 Music Live Stream | Gaming Music.",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/10V6xet5ODk/default_live.jpg",
"width": 120,
"height": 90
},
"medium": {
"url": "https://i.ytimg.com/vi/10V6xet5ODk/mqdefault_live.jpg",
"width": 320,
"height": 180
},
"high": {
"url": "https://i.ytimg.com/vi/10V6xet5ODk/hqdefault_live.jpg",
"width": 480,
"height": 360
}
},
"channelTitle": "7clouds",
"liveBroadcastContent": "live"
}
},
{ ... some other data }
];
return {videos};
}
}
答案 0 :(得分:0)
试试这个。
public static void Main(string[] args)
{
DateTime StartTime = DateTime.Now;
/// do something here... that actually takes time
Thread.Sleep(TimeSpan.FromSeconds(1));
/// next estimate update
{
double WorkDone = 0.10; // e.g. 10%... give some indication how much work has been done between 0 and 1
TimeSpan TimeSpent = DateTime.Now - StartTime;
TimeSpan TimeOverall = TimeSpan.FromTicks((long) (TimeSpent.Ticks / WorkDone));
TimeSpan TimeRemaining = TimeOverall - TimeSpent;
Console.WriteLine(TimeRemaining.TotalSeconds);
}
}
DateTime.Now分辨率为Ticks或1E-7秒,如果您经常请求时间,则没有时间过去。要获得更高的分辨率,您需要能够测量CPU周期的性能计数器。
答案 1 :(得分:0)
您拥有的代码相当接近,但是您有一个错误:您的starttime
变量是一个局部变量,您可以在尝试使用它来计算经过的时间之前将其重置为当前时间。因此,经过的时间总是零,或非常接近。
您应该将变量移动为实例字段,以便它可以在进度更新之间保留:
DateTime starttime;
private void backgroundWorker1_DoWork(
object sender, System.ComponentModel.DoWorkEventArgs e)
{
int sum = 0;
// Using UtcNow will ensure the time calculation is correct even if
// the work occurs during a daylight saving time change-over
starttimme = DateTime.UtcNow;
for (int i = 1; i <= 100; i++)
{
// run the process here
Thread.Sleep(100);
// end process
sum = sum + i;
backgroundWorker1.ReportProgress(i);
if (backgroundWorker1.CancellationPending)
{
e.Cancel = true;
backgroundWorker1.ReportProgress(0);
return;
}
}
// displays the sum of the process
e.Result = sum;
}
private void backgroundWorker1_ProgressChanged(
object sender, System.ComponentModel.ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
label5.Text = e.ProgressPercentage.ToString() + "%";
var timespent = DateTime.UtcNow - starttime;
// Casting to double here is superfluous. TotalSeconds is already a
// double, and its presence in the expression results in the rest of
// the values being promoted to double and the expression type being double.
double secondsremaining =
(double)(timespent.TotalSeconds / progressBar1.Value) *
(progressBar1.Maximum - progressBar1.Value);
label7.Text = "Time remaining:" + (int)secondsremaining;
Console.WriteLine(secondsremaining);
}