Skip to content

Commit 1095f38

Browse files
committed
Improved async pattern
1 parent e11fb60 commit 1095f38

File tree

1 file changed

+30
-29
lines changed

1 file changed

+30
-29
lines changed

DeadLock/Forms/FrmMain.cs

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -159,46 +159,47 @@ private void LanguageSwitch()
159159
private async void Update(bool showError, bool showNoUpdates)
160160
{
161161
Language l = _languageManager.GetLanguage();
162-
await Task.Run(() =>
162+
163+
try
163164
{
164-
try
165-
{
166-
WebClient wc = new WebClient();
167-
string xml = wc.DownloadString("http://codedead.com/Software/DeadLock/update.xml");
165+
WebClient wc = new WebClient();
166+
string xml = await wc.DownloadStringTaskAsync("http://codedead.com/Software/DeadLock/update.xml");
168167

169-
XmlSerializer serializer = new XmlSerializer(_update.GetType());
170-
using (MemoryStream stream = new MemoryStream())
171-
{
172-
StreamWriter writer = new StreamWriter(stream);
173-
writer.Write(xml);
174-
writer.Flush();
175-
stream.Position = 0;
176-
_update = (Update)serializer.Deserialize(stream);
177-
writer.Dispose();
178-
}
179-
if (_update.CheckForUpdate())
168+
XmlSerializer serializer = new XmlSerializer(_update.GetType());
169+
using (MemoryStream stream = new MemoryStream())
170+
{
171+
StreamWriter writer = new StreamWriter(stream);
172+
writer.Write(xml);
173+
writer.Flush();
174+
stream.Position = 0;
175+
_update = (Update)serializer.Deserialize(stream);
176+
writer.Dispose();
177+
}
178+
if (_update.CheckForUpdate())
179+
{
180+
if (MessageBoxAdv.Show(l.MsgVersion + " " + _update.GetUpdateVersion() + " " + l.MsgAvailable + Environment.NewLine + l.MsgDownloadNewVersion, "DeadLock", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
180181
{
181-
if (MessageBoxAdv.Show(l.MsgVersion + " " + _update.GetUpdateVersion() + " " + l.MsgAvailable + Environment.NewLine + l.MsgDownloadNewVersion, "DeadLock", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
182+
await Task.Run(() =>
182183
{
183184
new FrmUpdater(_update, _languageManager.GetLanguage()).ShowDialog();
184-
}
185-
}
186-
else
187-
{
188-
if (showNoUpdates)
189-
{
190-
MessageBoxAdv.Show(l.MsgLatestVersionAlreadyInstalled, "DeadLock", MessageBoxButtons.OK, MessageBoxIcon.Information);
191-
}
185+
});
192186
}
193187
}
194-
catch (Exception ex)
188+
else
195189
{
196-
if (showError)
190+
if (showNoUpdates)
197191
{
198-
MessageBoxAdv.Show(ex.Message, "DeadLock", MessageBoxButtons.OK, MessageBoxIcon.Error);
192+
MessageBoxAdv.Show(l.MsgLatestVersionAlreadyInstalled, "DeadLock", MessageBoxButtons.OK, MessageBoxIcon.Information);
199193
}
200194
}
201-
});
195+
}
196+
catch (Exception ex)
197+
{
198+
if (showError)
199+
{
200+
MessageBoxAdv.Show(ex.Message, "DeadLock", MessageBoxButtons.OK, MessageBoxIcon.Error);
201+
}
202+
}
202203
}
203204

204205
/// <summary>

0 commit comments

Comments
 (0)