From bdb347debe455ef86e29f01a81bfcae4a7c5c16c Mon Sep 17 00:00:00 2001 From: emkael Date: Sat, 20 Dec 2014 18:49:46 +0000 Subject: * parallel code troubleshoot and bugfix git-svn-id: https://svn.emkael.info/an9k@30 05ec0d5d-773b-4d93-9e23-c81a7ac79feb --- Analizator9000/Analizator9000/Accumulator.cs | 63 ++++++++++++++++------------ 1 file changed, 36 insertions(+), 27 deletions(-) (limited to 'Analizator9000') diff --git a/Analizator9000/Analizator9000/Accumulator.cs b/Analizator9000/Analizator9000/Accumulator.cs index 36c175c..9ed04cf 100644 --- a/Analizator9000/Analizator9000/Accumulator.cs +++ b/Analizator9000/Analizator9000/Accumulator.cs @@ -111,7 +111,10 @@ namespace Analizator9000 for (int i = 0; i < toRun; i++) { Action worker = this.analyzeDeal; - worker.BeginInvoke(this.deals.Pop(), this.endAnalyze, worker); + lock (this.deals) + { + worker.BeginInvoke(this.deals.Pop(), this.endAnalyze, worker); + } this.threadsRunning++; } return this.deals.Count; @@ -178,6 +181,7 @@ namespace Analizator9000 this.abort = true; } + private Object threadLock = new Object(); /// /// Callback method for worker threads, ends the single deal analysis, updates the total result and fires next analysis if necessary. /// @@ -185,41 +189,46 @@ namespace Analizator9000 private void endAnalyze(IAsyncResult methodResult) { ((Action)methodResult.AsyncState).EndInvoke(methodResult); - bool finished = false; - if (this.abort) - { - this.form.setProgress(0); - this.form.addStatusLine("Analiza przewana. Częściowe wyniki w pliku: " + this.filename); - finished = true; - } - else + lock (this.threadLock) { - this.threadsRunning--; - this.analyzed++; - this.form.setProgress((int)(100 * this.analyzed / this.toAnalyze)); - if (threadsRunning == 0 && this.deals.Count == 0) + bool finished = false; + if (this.abort) { - this.form.setProgress(100); - this.form.addStatusLine("Analiza zakończona. Wyniki w pliku: " + this.filename); + this.form.setProgress(0); + this.form.addStatusLine("Analiza przewana. Częściowe wyniki w pliku: " + this.filename); finished = true; - } - if (threadsRunning < this.portionSize) + } + else { - // Increasing the parameter would cause exponential thread creation rate. Funny. - this.run(1); + + this.threadsRunning--; + this.analyzed++; + this.form.setProgress((int)(100 * this.analyzed / this.toAnalyze)); + if (threadsRunning == 0 && this.deals.Count == 0) + { + this.form.setProgress(100); + this.form.addStatusLine("Analiza zakończona. Wyniki w pliku: " + this.filename); + finished = true; + } + if (threadsRunning < this.portionSize) + { + // Increasing the parameter would cause exponential thread creation rate. Funny. + this.run(1); + } } - } - if (finished) - { - try + if (finished) { - this.outputFile.WriteLine(this.getString()); - this.outputFile.Close(); + try + { + this.outputFile.WriteLine(this.getString()); + this.outputFile.Close(); + } + catch (Exception) { }; + this.form.endAnalysis(); } - catch (Exception) { }; - this.form.endAnalysis(); } } + /// /// Presents the current analysis results in textual form. -- cgit v1.2.3