using System.Drawing;
using System.Linq;
using System.Text;
+using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text.RegularExpressions;
public partial class UserInfoDialog : OTBaseForm
{
private TwitterUser _displayUser;
+ private CancellationTokenSource cancellationTokenSource = null;
private readonly TweenMain mainForm;
private readonly Twitter twitter;
this.LabelScreenName.Font = this.ReplaceToGlobalFont(this.LabelScreenName.Font);
}
+ private void CancelLoading()
+ {
+ var newTokenSource = new CancellationTokenSource();
+ var oldTokenSource = Interlocked.Exchange(ref this.cancellationTokenSource, newTokenSource);
+
+ if (oldTokenSource != null)
+ {
+ oldTokenSource.Cancel();
+ oldTokenSource.Dispose();
+ }
+ }
+
public async Task ShowUserAsync(TwitterUser user)
{
if (user == null || user == this._displayUser)
return;
+ this.CancelLoading();
+
+ var cancellationToken = this.cancellationTokenSource.Token;
+
this._displayUser = user;
this.LabelId.Text = user.IdStr;
this.LabelScreenName.Text = user.ScreenName;
- this.LabelName.Text = WebUtility.HtmlDecode(user.Name);
+ this.LabelName.Text = user.Name;
this.LabelLocation.Text = user.Location ?? "";
this.LabelCreatedAt.Text = MyCommon.DateTimeParse(user.CreatedAt).ToString();
await Task.WhenAll(new[]
{
- this.SetDescriptionAsync(user.Description),
- this.SetRecentStatusAsync(user.Status),
- this.SetLinkLabelWebAsync(user.Url),
- this.SetUserImageAsync(user.ProfileImageUrlHttps),
- this.LoadFriendshipAsync(user.ScreenName),
+ this.SetDescriptionAsync(user.Description, cancellationToken),
+ this.SetRecentStatusAsync(user.Status, cancellationToken),
+ this.SetLinkLabelWebAsync(user.Url, cancellationToken),
+ this.SetUserImageAsync(user.ProfileImageUrlHttps, cancellationToken),
+ this.LoadFriendshipAsync(user.ScreenName, cancellationToken),
});
}
- private async Task SetDescriptionAsync(string descriptionText)
+ private async Task SetDescriptionAsync(string descriptionText, CancellationToken cancellationToken)
{
if (descriptionText != null)
{
var atlist = new List<string>();
- var html = WebUtility.HtmlEncode(descriptionText);
+ // description に含まれる < > " の記号のみエスケープを一旦解除する
+ var decodedText = descriptionText.Replace("<", "<").Replace(">", ">").Replace(""", "\"");
+
+ var html = WebUtility.HtmlEncode(decodedText);
html = await this.twitter.CreateHtmlAnchorAsync(html, atlist, null);
html = this.mainForm.createDetailHtml(html);
+ if (cancellationToken.IsCancellationRequested)
+ return;
+
this.DescriptionBrowser.DocumentText = html;
}
else
}
}
- private async Task SetUserImageAsync(string imageUri)
+ private async Task SetUserImageAsync(string imageUri, CancellationToken cancellationToken)
{
var oldImage = this.UserPicture.Image;
if (oldImage != null)
{
var uri = imageUri.Replace("_normal", "_bigger");
- var imageStream = await Networking.Http.GetStreamAsync(uri)
- .ConfigureAwait(false);
+ using (var imageStream = await Networking.Http.GetStreamAsync(uri).ConfigureAwait(false))
+ {
+ var image = await MemoryImage.CopyFromStreamAsync(imageStream)
+ .ConfigureAwait(false);
- return await MemoryImage.CopyFromStreamAsync(imageStream)
- .ConfigureAwait(false);
+ if (cancellationToken.IsCancellationRequested)
+ {
+ image.Dispose();
+ throw new OperationCanceledException(cancellationToken);
+ }
+
+ return image;
+ }
});
}
- private async Task SetLinkLabelWebAsync(string uri)
+ private async Task SetLinkLabelWebAsync(string uri, CancellationToken cancellationToken)
{
if (uri != null)
{
var expandedUrl = await ShortUrl.Instance.ExpandUrlAsync(uri);
+ if (cancellationToken.IsCancellationRequested)
+ return;
+
this.LinkLabelWeb.Text = uri;
this.LinkLabelWeb.Tag = expandedUrl;
this.ToolTip1.SetToolTip(this.LinkLabelWeb, expandedUrl);
}
}
- private async Task SetRecentStatusAsync(TwitterStatus status)
+ private async Task SetRecentStatusAsync(TwitterStatus status, CancellationToken cancellationToken)
{
var atlist = new List<string>();
" Posted at " + MyCommon.DateTimeParse(status.CreatedAt) +
" via " + status.Source);
+ if (cancellationToken.IsCancellationRequested)
+ return;
+
this.RecentPostBrowser.DocumentText = html;
}
else
}
}
- private async Task LoadFriendshipAsync(string screenName)
+ private async Task LoadFriendshipAsync(string screenName, CancellationToken cancellationToken)
{
this.LabelIsFollowing.Text = "";
this.LabelIsFollowed.Text = "";
return new { IsFollowing, IsFollowedBy };
});
+ if (cancellationToken.IsCancellationRequested)
+ return;
+
if (friendship == null)
{
LabelIsFollowed.Text = Properties.Resources.GetFriendshipInfo6;
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
+ var ret = MessageBox.Show(this, Properties.Resources.ChangeIconToolStripMenuItem_Confirm,
+ Application.ProductName, MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
+ if (ret != DialogResult.OK)
+ return;
+
string filename = ((string[])e.Data.GetData(DataFormats.FileDrop, false))[0];
await this.DoChangeIcon(filename);
}
{
if (disposing)
{
+ var cts = this.cancellationTokenSource;
+ cts.Cancel();
+ cts.Dispose();
+
var oldImage = this.UserPicture.Image;
if (oldImage != null)
{