public static IPropagatorBlock<File, FileLine> GetFileLinesEnumeratorBlock()
        {
            var resultsBlock = new BufferBlock<FileLine>();
            var actionBlock = new ActionBlock<File>(
                async file =>
                {
                    using (var reader = new System.IO.StreamReader(new System.IO.FileStream(
                        file.FullPath,
                        System.IO.FileMode.Open,
                        System.IO.FileAccess.Read,
                        System.IO.FileShare.Read,
                        bufferSize: 4096,
                        useAsync: true)))
                    {
                        string line;
                        var row = 1;
                        while ((line = await reader.ReadLineAsync()) != null)
                        {
                            if (!string.IsNullOrWhiteSpace(line))
                            {
                                resultsBlock.Post(new FileLine(file, row, line));
                            }

                            row++;
                        }
                    }
                },
                new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = Utils.GlobalMaxDegreeOfParallelism });

            actionBlock.PropagateCompleted(resultsBlock);
            return DataflowBlock.Encapsulate(actionBlock, resultsBlock);
        }
Beispiel #2
0
 // 根据用户名找用户
 public async Task<IdentityUser> FindByNameAsync(string userName)
 {
     using (var stream = new System.IO.StreamReader(_filePath))
     {
         string line;
         IdentityUser result = null;
         while ((line = await stream.ReadLineAsync()) != null)
         {
             var user = IdentityUser.FromString(line);
             if (user.UserName == userName)
             {
                 result = user;
                 break;
             }
         }
         return result;
     }
 }
Beispiel #3
0
 private async void LoadBMSFileAsync(IProgress<string> progress)
 {
     await Task.Run(async () =>
     {
         System.Console.WriteLine("Load start.");
         IsLoading = true;
         System.IO.FileStream fs
             = new System.IO.FileStream(Path, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite);
         System.IO.StreamReader sr
             = new System.IO.StreamReader(fs);
         string result = string.Empty;
         string buffer = await sr.ReadLineAsync();
         while (buffer != null)
         {
             //progress.Report(buffer);
             System.Console.WriteLine(buffer);
             buffer.Replace("\n", "").Replace("\r", "");
             if (buffer != string.Empty)
                 InterpretCommand(buffer);
             result += buffer + System.Environment.NewLine;
             buffer = await sr.ReadLineAsync();
         }
         sr.Close();
         /* set bmsData magnitude */
         foreach(BmsData source in ListBmsDataCh2) //error may happen because list is not initialized
         {
             foreach (BmsData target in ListBmsData)
             {
                 if (target.BarNumber == source.BarNumber)
                 {
                     target.BarMagnitude = source.BarMagnitude;
                 }
             }
         }
         /* calc time */
         int bar = 0;
         int tempTime = 10000;
         int nextTime = tempTime;
         ListBmsData.Sort((a, b) => a.BarNumber - b.BarNumber);
         foreach(BmsData tmp in ListBmsData)
         {
             if (bar == tmp.BarNumber)
             {
                 tmp.calcObjectTime(tempTime);
             }
             else if(bar + 1 == tmp.BarNumber){
                 tempTime = nextTime;
                 nextTime = tmp.calcObjectTime(tempTime);
                 bar++;
             }
         }
         /* check bms data */
         foreach(BmsData tmp in ListBmsData)
         {
             if(tmp.Channel == 11)
                 tmp.printBmsData();
         }
         /* check wav data */
         /*
         foreach(KeyValuePair<int, Wav> dic in WavDictionary)
         {
             System.Console.WriteLine("Wav{0}:{1}, {2}", dic.Key, dic.Value.FileName, dic.Value.Number); 
         }
         */
         IsLoading = false;
     }
     );
 }
			/// <summary>
			/// Carga las palabras y su definicion de forma asincrona y verifica que no haya ningun problema
			/// </summary>
			/// <returns>The words async.</returns>
			async Task CargaPaisesAsync ()
			{


				//Obtiene el recursos desde la tarea asincrona para poder abrir un input stream 
				//y leer el archivo de recurso alojado en la carpeta raw
				var resources = helperContext.Resources;
				var inputStream = resources.OpenRawResource (Resource.Raw.paises);

				using (var reader = new System.IO.StreamReader(inputStream)) {

					try {
						String line;
						//ciclo que lee una linea del archivo
						while ((line = await reader.ReadLineAsync ()) != null) {
							//Hace un split de la linea usando como separador el carcter "@"
							String[] strings = TextUtils.Split (line, "@");
							//verifica que tenga dos elementos la linea(la palabra y su definicion)
							if (strings.Length < 2) 
								continue;
							//invoca al metodo para guardar la palabra y su definicion en la tabla
							long id = AddPais(strings [0].Trim (), strings [1].Trim ());
							if (id < 0) 
							{
								Log.Error (TAG, "unable to add word: " + strings [0].Trim ());
							}
						}
					} finally {
						reader.Close ();
					}
				}

				Log.Debug (TAG, "DONE loading words.");
			}