Example #1
0
        /// <summary>
        /// The main engine which executes as an infinite loop until it is cancelled or
        /// the queue becomes empty
        /// </summary>
        public void Dequeue()
        {
            while (true)
            {
                if (CancelToken.IsCancellationRequested)
                {
                    CancelToken.ThrowIfCancellationRequested();
                }

                var tasks = CollectActions();

                if (tasks.Count > 0)
                {
                    ProcessAction?.Invoke(new QueueServiceEventArgs(
                                              tasks.Count, EQueueServiceState.BeforeExecute));

                    if (CancelToken.IsCancellationRequested)
                    {
                        CancelToken.ThrowIfCancellationRequested();
                    }

                    Parallel.ForEach(tasks, task => task.ActionMethod());

                    ProcessAction?.Invoke(new QueueServiceEventArgs(tasks.Count,
                                                                    EQueueServiceState.CompletedExecute));
                }
                else if (MarkCompleted && _queue.Count <= 0)
                {
                    break;
                }
            }
        }
            private async Task <bool> InputOneScan(int scanNumber)
            {
                var scan       = new List <ScannedImage>();
                var pageNumber = 1;

                ProgressCallback(scanNumber == -1
                    ? string.Format(MiscResources.BatchStatusPage, pageNumber++)
                    : string.Format(MiscResources.BatchStatusScanPage, pageNumber++, scanNumber + 1));
                CancelToken.ThrowIfCancellationRequested();
                try
                {
                    await DoScan(scanNumber, scan, pageNumber);
                }
                catch (OperationCanceledException)
                {
                    scans.Add(scan);
                    throw;
                }
                if (scan.Count == 0)
                {
                    // Presume cancelled
                    return(false);
                }
                scans.Add(scan);
                return(true);
            }
Example #3
0
        protected override int ExecuteCommand(CommandLineApplication app, IConsole console)
        {
            Log.Info("Listing files...");

            var filesList = GetFilesList(app).ToList();

            Log.Info($"Starting the analyze on {filesList.Count} files.");

            var xcode = new UoeEncryptor(null);

            var i          = 0;
            var outputList = new List <string>();

            foreach (var file in filesList)
            {
                CancelToken?.ThrowIfCancellationRequested();
                var isEncrypted = xcode.IsFileEncrypted(file);
                if (isEncrypted && !ListDecrypted || !isEncrypted && ListDecrypted)
                {
                    outputList.Add(file);
                }
                i++;
                Log.ReportProgress(filesList.Count, i, $"Analyzing {file}.");
            }

            foreach (var file in outputList)
            {
                Out.WriteResultOnNewLine(file);
            }

            Log.Info($"A total of {outputList.Count} files are {(ListDecrypted ? "decrypted" : "encrypted")}.");

            return(0);
        }
 private async Task DoScan(int scanNumber, List <ScannedImage> scan, int pageNumber)
 {
     await scanPerformer.PerformScan(profile, scanParams, BatchForm, null, image =>
     {
         scan.Add(image);
         CancelToken.ThrowIfCancellationRequested();
         ProgressCallback(scanNumber == -1
             ? string.Format(MiscResources.BatchStatusPage, pageNumber++)
             : string.Format(MiscResources.BatchStatusScanPage, pageNumber++, scanNumber + 1));
     }, CancelToken);
 }
Example #5
0
        public bool Next()
        {
            CancelToken.ThrowIfCancellationRequested();

            if (NodeAct == null)
            {
                return(false);
            }

            NodeAct = NodeAct.Next;

            return(NodeAct != null);
        }
Example #6
0
        private int GetGoogleAllLinks(string SearchString, CancellationToken cancelToken)
        {
            int  totalResults = 0, currentPage = 0;
            bool moreResults;

            do
            {
                totalResults += GetGoogleResults(SearchString, MaxResultPerPage, currentPage * MaxResultPerPage, cancelToken, out moreResults);
                currentPage++;
                CancelToken.ThrowIfCancellationRequested();
            } while (moreResults);
            return(totalResults);
        }
            private async Task Input()
            {
                await Task.Factory.StartNew(async() =>
                {
                    scans = new List <List <ScannedImage> >();

                    switch (Settings.ScanType)
                    {
                    case BatchScanType.Single:
                        await InputOneScan(-1);
                        break;

                    case BatchScanType.MultipleWithDelay:
                        {
                            for (var i = 0; i < Settings.ScanCount; i++)
                            {
                                ProgressCallback(string.Format(MiscResources.BatchStatusWaitingForScan, i + 1));
                                if (i != 0)
                                {
                                    ThreadSleepWithCancel(TimeSpan.FromSeconds(Settings.ScanIntervalSeconds), CancelToken);
                                    CancelToken.ThrowIfCancellationRequested();
                                }

                                if (!await InputOneScan(i))
                                {
                                    return;
                                }
                            }

                            break;
                        }

                    case BatchScanType.MultipleWithPrompt:
                        {
                            var i = 0;
                            do
                            {
                                ProgressCallback(string.Format(MiscResources.BatchStatusWaitingForScan, i + 1));
                                if (!await InputOneScan(i++))
                                {
                                    return;
                                }
                                CancelToken.ThrowIfCancellationRequested();
                            } while (PromptForNextScan());

                            break;
                        }
                    }
                }, TaskCreationOptions.LongRunning).Unwrap();
            }
        public void ThrowIfAnyParametersInvalid()
        {
            if (DataPersistenceLayer == null)
            {
                throw new ArgumentException(nameof(DataPersistenceLayer));
            }
            if (ReportExceptionFunction == null)
            {
                throw new ArgumentNullException(nameof(ReportExceptionFunction));
            }
            if (ReportOutputFunction == null)
            {
                throw new ArgumentNullException(nameof(ReportOutputFunction));
            }
            if (LogOutputFunction == null)
            {
                throw new ArgumentNullException(nameof(LogOutputFunction));
            }
            if (ReportResultsFunction == null)
            {
                throw new ArgumentNullException(nameof(ReportResultsFunction));
            }
            if (string.IsNullOrWhiteSpace(SelectedFolder))
            {
                throw new ArgumentNullException(nameof(SelectedFolder));
            }
            if (!Directory.Exists(SelectedFolder))
            {
                throw new DirectoryNotFoundException(SelectedFolder);
            }

            char suppliedDriveLetter = char.ToUpper(SelectedFolder[0]);

            List <char> foundDriveLetters = DriveInfo.GetDrives()
                                            .Where(d => d.IsReady && d.DriveFormat == "NTFS")
                                            .Select(di => di.Name.ToUpper()[0])
                                            .ToList();

            if (!foundDriveLetters.Any() || !foundDriveLetters.Contains(suppliedDriveLetter))
            {
                throw new DriveNotFoundException($"The drive {suppliedDriveLetter}:\\ was not found, the drive was not mounted or ready, or the drive had a file system other than NTFS.");
            }

            if (CancelToken == null)
            {
                throw new ArgumentNullException(nameof(CancelToken), "If you do not want to pass a CancellationToken, then pass 'CancellationToken.None'");
            }
            CancelToken.ThrowIfCancellationRequested();
        }
Example #9
0
        public bool Is(int argCount, Func <LinkedListNode <Token>, LinkedListNode <Token> > argGetNode, Func <Token, bool> argCond)
        {
            var pNode = NodeAct;

            for (var i = 0; i < argCount; i++)
            {
                CancelToken.ThrowIfCancellationRequested();

                if (pNode == null || !argCond.Invoke(pNode.Value))
                {
                    return(false);
                }

                pNode = argGetNode(pNode);
            }

            return(true);
        }
Example #10
0
        private async Task ProcessSocketData()
        {
            IsRunning = true;

            // Buffers for received data
            var receivedMessage = new List <byte>();
            var buffer          = new byte[9192];

            while (true)
            {
                try
                {
                    // Get data until the complete message is received
                    WebSocketReceiveResult response;
                    do
                    {
                        response = await _client.ReceiveAsync(new ArraySegment <byte>(buffer), CancelToken);

                        if (response.CloseStatus != null)
                        {
                            throw new Exception(response.CloseStatusDescription);
                        }
                        var segment = new ArraySegment <byte>(buffer, 0, response.Count);
                        receivedMessage.AddRange(segment);
                    } while (!response.EndOfMessage);

                    // Decode the message
                    var messageJson = Encoding.ASCII.GetString(receivedMessage.ToArray());
                    receivedMessage.Clear();

                    // Parse and notify subscriber
                    var data = JsonConvert.DeserializeObject <TResponse>(messageJson);
                    OnData(_api, data);
                }
                catch (OperationCanceledException) { }

                if (CancelToken.IsCancellationRequested)
                {
                    IsRunning = false;
                    CancelToken.ThrowIfCancellationRequested();
                }
            }
        }
Example #11
0
        /// <summary>
        /// 异步写入单行数据, 可多次执行, 之后执行 Close 方法关闭写入流.
        /// </summary>
        /// <param name="rowData">一行数据,由字段集合组成.</param>
        /// <returns>Task</returns>
        public async Task WriteLineAsync(List <string> rowData)
        {
            if (CancelToken.IsCancellationRequested)
            {
                CancelToken.ThrowIfCancellationRequested();
            }

            if (rowData == null)
            {
                throw new ArgumentNullException(nameof(rowData));
            }

            //如果写入过一条数据, 则字段数固定. 如果再次写入的字段数不同, 报异常.
            if (columnCount > 0 && columnCount != rowData.Count)
            {
                throw new ArgumentException("the rowData count must be equal to " + columnCount.ToString());
            }

            await csvStream.WriteLineAsync(SerializeRow( rowData ));

            TotalRowCount++;

            //设置字段数
            if (columnCount == 0)
            {
                columnCount = rowData.Count;
            }

            //发送通知
            if (dataHandler != null)
            {
                //如果取余数=0, 发送通知.
                if (TotalRowCount % WriteProgressSize == 0L)
                {
                    CsvWriteProgressInfo info = new CsvWriteProgressInfo();
                    info.CurrentRowCount = WriteProgressSize;
                    info.WirteRowCount   = TotalRowCount;
                    dataHandler.Invoke(info);
                }
            }
        }
        /// <summary>Asynchronously writes the frequency and <see cref="NetworkParametersMatrix"/> the network data of the file.</summary>
        /// <param name="frequency">The frequency of the network data to be written.</param>
        /// /// <param name="matrix">The network data to be written.</param>
        /// <remarks>If <see cref="WriteHeaderAsync"/> has not yet been called, this method will be called automatically before writing any network data.</remarks>
        public async Task WriteDataAsync(double frequency, NetworkParametersMatrix matrix)
        {
            if (!headerWritten)
            {
                await WriteHeaderAsync();
            }

            CancelToken.ThrowIfCancellationRequested();

            if (settings.IncludeColumnNames && !columnsWritten)
            {
                string columns = FormatColumns(matrix.NumPorts);
                await WriteCommentLineAsync(columns);

                columnsWritten = true;

                CancelToken.ThrowIfCancellationRequested();
            }
            string line = FormatEntry(frequency, matrix);
            await Writer.WriteLineAsync(line);
        }
Example #13
0
        protected void ProcessFiles(CommandLineApplication app, bool encrypt)
        {
            ValidateOptions();

            if (!string.IsNullOrEmpty(OutputDirectory) && !Directory.Exists(OutputDirectory))
            {
                Log.Info($"Creating directory : {OutputDirectory}.");
                Directory.CreateDirectory(OutputDirectory);
            }

            Log.Info("Listing files...");

            var filesList = GetFilesList(app).ToList();

            Log.Info($"Processing {filesList.Count} files.");

            var xcode = new UoeEncryptor(EncryptionKey);

            var i          = 0;
            var outputList = new List <string>();

            foreach (var file in filesList)
            {
                CancelToken?.ThrowIfCancellationRequested();
                var outputFile = Path.Combine((string.IsNullOrEmpty(OutputDirectory) ? Path.GetDirectoryName(file) : OutputDirectory) ?? "", $"{Path.GetFileName(file)}{(string.IsNullOrEmpty(OutputFileSuffix) ? "" : OutputFileSuffix)}");
                try {
                    xcode.ConvertFile(file, encrypt, outputFile);
                    outputList.Add($"{file} >> {outputFile}");
                } catch (UoeAlreadyConvertedException) { }
                i++;
                Log.ReportProgress(filesList.Count, i, $"Converting file to : {outputFile}.");
            }

            foreach (var file in outputList)
            {
                Out.WriteResultOnNewLine(file);
            }

            Log.Info($"A total of {outputList.Count} files were converted.");
        }
            public async Task Do()
            {
                profile    = profileManager.Profiles.First(x => x.DisplayName == Settings.ProfileDisplayName);
                scanParams = new ScanParams
                {
                    DetectPatchCodes = Settings.OutputType == BatchOutputType.MultipleFiles && Settings.SaveSeparator == SaveSeparator.PatchT,
                    NoUi             = true,
                    DoOcr            = Settings.OutputType == BatchOutputType.Load ? (bool?)null // Use the default behaviour if we don't know what will be done with the images
                        : GetSavePathExtension().ToLower() == ".pdf" && ocrManager.DefaultParams?.LanguageCode != null,
                    OcrCancelToken = CancelToken
                };

                try
                {
                    CancelToken.ThrowIfCancellationRequested();
                    await Input();
                }
                catch (OperationCanceledException)
                {
                    return;
                }
                catch (Exception)
                {
                    CancelToken.ThrowIfCancellationRequested();
                    // Save at least some data so it isn't lost
                    await Output();

                    throw;
                }

                try
                {
                    CancelToken.ThrowIfCancellationRequested();
                    await Output();
                }
                catch (OperationCanceledException)
                {
                }
            }
Example #15
0
        private async Task ProcessSocketData()
        {
            IsRunning = true;

            // Buffers for received data
            var receivedMessage = new List <byte>();
            var buffer          = new byte[4096];

            while (true)
            {
                try
                {
                    // Get data until the complete message is received
                    WebSocketReceiveResult response;
                    do
                    {
                        response = await _client.ReceiveAsync(new ArraySegment <byte>(buffer), CancelToken);

                        receivedMessage.AddRange(new ArraySegment <byte>(buffer, 0, response.Count));
                    } while (!response.EndOfMessage);

                    // Decode the message
                    var messageJson = (new ASCIIEncoding()).GetString(buffer).Substring(0, receivedMessage.Count);
                    var data        = JsonConvert.DeserializeObject <TResponse>(messageJson);
                    receivedMessage.Clear();

                    // Notify subscriber
                    OnData(data);
                }
                catch (OperationCanceledException) { }

                if (CancelToken.IsCancellationRequested)
                {
                    IsRunning = false;
                    CancelToken.ThrowIfCancellationRequested();
                }
            }
        }
Example #16
0
        public bool Is(out LinkedListNode <Token>[] argNodes, int argCount, Func <LinkedListNode <Token>, LinkedListNode <Token> > argGetNode, Func <Token, bool> argCond)
        {
            var pNodes = new LinkedListNode <Token> [argCount];
            var pNode  = NodeAct;

            for (var i = 0; i < argCount; i++)
            {
                CancelToken.ThrowIfCancellationRequested();

                if (pNode == null || !argCond.Invoke(pNode.Value))
                {
                    argNodes = null;

                    return(false);
                }
                pNodes[i] = pNode;
                pNode     = argGetNode(pNode);
            }

            argNodes = pNodes;

            return(true);
        }
Example #17
0
        /// <summary>
        /// 异步读取, 每读取 readProgressSize 条记录或到文件末尾触发通知事件. 此方法只能调用一次, 如果多次调用会产生异常.
        /// </summary>
        /// <typeparam name="T">数据行转换时对应的实体类型</typeparam>
        /// <param name="dataHandler">数据处理方法</param>
        /// <param name="convertRowData">数据行转换为 T 实例的方法</param>
        /// <param name="cancelToken">取消参数</param>
        /// <param name="readProgressSize">每读取多少行数据触发通知事件, 默认为 1000.</param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException">dataHandler 为空时</exception>
        /// <exception cref="ArgumentNullException">convertRowData 为空时</exception>
        /// <exception cref="ArgumentException">readProgressSize 不大于0时</exception>
        /// <exception cref="OperationCanceledException">cancelToken.IsCancellationRequested 为 true 时</exception>
        public async Task ReadAsync <T>(Action <CsvReadProgressInfo <T> > dataHandler, Func <List <string>, T> convertRowData, int readProgressSize = 1000) where T : new()
        {
            #region Check params

            if (CancelToken != null && CancelToken.IsCancellationRequested)
            {
                CancelToken.ThrowIfCancellationRequested();
            }

            if (dataHandler == null)
            {
                throw new ArgumentNullException(nameof(dataHandler));
            }

            if (convertRowData == null)
            {
                throw new ArgumentNullException(nameof(convertRowData));
            }

            if (readProgressSize <= 0)
            {
                throw new ArgumentException($"The property {nameof( readProgressSize )} must be greater than 0");
            }

            #endregion

            //读取数据缓冲
            CharBuffer buffer = new CharBuffer(readBufferLength);
            //标题行
            List <string> columnNames = new List <string>();
            //完整的行
            List <List <string> > rows = new List <List <string> >();
            //用于临时存放不足一行的数据
            List <char> subLine = new List <char>();

            //获得数据流总字节数
            long totalBytes = csvStream.BaseStream.Length;
            //当前读取字节数
            long currentBytes = 0;

            //开始循环读取数据
            while (!csvStream.EndOfStream)
            {
                if (CancelToken.IsCancellationRequested)
                {
                    CancelToken.ThrowIfCancellationRequested();
                }

                //读取一块数据
                int count = await csvStream.ReadAsync(buffer.Buffer, buffer.EndIndex, buffer.Buffer.Length - buffer.EndIndex);

                buffer.Length     = buffer.Length + count;
                buffer.StartIndex = 0;
                currentBytes      = csvStream.BaseStream.Position;
                GetRows(ref buffer, ref rows);
                Progress(currentBytes, totalBytes, dataHandler, convertRowData, readProgressSize, rows, ref columnNames);

                //如果到了文件流末尾且还有未处理的字符,添加末尾换行符处理余下字符.
                if (csvStream.EndOfStream && buffer.EndIndex > 0)
                {
                    //在末尾添加换行符
                    buffer.Buffer.SetValue('\r', buffer.EndIndex);
                    buffer.Buffer.SetValue('\n', buffer.EndIndex + 1);
                    buffer.Length = buffer.Length + 2;
                    GetRows(ref buffer, ref rows);
                    Progress(currentBytes, totalBytes, dataHandler, convertRowData, readProgressSize, rows, ref columnNames);

                    //如果还有剩余字符,说明格式错误
                    if (buffer.Length > 0)
                    {
                        throw new Exception("The csv file format error!");
                    }

                    //为下一块数据使用准备
                    subLine.Clear();
                }
            }

            //资料有不完整的行,或者读取错位导致剩余。
            if (buffer.Length > 0)
            {
                throw new Exception("The csv file format error!");
            }
        }