gpt4 book ai didi

c# - 设置后台 worker 的正确工作

转载 作者:太空宇宙 更新时间:2023-11-03 12:07:52 25 4
gpt4 key购买 nike

我在设置进度条中正确显示后台工作人员时遇到问题。

也就是说,我想加载到数据网格中,而 ecel 加载到我的进度条中。这是我当前的代码,它可以工作,但首先我得到 excel,然后是 backgroundworker,如何将它设置为并行完成?

      private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
int process = ((DataParameter)e.Argument).Process;
int delay = ((DataParameter)e.Argument).Delay;
int index = 1;

try
{
//for (int i = 0; i < process; i++)
//{
//if (!backgroundWorker.CancellationPending)
//{
// backgroundWorker.ReportProgress(index++ * 100 / process, string.Format("Process data {0}", i));
// Thread.Sleep(delay);
//}
// }

}
catch (Exception ex)
{
backgroundWorker.CancelAsync();
MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}

private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progress.Value = e.ProgressPercentage;
lblPercent.Text = string.Format("Processing...{0}%", e.ProgressPercentage);
progress.Update();
}

private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
MessageBox.Show("Process has been completed.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
}

private void buttonBrowse_Click(object sender, EventArgs e)
{
OpenFileDialog fd = new OpenFileDialog();
fd.Filter = "File Excel|*.xlsx";
DialogResult re = fd.ShowDialog();
excel_name = fd.SafeFileName;
if (re == DialogResult.OK)
{
string fileName = fd.FileName;

if (!backgroundWorker.IsBusy)
{
inputParameter.Delay = 100;
inputParameter.Process = 200;
backgroundWorker.RunWorkerAsync(inputParameter);
ReadExcel(fileName);
}
}
}

public void ReadExcel(string Path)
{
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
Excel.Range range;

int rCnt;
int cCnt;
int rw = 0;
int cl = 0;

xlApp = new Excel.Application();
xlWorkBook = xlApp.Workbooks.Open(Path, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

range = xlWorkSheet.UsedRange;
rw = range.Rows.Count;
cl = range.Columns.Count;
int tenPercent = rw / 10;
int percentCounter = 0;
for (rCnt = 1; rCnt <= rw; rCnt++)
{
for (cCnt = 0; cCnt <= cl; cCnt++)
{
no = Convert.ToString((range.Cells[rCnt, 1] as Excel.Range).Value);
name = Convert.ToString((range.Cells[rCnt, 2] as Excel.Range).Value);
time_to = Convert.ToString((range.Cells[rCnt, 3] as Excel.Range).Value);
}
if (++percentCounter == tenPercent)
{
double progress = rCnt / (double)rw;
lblPercent.Text = string.Format("Processing...{0}%", 100 * progress);
percentCounter = 0;
}
DateTime dt = DateTime.Parse(no);
string date = dt.ToString("yyyy'/'MM'/'dd");

double d = double.Parse(name);
DateTime conv = DateTime.FromOADate(d);
string time = conv.ToString("HH:mm:ss");

double f = double.Parse(time_to);
DateTime conv2 = DateTime.FromOADate(f);
string time2 = conv2.ToString("HH:mm:ss");

epg_id = ((range.Cells[rCnt, 4] as Excel.Range).Value);


SQLiteConnection sqliteCon = new SQLiteConnection(dbConnectionString);
sqliteCon.Open();
string selectQuery = "SELECT * FROM EPGdaten WHERE File=@File";
command = new SQLiteCommand(selectQuery, sqliteCon);
command.Parameters.Add(new SQLiteParameter("@File", epg_id));
sqdr = command.ExecuteReader();
if (sqdr.Read())
{
string s = sqdr.GetValue(1).ToString();
string b = sqdr.GetValue(3).ToString();
dataGridView1.Rows.Add(date, time, time2, s, b);
}
else
{
dataGridView1.Rows.Add(date, time, time2);
}


sqdr.Close();
sqliteCon.Close();
sqliteCon.Dispose();
}
xlWorkBook.Close(true, null, null);
xlApp.Quit();

Marshal.ReleaseComObject(xlWorkSheet);
Marshal.ReleaseComObject(xlWorkBook);
Marshal.ReleaseComObject(xlApp);
}

我是初学者,看了很多例子,第一次做这个一直没搞定..

谢谢大家的帮助

最佳答案

将这样的代码放在您的 ReadExcel 方法中

            int rw = 967;
int tenPercent = rw / 10;
int percentCounter = 0;
for (int rCnt = 1; rCnt <= rw; rCnt++)
{

if( ++percentCounter == tenPercent )
{
double progress = rCnt / (double)rw;
Console.WriteLine("Progress : {0}%", 100 * progress);
percentCounter = 0;
}
}
Console.ReadLine();

关于c# - 设置后台 worker 的正确工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53987997/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com