即时通讯使用后台工作程序将2个sqlite数据库合并为1但我想添加一个进度条,以便用户知道完成后台工作程序的工作正常但是当我添加代码来报告进度时,进度条没有移动任何帮助非常感谢背景工作者代码
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;
}
答案 0 :(得分:2)
我看不到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();
}
答案 1 :(得分:1)
您应该在backgroundWorker1_RunWorkerCompleted
事件中处理异常。
来自BackgroundWorker.DoWork Event
如果操作引发了代码无法处理的异常, BackgroundWorker捕获异常并将其传递给 RunWorkerCompleted事件处理程序,它作为错误公开 System.ComponentModel.RunWorkerCompletedEventArgs的属性。如果你 在Visual Studio调试器下运行,调试器将中断 在DoWork事件处理程序中未处理的异常的位置 被提出来了。
您还应该查看RunWorkerCompletedEventArgs Class
提供的示例是
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
// First, handle the case where an exception was thrown.
if (e.Error != null)
{
MessageBox.Show(e.Error.Message);
}
else if (e.Cancelled)
{
// Next, handle the case where the user canceled
// the operation.
// Note that due to a race condition in
// the DoWork event handler, the Cancelled
// flag may not have been set, even though
// CancelAsync was called.
resultLabel.Text = "Canceled";
}
else
{
// Finally, handle the case where the operation
// succeeded.
resultLabel.Text = e.Result.ToString();
}
}
答案 2 :(得分:1)
确保BackgroundWorker的WorkerReportsProgress
设置为true
。