Skip to content

Commit 7543ba6

Browse files
committed
fix: improve progress dialog behaviour
Readded a separate STA thread and static instance of the dialog to avoid thread state issues
1 parent 1424e03 commit 7543ba6

File tree

2 files changed

+66
-39
lines changed

2 files changed

+66
-39
lines changed

Common/DaisyAddinWPFLib/WPFConverter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public ConversionStatus requestUserParameters(DocumentProperties currentDocument
3333
// progressDialog.Focus();
3434
// return ConversionStatus.ReadyForConversion;
3535
//}
36-
((WPFEventsHandler)this.EventsHandler).Dialog.Close();
36+
//((WPFEventsHandler)this.EventsHandler).Dialog.Close();
3737
return ConversionStatus.Canceled;
3838
}
3939

Common/DaisyAddinWPFLib/WPFEventsHandler.cs

Lines changed: 65 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -15,79 +15,106 @@ namespace Daisy.SaveAsDAISY.WPF
1515
{
1616
public class WPFEventsHandler : Daisy.SaveAsDAISY.Conversion.Events.IConversionEventsHandler
1717
{
18+
private static ConversionProgress DialogInstance = null;
1819

19-
public ConversionProgress Dialog = null;
20-
private Dispatcher _dispatcher;
2120

2221
#region Conversion progress dialog
2322
public void TryInitializeProgress(string message, int maximum = 1, int step = 1)
2423
{
25-
Dispatcher.CurrentDispatcher.Invoke((Action)delegate {
26-
try {
27-
if (Dialog == null) {
28-
Dialog = new ConversionProgress();
29-
Dialog.Closed += Dialog_Closed;
24+
try {
25+
if (DialogInstance == null) {
26+
var test = new Thread(() =>
27+
{
28+
DialogInstance = new ConversionProgress();
29+
DialogInstance.Closed += Dialog_Closed;
3030
if (cancelButtonClicked != null) {
31-
Dialog.setCancelClickListener(cancelButtonClicked);
31+
DialogInstance.setCancelClickListener(cancelButtonClicked);
3232
}
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));
33+
DialogInstance.Show();
3834

35+
DialogInstance.Dispatcher.Invoke(() => DialogInstance.InitializeProgress(message, maximum, step));
36+
while(DialogInstance != null) {
37+
Dispatcher.Run();
38+
}
39+
40+
});
41+
test.SetApartmentState(ApartmentState.STA);
42+
test.Start();
43+
Thread.Sleep(500); // give some time to show the dialog
44+
45+
} else {
46+
DialogInstance.Dispatcher.Invoke(() => {
47+
DialogInstance.Activate();
48+
DialogInstance.InitializeProgress(message, maximum, step);
49+
});
3950
}
40-
catch (Exception e) {
41-
AddinLogger.Error("Unable to show message in progress dialog: " + message);
42-
}
43-
});
51+
52+
}
53+
catch (Exception e) {
54+
AddinLogger.Error("Unable to show message in progress dialog: " + message + " " + e.Message);
55+
}
4456
}
4557

4658
private event CancelClickListener cancelButtonClicked = null;
4759

4860
public void setCancelClickListener(CancelClickListener cancelAction)
4961
{
5062
cancelButtonClicked = cancelAction;
51-
if (Dialog != null) {
52-
Dialog.setCancelClickListener(cancelAction);
63+
if (DialogInstance != null) {
64+
DialogInstance.setCancelClickListener(cancelAction);
5365
}
5466
}
5567

5668
private void Dialog_Closed(object sender, EventArgs e)
5769
{
58-
Dialog = null;
70+
DialogInstance = null;
5971
}
6072

6173
private void TryShowMessage(string message, bool isProgress = false)
6274
{
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);
75+
try {
76+
if (DialogInstance == null) {
77+
var test = new Thread(() =>
78+
{
79+
DialogInstance = new ConversionProgress();
80+
DialogInstance.Closed += Dialog_Closed;
81+
if (cancelButtonClicked != null) {
82+
DialogInstance.setCancelClickListener(cancelButtonClicked);
83+
}
84+
DialogInstance.Show();
85+
86+
DialogInstance.Dispatcher.Invoke(() => DialogInstance.AddMessage(message, isProgress));
87+
while (DialogInstance != null) {
88+
Dispatcher.Run();
89+
}
90+
91+
});
92+
test.SetApartmentState(ApartmentState.STA);
93+
test.Start();
94+
Thread.Sleep(500); // give some time to show the dialog
95+
} else {
96+
DialogInstance.Dispatcher.Invoke(() => {
97+
DialogInstance.Activate();
98+
DialogInstance.AddMessage(message, isProgress);
99+
});
76100
}
77-
});
101+
}
102+
catch (Exception e) {
103+
AddinLogger.Error("Unable to show message in progress dialog: " + message + " " + e.Message);
104+
}
78105
}
79106

80107

81108

82109
private void TryClosingDialog(int sleepBefore)
83110
{
84-
if (Dialog == null) {
111+
if (DialogInstance == null) {
85112
return;
86113
}
87-
Dialog.Dispatcher.Invoke(() => {
114+
DialogInstance.Dispatcher.Invoke(() => {
88115
Thread.Sleep(sleepBefore);
89-
Dialog.Close();
90-
Dialog = null;
116+
DialogInstance.Close();
117+
DialogInstance = null;
91118
});
92119
}
93120
#endregion

0 commit comments

Comments
 (0)