Fixed event handling.

This commit is contained in:
Michael Bucari-Tovo 2021-08-12 12:47:02 -06:00
parent 7acaac7bd3
commit 5ab4183f9b
2 changed files with 11 additions and 17 deletions

View File

@ -21,13 +21,13 @@ namespace LibationWinForms.BookLiberation
processable.Completed += OnCompleted;
processable.StatusUpdate += OnStatusUpdate;
//If IStreamable.StreamingCompleted is never fired, we still
//need to dispose of the form after IProcessable.Completed
//The form is created on IProcessable.Begin and we
//dispose of it on IProcessable.Completed
processable.Completed += OnCompletedDispose;
//Don't unsubscribe from Dispose because it fires on
//IStreamable.StreamingCompleted, and the IProcessable
//events need to live past that event.
//Don't unsubscribe from Dispose because it fires when
//IStreamable.StreamingCompleted closes the form, and
//the IProcessable events need to live past that event.
processable.Completed += OnUnsubscribeAll;
}
}

View File

@ -34,12 +34,12 @@ namespace LibationWinForms.BookLiberation
Streamable.StreamingProgressChanged += OnStreamingProgressChanged;
Streamable.StreamingTimeRemaining += OnStreamingTimeRemaining;
Disposed += OnUnsubscribeAll;
FormClosed += OnUnsubscribeAll;
}
private void OnUnsubscribeAll(object sender, EventArgs e)
{
Disposed -= OnUnsubscribeAll;
FormClosed -= OnUnsubscribeAll;
Streamable.StreamingBegin -= OnStreamingBegin;
Streamable.StreamingCompleted -= OnStreamingCompleted;
@ -55,30 +55,24 @@ namespace LibationWinForms.BookLiberation
//thread that created form, otherwise the form and the window handle will be on
//different threads. Form.BeginInvoke won't work until the form is created
//(ie. shown) because control doesn't get a window handle until it is Shown.
static void sendCallback(object asyncArgs)
{
var e = asyncArgs as AsyncCompletedEventArgs;
((Action)e.UserState)();
}
Action code = Show;
Action show = Show;
if (InvokeRequired)
SyncContext.Send(
sendCallback,
new AsyncCompletedEventArgs(null, false, code));
new AsyncCompletedEventArgs(null, false, show));
else
code();
show();
}
public virtual void OnStreamingProgressChanged(object sender, DownloadProgress downloadProgress) { }
public virtual void OnStreamingTimeRemaining(object sender, TimeSpan timeRemaining) { }
public virtual void OnStreamingCompleted(object sender, string completedString)
=> this.UIThread(() =>
{
Close();
Dispose();
});
public virtual void OnStreamingCompleted(object sender, string completedString) => this.UIThread(() => Close());
#endregion
}
}