void Generating(BackgroundWorker worker, DoWorkEventArgs e)
{
DateTime startTime = DateTime.Now;
TimeSpan workTime = new TimeSpan(0, minutes, 0);
DateTime stopTime = startTime + workTime;
bool isStopGenerating = false;
do
{ // The generating cycle
if (worker.CancellationPending)
{ // The Generating was stopped by the user
e.Cancel = true;
isStopGenerating = true;
}
else if (minutes > 0 && stopTime < DateTime.Now)
{ // The time finished
isStopGenerating = true;
}
else
{ // The main job
GenerateStrategySlots();
GenerateSameOppSignal();
GeneratePermanentSL();
GeneratePermanentTP();
// Calculates the back test.
bool isBetter = CalculateTheResult(false);
// Initial Optimization
if (chbInitialOptimisation.Checked)
PerformInitialOptimization(worker, isBetter);
}
if (minutes > 0)
{
// Report progress as a percentage of the total task.
TimeSpan passedTime = DateTime.Now - startTime;
int percentComplete = (int)(100 * passedTime.TotalSeconds / workTime.TotalSeconds);
percentComplete = percentComplete > 100 ? 100 : percentComplete;
if (percentComplete > progressPercent)
{
progressPercent = percentComplete;
worker.ReportProgress(percentComplete);
}
}
} while (!isStopGenerating);
return;
}