Hello,
I write a program that shall make an unattended SQL servers installation. There is a main form and many methods which I start in a top-down manner. It looks like that: collecting data and checking them, SQL servers install, install scripts, install service pack, install scripts etc. I would like to inform the user about the progress of the program (e.g. with progress bar). First I tried to work with
BackgroundWorker
, it works well but the Worker doesn’t wait (or should I say: I don’t know how to make it wait) for the previous method to be finished – what I actually need (there is no sense in installing Service Pack while the SQL Server is still not installed. I have found a proposal for solution of Jon Skeet site (
http://www.yoda.arachsys.com/csharp/threads/winforms.shtml[
^].). And used it in that way (here just one method: copy some data from drive A to drive B, after copying I should start SQL-Server installation, Service Pack etc.):
private void button1_Click(object sender, EventArgs e)
{
SQLInstallation inst = new SQLInstallation();
inst.LWCopy(ref myAl, "C:\\ABC", "C:\\BDC");
pBar1.Visible = true;
pBar1.Minimum = 0;
pBar1.Maximum = myAl.Count;
pBar1.Value = 1;
pBar1.Step = 1;
lock (stateLock)
{
target = myAl.Count;
}
Thread t = new Thread(new ThreadStart(ThreadJob));
t.IsBackground = true;
t.Start();
}
void ThreadJob()
{
Object[] jajco = myAl.ToArray();
MethodInvoker updateCounterDelegate = new MethodInvoker(UpdateCount);
int localTarget;
lock (stateLock)
{
localTarget = target;
}
UpdateStatus("Installationsdateien kopieren.");
lock (stateLock)
{
currentCount = 0;
}
Invoke(updateCounterDelegate);
Thread.Sleep(50);
UpdateStatus("Counting");
for (int i = 0; i < localTarget; i++)
{
lock (stateLock)
{
string input = jajco[i].ToString().Substring(0, jajco[i].ToString().IndexOf(","));
string target = jajco[i].ToString().Substring(jajco[i].ToString().IndexOf(",") + 1);
File.Copy(input, target, true);
currentCount = i;
strAnzeige = target;
}
Invoke(updateCounterDelegate);
Thread.Sleep(50);
}
UpdateStatus("Finished");
myAl = null;
jajco = null;
}
void UpdateStatus(string value)
{
if (InvokeRequired)
{
BeginInvoke(new StringParameterDelegate(UpdateStatus), new object[] { value });
return;
}
lblStatus.Text = value;
}
void UpdateCount()
{
int tmpCount;
lock (stateLock)
{
tmpCount = currentCount;
}
pBar1.Value = tmpCount;
lblStatus.Text = "Coping..: " + strAnzeige;
}
Is this the solution or shall try to solve the problem writing a queue?
A help would be appreciate...
Thanks
Andy