• R/O
  • HTTP
  • SSH
  • HTTPS

open-tween: Commit

開発に使用するリポジトリ


Commit MetaInfo

Revision8ae04f8257c60257cbd35a8c898f3d0bfe6ccad2 (tree)
Time2019-10-05 19:03:17
AuthorKimura Youichi <kim.upsilon@bucy...>
CommiterKimura Youichi

Log Message

TimerCallback内で発生した例外を必ずハンドルするように修正

タイマーでハンドルされない例外が生じると時々プロセスごと異常終了することがあるため

Change Summary

Incremental Difference

--- a/OpenTween/ApplicationEvents.cs
+++ b/OpenTween/ApplicationEvents.cs
@@ -102,6 +102,7 @@ namespace OpenTween
102102 };
103103 Application.ThreadException += (s, e) => OnUnhandledException(e.Exception);
104104 AppDomain.CurrentDomain.UnhandledException += (s, e) => OnUnhandledException((Exception)e.ExceptionObject);
105+ AsyncTimer.UnhandledException += (s, e) => OnUnhandledException(e.Exception);
105106
106107 Application.EnableVisualStyles();
107108 Application.SetCompatibleTextRenderingDefault(false);
--- /dev/null
+++ b/OpenTween/AsyncTimer.cs
@@ -0,0 +1,61 @@
1+// OpenTween - Client of Twitter
2+// Copyright (c) 2019 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
3+// All rights reserved.
4+//
5+// This file is part of OpenTween.
6+//
7+// This program is free software; you can redistribute it and/or modify it
8+// under the terms of the GNU General Public License as published by the Free
9+// Software Foundation; either version 3 of the License, or (at your option)
10+// any later version.
11+//
12+// This program is distributed in the hope that it will be useful, but
13+// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14+// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15+// for more details.
16+//
17+// You should have received a copy of the GNU General Public License along
18+// with this program. If not, see <http://www.gnu.org/licenses/>, or write to
19+// the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
20+// Boston, MA 02110-1301, USA.
21+
22+#nullable enable
23+
24+using System;
25+using System.Threading;
26+using System.Threading.Tasks;
27+
28+namespace OpenTween
29+{
30+ public sealed class AsyncTimer : IDisposable
31+ {
32+ private readonly Func<Task> callback;
33+ private readonly Timer timer;
34+
35+ public static event EventHandler<ThreadExceptionEventArgs>? UnhandledException;
36+
37+ public AsyncTimer(Func<Task> callback)
38+ {
39+ this.callback = callback;
40+ this.timer = new Timer(this.TimerCallback);
41+ }
42+
43+ private async void TimerCallback(object _)
44+ {
45+ try
46+ {
47+ await this.callback().ConfigureAwait(false);
48+ }
49+ catch (Exception ex)
50+ {
51+ UnhandledException?.Invoke(this, new ThreadExceptionEventArgs(ex));
52+ }
53+ }
54+
55+ public void Change(TimeSpan dueTime, TimeSpan period)
56+ => this.timer.Change(dueTime, period);
57+
58+ public void Dispose()
59+ => this.timer.Dispose();
60+ }
61+}
--- a/OpenTween/OpenTween.csproj
+++ b/OpenTween/OpenTween.csproj
@@ -98,6 +98,7 @@
9898 <SubType>Code</SubType>
9999 </Compile>
100100 <Compile Include="ApplicationSettings.cs" />
101+ <Compile Include="AsyncTimer.cs" />
101102 <Compile Include="AtIdSupplement.cs">
102103 <SubType>Form</SubType>
103104 </Compile>
--- a/OpenTween/Resources/ChangeLog.txt
+++ b/OpenTween/Resources/ChangeLog.txt
@@ -1,6 +1,7 @@
11 更新履歴
22
33 ==== Ver 2.4.2-dev(2019/xx/xx)
4+ * FIX: タブ更新時にエラーが発生するとプロセスが異常終了する場合がある不具合を修正
45
56 ==== Ver 2.4.1(2019/09/25)
67 * FIX: 「タブを一覧の下に表示する」を無効にすると起動時にエラーが発生する不具合を修正 (thx @mulsys!)
--- a/OpenTween/ThrottlingTimer.cs
+++ b/OpenTween/ThrottlingTimer.cs
@@ -35,7 +35,7 @@ namespace OpenTween
3535 private const int TIMER_DISABLED = 0;
3636 private const int TIMER_ENABLED = 1;
3737
38- private readonly Timer throttlingTimer;
38+ private readonly AsyncTimer throttlingTimer;
3939 private readonly Func<Task> timerCallback;
4040
4141 private long lastCalledTick;
@@ -68,7 +68,7 @@ namespace OpenTween
6868 this.LastInvoked = DateTimeUtc.MinValue;
6969 this.InvokeLeading = leading;
7070 this.InvokeTrailing = trailing;
71- this.throttlingTimer = new Timer(this.Execute);
71+ this.throttlingTimer = new AsyncTimer(this.Execute);
7272 }
7373
7474 public void Call()
@@ -89,7 +89,7 @@ namespace OpenTween
8989 }
9090 }
9191
92- private async void Execute(object _)
92+ private async Task Execute()
9393 {
9494 var lastCalled = this.LastCalled;
9595 var lastInvoked = this.LastInvoked;
--- a/OpenTween/Thumbnail/Services/ImgAzyobuziNet.cs
+++ b/OpenTween/Thumbnail/Services/ImgAzyobuziNet.cs
@@ -53,7 +53,7 @@ namespace OpenTween.Thumbnail.Services
5353
5454 protected string? ApiBase;
5555 protected IEnumerable<Regex>? UrlRegex = null;
56- protected Timer UpdateTimer;
56+ protected AsyncTimer UpdateTimer;
5757
5858 protected HttpClient http
5959 => this.localHttpClient ?? Networking.Http;
@@ -74,7 +74,7 @@ namespace OpenTween.Thumbnail.Services
7474
7575 public ImgAzyobuziNet(HttpClient? http, bool autoupdate)
7676 {
77- this.UpdateTimer = new Timer(async _ => await this.LoadRegexAsync());
77+ this.UpdateTimer = new AsyncTimer(this.LoadRegexAsync);
7878 this.AutoUpdate = autoupdate;
7979
8080 this.Enabled = true;
@@ -109,10 +109,10 @@ namespace OpenTween.Thumbnail.Services
109109 public bool DisabledInDM { get; set; }
110110
111111 protected void StartAutoUpdate()
112- => this.UpdateTimer.Change(0, 30 * 60 * 1000); // 30分おきに更新
112+ => this.UpdateTimer.Change(TimeSpan.Zero, TimeSpan.FromMinutes(30)); // 30分おきに更新
113113
114114 protected void StopAutoUpdate()
115- => this.UpdateTimer.Change(Timeout.Infinite, Timeout.Infinite);
115+ => this.UpdateTimer.Change(Timeout.InfiniteTimeSpan, Timeout.InfiniteTimeSpan);
116116
117117 public async Task LoadRegexAsync()
118118 {
--- a/OpenTween/TimelineScheduler.cs
+++ b/OpenTween/TimelineScheduler.cs
@@ -30,7 +30,7 @@ namespace OpenTween
3030 {
3131 public class TimelineScheduler
3232 {
33- private readonly Timer timer;
33+ private readonly AsyncTimer timer;
3434
3535 private bool enabled = false;
3636 private bool systemResumeMode = false;
@@ -125,7 +125,7 @@ namespace OpenTween
125125 }
126126
127127 public TimelineScheduler()
128- => this.timer = new Timer(_ => this.TimerCallback());
128+ => this.timer = new AsyncTimer(this.TimerCallback);
129129
130130 public void RefreshSchedule()
131131 {
@@ -148,7 +148,7 @@ namespace OpenTween
148148 this.RefreshSchedule();
149149 }
150150
151- private async void TimerCallback()
151+ private async Task TimerCallback()
152152 {
153153 try
154154 {
Show on old repository browser