gpt4 book ai didi

c# - 进度条不适用于后台工作人员

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

我正在使用后台 worker 将 2 个 sqlite 数据库合并为 1 个数据库,但我想添加一个进度条,以便用户知道它何时完成后台 worker 工作正常但是当我添加代码以报告进度时,进度条没有任何帮助非常感谢这里的后台工作人员代码

    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
for (int i = 0; i < 100; i++)
{
string str2 = "";
if (saveFileDialog1.FileName != null)
{
str2 = saveFileDialog1.FileName;
if (!File.Exists(str2))
{
SQLiteConnection.CreateFile(saveFileDialog1.FileName);
}
}

SQLiteConnection connection = new SQLiteConnection("Data Source=" + saveFileDialog1.FileName);
connection.Open();

SQLiteCommand newcdbdatas = DatabaseHelper.CreateCommand("create table datas (id integer, ot integer, alias integer, setcode integer, type integer, atk integer, def integer, level integer, race integer, attribute integer, category integer)", connection);
SQLiteCommand newcdbtexts = DatabaseHelper.CreateCommand("create table texts (id integer, name varchar(128), desc varchar(1024), str1 varchar(256), str2 varchar(256), str3 varchar(256), str4 varchar(256), str5 varchar(256), str6 varchar(256), str7 varchar(256), str8 varchar(256), str9 varchar(256), str10 varchar(256), str11 varchar(256), str12 varchar(256), str13 varchar(256), str14 varchar(256), str15 varchar(256), str16 varchar(256), str17 varchar(256), str18 varchar(256), str19 varchar(256))", connection);
DatabaseHelper.ExecuteNonCommand(newcdbdatas);
DatabaseHelper.ExecuteNonCommand(newcdbtexts);

SQLiteCommand command = null;
foreach (int id in Program.CardData1.Keys)
{
int cardid = Program.CardData1[id].Id;
int ot = Program.CardData1[id].Ot;
int cardalias = Program.CardData1[id].AliasId;
int atk = Program.CardData1[id].Atk;
int def = Program.CardData1[id].Def;

command = DatabaseHelper.CreateCommand("INSERT INTO datas (id,ot,alias,setcode,type,atk,def,level,race,attribute,category)" +
" VALUES (@id, @ot, @alias, @setcode, @type, @atk, @def, @level, @race, @attribute, @category)", connection);

command.Parameters.Add(new SQLiteParameter("@id", cardid));
command.Parameters.Add(new SQLiteParameter("@ot", ot));
command.Parameters.Add(new SQLiteParameter("@alias", cardalias));
command.Parameters.Add(new SQLiteParameter("@setcode", Program.CardData1[id].SetCode));
command.Parameters.Add(new SQLiteParameter("@type", Program.CardData1[id].Type));
command.Parameters.Add(new SQLiteParameter("@atk", atk));
command.Parameters.Add(new SQLiteParameter("@def", def));
command.Parameters.Add(new SQLiteParameter("@level", Program.CardData1[id].Level));
command.Parameters.Add(new SQLiteParameter("@race", Program.CardData1[id].Race));
command.Parameters.Add(new SQLiteParameter("@attribute", Program.CardData1[id].Attribute));
command.Parameters.Add(new SQLiteParameter("@category", Program.CardData1[id].Category));
DatabaseHelper.ExecuteNonCommand(command);
command = DatabaseHelper.CreateCommand("INSERT INTO texts (id,name,desc)" +
" VALUES (@id,@name,@des)", connection);
command.Parameters.Add(new SQLiteParameter("@id", cardid));
command.Parameters.Add(new SQLiteParameter("@name", Program.CardData1[id].Name));
command.Parameters.Add(new SQLiteParameter("@des", Program.CardData1[id].Description));
DatabaseHelper.ExecuteNonCommand(command);
}
connection.Close();
backgroundWorker1.ReportProgress(i);
}
}

private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{

}

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
}

最佳答案

我看不到 for 循环的使用,每次循环中唯一改变的值是 i 变量,并且唯一使用 i 是报告进度。所以我认为您实际上不需要那个 for 循环。尝试使用 progressPercentage 报告进度,如以下示例所示:

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
string str2 = "";
if (saveFileDialog1.FileName != null)
{
str2 = saveFileDialog1.FileName;
if (!File.Exists(str2))
{
SQLiteConnection.CreateFile(saveFileDialog1.FileName);
}
}

SQLiteConnection connection = new SQLiteConnection("Data Source=" + saveFileDialog1.FileName);
connection.Open();

SQLiteCommand newcdbdatas = DatabaseHelper.CreateCommand("create table datas (id integer, ot integer, alias integer, setcode integer, type integer, atk integer, def integer, level integer, race integer, attribute integer, category integer)", connection);
SQLiteCommand newcdbtexts = DatabaseHelper.CreateCommand("create table texts (id integer, name varchar(128), desc varchar(1024), str1 varchar(256), str2 varchar(256), str3 varchar(256), str4 varchar(256), str5 varchar(256), str6 varchar(256), str7 varchar(256), str8 varchar(256), str9 varchar(256), str10 varchar(256), str11 varchar(256), str12 varchar(256), str13 varchar(256), str14 varchar(256), str15 varchar(256), str16 varchar(256), str17 varchar(256), str18 varchar(256), str19 varchar(256))", connection);
DatabaseHelper.ExecuteNonCommand(newcdbdatas);
DatabaseHelper.ExecuteNonCommand(newcdbtexts);

SQLiteCommand command = null;

///////I added somthing here//////////
var progressCounter = 0;
var max = Program.CardData1.Keys.Count;
///////////////////////////////////////

foreach (int id in Program.CardData1.Keys)
{
int cardid = Program.CardData1[id].Id;
int ot = Program.CardData1[id].Ot;
int cardalias = Program.CardData1[id].AliasId;
int atk = Program.CardData1[id].Atk;
int def = Program.CardData1[id].Def;

command = DatabaseHelper.CreateCommand("INSERT INTO datas (id,ot,alias,setcode,type,atk,def,level,race,attribute,category)" +
" VALUES (@id, @ot, @alias, @setcode, @type, @atk, @def, @level, @race, @attribute, @category)", connection);

command.Parameters.Add(new SQLiteParameter("@id", cardid));
command.Parameters.Add(new SQLiteParameter("@ot", ot));
command.Parameters.Add(new SQLiteParameter("@alias", cardalias));
command.Parameters.Add(new SQLiteParameter("@setcode", Program.CardData1[id].SetCode));
command.Parameters.Add(new SQLiteParameter("@type", Program.CardData1[id].Type));
command.Parameters.Add(new SQLiteParameter("@atk", atk));
command.Parameters.Add(new SQLiteParameter("@def", def));
command.Parameters.Add(new SQLiteParameter("@level", Program.CardData1[id].Level));
command.Parameters.Add(new SQLiteParameter("@race", Program.CardData1[id].Race));
command.Parameters.Add(new SQLiteParameter("@attribute", Program.CardData1[id].Attribute));
command.Parameters.Add(new SQLiteParameter("@category", Program.CardData1[id].Category));
DatabaseHelper.ExecuteNonCommand(command);
command = DatabaseHelper.CreateCommand("INSERT INTO texts (id,name,desc)" +
" VALUES (@id,@name,@des)", connection);
command.Parameters.Add(new SQLiteParameter("@id", cardid));
command.Parameters.Add(new SQLiteParameter("@name", Program.CardData1[id].Name));
command.Parameters.Add(new SQLiteParameter("@des", Program.CardData1[id].Description));
DatabaseHelper.ExecuteNonCommand(command);

///////I added somthing here//////////
progressCounter++;
var progressPercentage = progressCounter*100/max;
backgroundWorker1.ReportProgress(progressPercentage);
///////////////////////////////////////
}
connection.Close();
}

关于c# - 进度条不适用于后台工作人员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20833719/

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