Beispiel #1
0
        /// <summary>
        /// 异步写入csv
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        private async Task WriteData(string fileName)
        {
            if (this.modelCsvData != null)
            {
                Stopwatch      sc  = null;
                CsvWriteHelper csv = null;

                try
                {
                    CsvFlag flag = new CsvFlag(Convert.ToChar(this.cob_separator.Text), Convert.ToChar(this.cob_FieldEnclosed.Text));
                    Progress <CsvWriteProgressInfo> p = new Progress <CsvWriteProgressInfo>(r =>
                    {
                        this.sync.Post(t =>
                        {
                            double val             = (t as CsvWriteProgressInfo).WirteRowCount / (double)(this.modelCsvData.Count + (this.cob_FirstIsHead.SelectedIndex == 0 ? 1 : 0));
                            this.pb_Progress.Value = Convert.ToInt32(val * 100);
                        }, r);
                    });

                    csv = new CsvWriteHelper(fileName, Encoding.UTF8, flag, cancelSource.Token, p, 1000);

                    sc = Stopwatch.StartNew();

                    //因为 ui 线程同步执行 WriteLineAsync 中的部分代码, 所以用 Task.Run 在其它线程中执行, 避免 ui 阻塞.
                    await Task.Run(async() =>
                    {
                        if (columnsName != null)
                        {
                            await csv.WriteLineAsync(columnsName);
                        }

                        await csv.WriteAsync(this.modelCsvData, f =>
                        {
                            return(ConvertModelToRowData(f));
                        });

                        await csv.FlushAsync();
                    }, this.cancelSource.Token);
                }
                finally
                {
                    if (csv != null)
                    {
                        csv.Close();
                    }

                    if (sc != null)
                    {
                        sc.Stop();
                    }

                    this.sync.Post(k =>
                    {
                        this.tb_Times.Text = k.ToString();
                    }, sc.Elapsed.TotalSeconds.ToString());
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// 异步写入csv
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        private async Task WriteData(string fileName)
        {
            if (ModelCsvData != null)
            {
                Stopwatch      sc  = null;
                CsvWriteHelper csv = null;

                try
                {
                    sc = Stopwatch.StartNew();

                    CsvFlag flag = new CsvFlag(Convert.ToChar(cob_separator.Text), Convert.ToChar(cob_FieldEnclosed.Text));
                    csv = new CsvWriteHelper(fileName, Encoding.UTF8, flag, f =>
                    {
                        Sync.Post(t =>
                        {
                            double val        = (t as CsvWriteProgressInfo).WirteRowCount / (double)(ModelCsvData.Count + (cob_FirstIsHead.SelectedIndex == 0 ? 1 : 0));
                            pb_Progress.Value = Convert.ToInt32(val * 100);
                        }, f);
                    }, CancelSource.Token, 1000);

                    if (ColumnsName != null)
                    {
                        await csv.WriteLineAsync(ColumnsName);
                    }

                    await csv.WriteAsync(ModelCsvData, f => ConvertModelToRowData(f));

                    await csv.FlushAsync();
                }
                finally
                {
                    csv?.Close();
                    sc?.Stop();
                    Sync.Post(k =>
                    {
                        tb_Times.Text = k.ToString();
                    }, sc.Elapsed.TotalSeconds.ToString());
                }
            }
        }