Skip to content

Commit d5a112e

Browse files
committed
fix(event): bypass sta thread error when code is running in task and better report errors with inner exceptions
Also moved webservice check report message after first check
1 parent a2f667f commit d5a112e

3 files changed

Lines changed: 43 additions & 22 deletions

File tree

Common/DaisyAddinWPFLib/WPFEventsHandler.cs

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,30 @@ public class WPFEventsHandler : Daisy.SaveAsDAISY.Conversion.Events.IConversionE
1717
{
1818

1919
public ConversionProgress Dialog = null;
20+
private Dispatcher _dispatcher;
2021

2122
#region Conversion progress dialog
2223
public void TryInitializeProgress(string message, int maximum = 1, int step = 1)
2324
{
24-
if(Dialog == null) {
25-
Dialog = new ConversionProgress();
26-
Dialog.Closed += Dialog_Closed;
27-
if(cancelButtonClicked != null) {
28-
Dialog.setCancelClickListener(cancelButtonClicked);
25+
Dispatcher.CurrentDispatcher.Invoke((Action)delegate {
26+
try {
27+
if (Dialog == null) {
28+
Dialog = new ConversionProgress();
29+
Dialog.Closed += Dialog_Closed;
30+
if (cancelButtonClicked != null) {
31+
Dialog.setCancelClickListener(cancelButtonClicked);
32+
}
33+
Dialog.Show();
34+
_dispatcher = Dispatcher.CurrentDispatcher;
35+
}
36+
_dispatcher.Invoke(() => Dialog.InitializeProgress(message, maximum, step));
37+
//Dialog.Dispatcher.Invoke(() => Dialog.InitializeProgress(message, maximum, step));
38+
2939
}
30-
Dialog.Show();
31-
}
32-
Dialog.Dispatcher.Invoke(() => Dialog.InitializeProgress(message, maximum, step));
40+
catch (Exception e) {
41+
AddinLogger.Error("Unable to show message in progress dialog: " + message);
42+
}
43+
});
3344
}
3445

3546
private event CancelClickListener cancelButtonClicked = null;
@@ -49,12 +60,21 @@ private void Dialog_Closed(object sender, EventArgs e)
4960

5061
private void TryShowMessage(string message, bool isProgress = false)
5162
{
52-
if (Dialog == null) {
53-
Dialog = new ConversionProgress();
54-
Dialog.Closed += Dialog_Closed;
55-
Dialog.Show();
56-
}
57-
Dialog.Dispatcher.Invoke(() => Dialog.AddMessage(message, isProgress));
63+
Dispatcher.CurrentDispatcher.Invoke((Action)delegate {
64+
try {
65+
if (Dialog == null) {
66+
Dialog = new ConversionProgress();
67+
Dialog.Closed += Dialog_Closed;
68+
Dialog.Show();
69+
_dispatcher = Dispatcher.CurrentDispatcher;
70+
}
71+
_dispatcher.Invoke(() => Dialog.AddMessage(message, isProgress));
72+
//Dialog.Dispatcher.Invoke(() => Dialog.AddMessage(message, isProgress));
73+
}
74+
catch (Exception e) {
75+
AddinLogger.Error("Unable to show message in progress dialog: " + message);
76+
}
77+
});
5878
}
5979

6080

@@ -223,15 +243,13 @@ public void onPostProcessingSuccess(ConversionParameters conversion)
223243
+ conversion.OutputPath,
224244
true
225245
);
226-
TryClosingDialog(3000);
227246
}
228247

229248
#endregion
230249

231250
public void onConversionCanceled()
232251
{
233252
TryShowMessage("Canceling conversion");
234-
TryClosingDialog(3000);
235253
}
236254

237255
public void onProgressMessageReceived(object sender, EventArgs e)
@@ -300,11 +318,6 @@ public bool IsContinueDTBookGenerationOnLostElements()
300318
return continueDTBookGenerationResult == MessageBoxResult.Yes;
301319
}
302320

303-
public void OnSuccess()
304-
{
305-
TryClosingDialog(3000);
306-
}
307-
308321
public void OnMasterSubValidationError(string error)
309322
{
310323
MasterSubValidation infoBox = new MasterSubValidation(error, "Validation");

Common/DaisyConverterLib/ConversionResult.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ public string ErrorMessage
7171
if(ErrorDetails is JobRequestError) {
7272
message += "\r\n" + ((JobRequestError)ErrorDetails).Description;
7373
}
74+
var inner = ErrorDetails.InnerException;
75+
while (inner != null) {
76+
message += "\r\n" + inner.Message;
77+
inner = inner.InnerException;
78+
}
7479
return message;
7580
}
7681
return null;

Common/DaisyConverterLib/Pipeline/Webservice.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,13 @@ public void WaitForActivation(IConversionEventsHandler events = null)
3333
int attempt = 20;
3434
bool isWorking = false;
3535
do {
36-
events?.onProgressMessageReceived(this, new DaisyEventArgs($"Checking engine web service status ({attempt} attempts remaining)..."));
3736
attempt--;
3837
try {
3938
AliveData data = this.Alive();
4039
isWorking = !(data == null || !data.Alive);
40+
if (!isWorking) {
41+
events?.onProgressMessageReceived(this, new DaisyEventArgs($"Checking engine web service status ({attempt} attempts remaining)..."));
42+
}
4143
}
4244
catch (AggregateException e) {
4345
isWorking = false;
@@ -126,6 +128,7 @@ public List<ScriptDefinition> GetScripts()
126128
foreach (XmlElement scriptNode in scriptsElm.GetElementsByTagName("script")) {
127129
scripts.Add(ScriptDefinition.FromXml(scriptNode));
128130
}
131+
129132
scripts = scripts.Select(s => GetScriptDetails(s)).ToList();
130133

131134
return scripts;

0 commit comments

Comments
 (0)