Created
December 23, 2013 02:54
-
-
Save tanaka-takayoshi/8091153 to your computer and use it in GitHub Desktop.
Windows Phoneでバックグラウンドタスクを登録するサンプル
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public async Task Load() | |
{ | |
var client = new ChatworkClient(SettingModel.Instance.ApiToken); | |
try | |
{ | |
var status = await client.My.GetStatusAsync(); | |
var myStatus = new MyStatus() | |
{ | |
Token = SettingModel.Instance.ApiToken, | |
Mention = status.mention_num, | |
MyTask = status.mytask_num, | |
Unread = status.unread_num | |
}; | |
MutexedIsoStorageFile.Write(myStatus); | |
StartPeriodicAgent(); | |
var rooms = await client.Room.GetAsync(); | |
Rooms.Clear(); | |
rooms.Select(r => new RoomViewModel(r)) | |
.ToList() | |
.ForEach(Rooms.Add); | |
} | |
catch (Exception e) | |
{ | |
var s = e; | |
} | |
} | |
private string periodicTaskName = "CheckChatworkStatusAgent"; | |
private void StartPeriodicAgent() | |
{ | |
var periodicTask = ScheduledActionService.Find(periodicTaskName) as PeriodicTask; | |
if (periodicTask != null) | |
{ | |
ScheduledActionService.Remove(periodicTaskName); | |
} | |
//Descriptionは設定でユーザーに表示するメッセージです | |
periodicTask = new PeriodicTask(periodicTaskName) | |
{ | |
Description = "Chatworkのステータスを確認します" | |
}; | |
try | |
{ | |
ScheduledActionService.Add(periodicTask); | |
#if(DEBUG_AGENT) | |
ScheduledActionService.LaunchForTest(periodicTaskName, TimeSpan.FromSeconds(60)); | |
#endif | |
} | |
catch (InvalidOperationException exception) | |
{ | |
if (exception.Message.Contains("BNS Error: The action is disabled")) | |
{ | |
MessageBox.Show("このアプリのバックグラウンドタスクが無効になっています。有効にしてください。"); | |
} | |
if (exception.Message.Contains("BNS Error: The maximum number of ScheduledActions of this type have already been added.")) | |
{ | |
Debug.WriteLine("同じ種類のバックグラウンドタスクが登録されています。"); | |
} | |
} | |
catch (SchedulerServiceException ex) | |
{ | |
Debug.WriteLine(ex.ToString()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment