/
QuadResCombinBackground.cs
144 lines (102 loc) · 3.19 KB
/
QuadResCombinBackground.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Programming by Eric Chauvin.
// Notes on this source code are at:
// ericbreakingrsa.blogspot.com
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
namespace ExampleServer
{
class QuadResCombinBackground : BackgroundWorker
{
private MainForm MForm;
private string SolutionP = "";
private string SolutionQ = "";
private string ProcessName = "No Name";
private QuadResCombinBackground()
{
}
internal QuadResCombinBackground( MainForm UseForm, QuadResWorkerInfo WInfo )
{
MForm = UseForm;
DoWork += new DoWorkEventHandler( QuadResCombinBackground_DoWork );
ProgressChanged += new ProgressChangedEventHandler( QuadResCombinBackground_ProgressChanged );
RunWorkerCompleted += new RunWorkerCompletedEventHandler( QuadResCombinBackground_RunWorkerCompleted );
WorkerReportsProgress = true;
WorkerSupportsCancellation = true;
ProcessName = WInfo.ProcessName;
}
private void QuadResCombinBackground_DoWork(object sender, DoWorkEventArgs e)
{
if( CancellationPending )
return;
if( MForm.GetIsClosing())
return;
BackgroundWorker Worker = (BackgroundWorker)sender;
QuadResWorkerInfo WInfo = (QuadResWorkerInfo)(e.Argument);
try // catch
{
if( Worker.CancellationPending )
{
e.Cancel = true;
return;
}
QuadResCombinatorics QuadResCombin = new QuadResCombinatorics( WInfo, Worker );
QuadResCombin.FindTheFactors();
SolutionP = QuadResCombin.GetSolutionPString();
SolutionQ = QuadResCombin.GetSolutionQString();
QuadResCombin.FreeEverything();
if( Worker.CancellationPending )
{
e.Cancel = true;
return;
}
}
catch( Exception Except )
{
Worker.ReportProgress( 0, "Error in QuadResCombinBackground DoWork process:" );
Worker.ReportProgress( 0, Except.Message );
e.Cancel = true;
}
}
private void QuadResCombinBackground_ProgressChanged( object sender, ProgressChangedEventArgs e )
{
// This runs in the UI thread.
if( CancellationPending )
return;
if( MForm.GetIsClosing())
return;
string CheckStatus = (string)e.UserState;
if( CheckStatus == null )
return;
if( CheckStatus.Length < 1 )
return;
// if( e.ProgressPercentage > 0 )
if( CheckStatus.Trim().Length < 1 )
MForm.ShowQuadResFormStatus( CheckStatus );
else
MForm.ShowQuadResFormStatus( ProcessName + ") " + CheckStatus );
}
private void QuadResCombinBackground_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if( CancellationPending )
return;
if( MForm.GetIsClosing())
return;
if( e.Cancelled )
{
MForm.ShowQuadResFormStatus( "Cancelled." );
return;
}
// MForm.ShowQuadResFormStatus( " " );
MForm.ShowQuadResFormStatus( "Finished Background process." );
// MForm.ShowQuadResFormStatus( "SolutionP: " + SolutionP );
// The string is either "", or it's "0", or it's a real solution.
if( SolutionP.Length > 1 )
{
MForm.FoundSolutionShutDownQuadRes( ProcessName, SolutionP, SolutionQ );
}
}
}
}