mirror of
https://github.com/sourcegit-scm/sourcegit.git
synced 2024-12-24 20:57:19 -08:00
optimize<Avatar>: use task queue to download avatar one by one
This commit is contained in:
parent
c22ea8f4cf
commit
6a9ee67524
1 changed files with 33 additions and 14 deletions
|
@ -73,6 +73,11 @@ namespace SourceGit.Helpers {
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private static Dictionary<string, BitmapImage> loaded = new Dictionary<string, BitmapImage>();
|
private static Dictionary<string, BitmapImage> loaded = new Dictionary<string, BitmapImage>();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Loader to join in queue.
|
||||||
|
/// </summary>
|
||||||
|
private static Task loader = null;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Render implementation.
|
/// Render implementation.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -150,23 +155,37 @@ namespace SourceGit.Helpers {
|
||||||
requesting.Add(email, new List<Avatar>());
|
requesting.Add(email, new List<Avatar>());
|
||||||
requesting[email].Add(this);
|
requesting[email].Add(this);
|
||||||
|
|
||||||
Task.Run(() => {
|
Action job = () => {
|
||||||
try {
|
try {
|
||||||
var agent = new WebClient();
|
HttpWebRequest req = WebRequest.CreateHttp("https://www.gravatar.com/avatar/" + md5 + "?d=404");
|
||||||
var data = agent.DownloadData("https://www.gravatar.com/avatar/" + md5 + "?d=404");
|
req.Timeout = 2000;
|
||||||
//var data = agent.DownloadData("https://cdn.s.loli.top/avatar/" + md5 + "?d=404");
|
req.Method = "GET";
|
||||||
File.WriteAllBytes(filePath, data);
|
|
||||||
|
|
||||||
if (requesting.ContainsKey(email)) {
|
HttpWebResponse rsp = req.GetResponse() as HttpWebResponse;
|
||||||
Dispatcher.Invoke(() => {
|
if (rsp.StatusCode == HttpStatusCode.OK) {
|
||||||
var img = new BitmapImage(new Uri(filePath));
|
using (Stream reader = rsp.GetResponseStream())
|
||||||
loaded[email] = img;
|
using (FileStream writer = File.OpenWrite(filePath)) {
|
||||||
foreach (var one in requesting[email]) one.Source = img;
|
reader.CopyTo(writer);
|
||||||
requesting.Remove(email);
|
}
|
||||||
});
|
|
||||||
|
if (requesting.ContainsKey(email)) {
|
||||||
|
Dispatcher.Invoke(() => {
|
||||||
|
var img = new BitmapImage(new Uri(filePath));
|
||||||
|
loaded[email] = img;
|
||||||
|
foreach (var one in requesting[email]) one.Source = img;
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch {}
|
} catch { }
|
||||||
});
|
|
||||||
|
requesting.Remove(email);
|
||||||
|
};
|
||||||
|
|
||||||
|
if (loader != null && !loader.IsCompleted) {
|
||||||
|
loader = loader.ContinueWith(t => { job(); });
|
||||||
|
} else {
|
||||||
|
loader = Task.Run(job);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
Loading…
Reference in a new issue