I developed an application that stores several connection strings. I just loop through the for loop and concatenate each db and execute sql against each db. this way I am updating several databases using a massive sql statement.
Now I need to use the parallel task library to update several db at the same time instead of updating one at a time in one cycle. I also want to handle the exception, and also want to show several progress indicators for several database operations. pause and resume work should be there.
when I click on the button, then several db connections will open, and a new progress bar will be added for each task in my form. each progress bar shows each progress of the db operation. when any task is completed, the corresponding progress bar will be removed from the form.
anyone can help me with code example how to do this using TPL. here I have one code that updates one progress indicator, but I need to update several progress indicators. int iterations = 100;
ProgressBar pb = new ProgressBar(); pb.Maximum = iterations; pb.Dock = DockStyle.Fill; Controls.Add(pb); Task.Create(delegate { Parallel.For(0, iterations, i => { Thread.SpinWait(50000000);
UPDATE Question
I did it this way. the code works, but all the values โโof the execution line increase sequentially. I have one form and one user control in winform applications. please look at my code and tell me what is wrong there.
main for code
public partial class Main : Form { public Main() { InitializeComponent(); this.DoubleBuffered = true; } private void btnStart_Click(object sender, EventArgs e) { Progress ucProgress = null; Dictionary<string, string> dicList = new Dictionary<string, string>(); dicList.Add("GB", "conn1"); dicList.Add("US", "conn2"); dicList.Add("DE", "conn3"); fpPanel.Controls.Clear(); Task.Factory.StartNew(() => { foreach (KeyValuePair<string, string> entry in dicList) { ucProgress = new Progress(); ucProgress.Country = entry.Key; ucProgress.DBConnection = entry.Value; fpPanel.BeginInvoke((MethodInvoker)delegate { fpPanel.Controls.Add(ucProgress); ucProgress.Process(); });
user control code
public partial class Progress : UserControl { public Progress() { InitializeComponent(); lblMsg.Text = ""; pbStatus.Minimum = 0; pbStatus.Maximum = 100; } public string Country { get; set; } public string DBConnection { get; set; } public string Sql { get; set; } public void SetMessage(string strMsg) { lblMsg.Text = strMsg; } public void Process() { var uiScheduler = TaskScheduler.FromCurrentSynchronizationContext(); Task.Factory.StartNew(() => { lblMsg.BeginInvoke((MethodInvoker)delegate { lblMsg.Text = "Connecting country " + Country; }); pbStatus.BeginInvoke((MethodInvoker)delegate { pbStatus.Value = 30; }); System.Threading.Thread.SpinWait(50000000);