private async Task SaveUnMemorizedWords() { DataProcesser dataproc = new DataProcesser(); StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync("words.txt", CreationCollisionOption.OpenIfExists); List <string[]> data = await dataproc.ReadAsync(file); foreach (string[] wordInformation in todayTask) { for (int i = 0; i < data.Count; i++) { if (data[i][0] == wordInformation[0]) { //找到对应的单词 data[i][data[i].Length - 2] = DateTime.Today.ToString(); data[i][data[i].Length - 1] = "0"; break; } } } memorizedWord.Clear(); await dataproc.WriteAsync(file, data); data.Clear(); GC.Collect(); }
private void ContentDialog_PrimaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args) { Task readFile = new Task(async(encoderSelectedIndex) => { DataProcesser dataproc = new DataProcesser(); //打开要导入的文件 List <string[]> data = await dataproc.ReadAsync(file, (DataProcesser.EncoderType)encoderSelectedIndex); //打开数据文件 StorageFolder folder = ApplicationData.Current.LocalFolder; //如果存在就打开,不存在就创建 file = await folder.CreateFileAsync("words.txt", CreationCollisionOption.OpenIfExists); List <string[]> orgData = await dataproc.ReadAsync(file, DataProcesser.EncoderType.UTF_8); //对比有没有一样的单词 List <string> tempWordIndex = new List <string>(); //tempWordIndex存储所有的英语单词,用于查找 for (int i = 0; i < orgData.Count; i++) //填充tempWordIndex { tempWordIndex.Add(orgData[i][0]); } foreach (string[] wordInformationForInput in data) { //遍历要导入的数据的每一行 int index = tempWordIndex.IndexOf(wordInformationForInput[0]); //检测英语单词是否相同 //下面检查汉语意思是否有相同 if (index != -1) //若在原数据的index处存在相同的英语单词 { List <string> orgWordInformationList = new List <string>(orgData[index]); //首先在orgData中取出该单词的原始数组 foreach (string hansMeaning in wordInformationForInput) { //遍历每一个汉语意思 if (orgWordInformationList.IndexOf(hansMeaning) == -1) { //如果没有找到相同的 orgWordInformationList.Insert(1, hansMeaning); //添加到汉语意思 orgWordInformationList[orgWordInformationList.Count - 1] = "0"; //重置剩余天数 } } orgData[index] = orgWordInformationList.ToArray(); //存回orgData; } else { //如果没找到相同的 //添加时间戳和剩余天数 string[] tempWordInformationForInput = new string[wordInformationForInput.Length + 2]; wordInformationForInput.CopyTo(tempWordInformationForInput, 0); tempWordInformationForInput[wordInformationForInput.Length] = DateTime.Today.ToString(); //加入今天的日期 tempWordInformationForInput[wordInformationForInput.Length + 1] = "0"; //重置剩余天数 orgData.Add(tempWordInformationForInput); //添加到数据表末尾 tempWordIndex.Add(wordInformationForInput[0]); //添加到临时词汇表末尾 } } //现在开始存储数据 await dataproc.WriteAsync(file, orgData); ReadInputFileOk(null, null); }, Encoder.SelectedIndex); //Encoder.SelectedIndex传入选择的编码,这破uwp不支持Dispatcher.Invoke() if (file != null) { readFile.Start(); } }
/// <summary> /// 刷新今日任务 /// </summary> /// <returns>返回一个异步Task</returns> private async Task RefreshAsync() { //填充今日任务列表 Random randomNumber = new Random(); DataProcesser dataproc = new DataProcesser(); List <string[]> data = await dataproc.ReadAsync( await ApplicationData.Current.LocalFolder.CreateFileAsync("words.txt", CreationCollisionOption.OpenIfExists) ); foreach (string[] wordInformation in data) { DateTime formerMemorizeDate = DateTime.Parse(wordInformation[wordInformation.Length - 2]); TimeSpan span = DateTime.Today - formerMemorizeDate; if (span.Days >= Convert.ToInt16(wordInformation[wordInformation.Length - 1])) { //如果大于设定的时间间隔,添加到今日任务 string[] oneWord = new string[6]; //[单词],[汉语意思]x4,[正确选项] oneWord[0] = wordInformation[0]; int correctSelection = randomNumber.Next(1, 5); //生成正确选项的序号 oneWord[5] = correctSelection.ToString(); //将正确选项的序号存入 oneWord[correctSelection] = wordInformation[randomNumber.Next(1, wordInformation.Length - 2)]; //将正确答案存入正确选项对应的序号 for (int i = 1; i < 5; i++) { if (i != correctSelection) { //在其他选项填入随机的汉语意思 bool randomWordOK = false; int randomWordIndex; while (!randomWordOK) { randomWordIndex = randomNumber.Next(0, data.Count); //随机到一个单词组 if (data[randomWordIndex][0] != oneWord[0]) { //随机的单词不是该单词本身 int randomWordElementCount = data[randomWordIndex].Length - 2; for (int j = 1; j < randomWordElementCount; j++) { int randomHansIndex = randomNumber.Next(1, randomWordElementCount); if (data[randomWordIndex][j] != oneWord[correctSelection]) { //随机的汉语意思与正确汉语的意思不同 oneWord[i] = data[randomWordIndex][j]; randomWordOK = true; break; } } } //多次随机直到随机到不同的单词 } } } todayTask.Add(oneWord); } } //填充完毕 //设置保存已背会单词的线程 Task saveWords = new Task(async() => { await Task.Delay(20000); while (todayTask.Count != 0) { //仍旧有今日任务,继续循环 await Task.Delay(20000); if (memorizedWord.Count >= 10) { await SaveMemorizedWords(); } } //否则保存一次,退出线程 await SaveMemorizedWords(); }); saveWords.Start(); //设置UI todayTaskCount = todayTask.Count; await Sum.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { this.Sum.Text = "共: " + data.Count.ToString(); this.Today.Text = "今日: 0 / " + todayTaskCount.ToString(); }); GC.Collect(); }