Exemple #1
0
 private static async Task Write(ProcessingState state)
 {
     await using var stream = File.Open(Constants.StateFilePath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read);
     stream.SetLength(0);
     await using var reader = new StreamWriter(stream, Encoder);
     await reader.WriteAsync(state.ToString());
 }
Exemple #2
0
        private static async Task <ProcessingState> Read()
        {
            if (!File.Exists(Constants.StateFilePath))
            {
                return(new ProcessingState());
            }

            await using var stream = File.Open(Constants.StateFilePath, FileMode.Open, FileAccess.Read, FileShare.Read);
            stream.Seek(0, SeekOrigin.Begin);
            using var reader = new StreamReader(stream, Encoder);
            var content = await reader.ReadToEndAsync();

            return(ProcessingState.FromString(content) ?? new ProcessingState());
        }
Exemple #3
0
        public async Task <int> GetIndex(NotificationSource source)
        {
            //multiple requests may try to obtain the new index
            //Therefore, to prevent raise conditions, we need to use locking mechanism.
            //It will ensure that only one thread obtains new index.
            await _lock.WaitAsync();

            try
            {
                var state = await Get();

                state.IncrementIndex(source);
                await Write(state);

                _state = state;
                return(state.GetIndex(source));
            }
            finally
            {
                _lock.Release();
            }
        }