Parallel.ForEachについてとても混乱しています。
Parallel.ForEachを使用して以下のコードを書き直すのを手伝っていただけませんか。
#region Register
private void btnStartRegister_Click(object sender, EventArgs e)
{
if (backgroundWorker2.IsBusy)
{
btnStartRegister.Enabled = false;
lblStatusOfaccMValue.Text = "Canceling...";
backgroundWorker2.CancelAsync();
}
else
{
txtLogInRegister.Text = string.Empty;
btnStartRegister.Text = "Cancel";
lblUserCreatedCountValue.Text = "---";
lblStatusOfaccMValue.Text = "Running...";
backgroundWorker2.RunWorkerAsync();
}
}
private void backgroundWorker2_DoWork(object sender, DoWorkEventArgs e)
{
backgroundWorker2.ReportProgress(Convert.ToInt32(0));
int UsersCount = int.Parse(txtUsersCount.Text);
UsersCreatedCount_Step = 0;
string path = Application.StartupPath;
string accfilePath = path + @"\acc.txt";
string logfilePath = path + @"\log.txt";
string Ok_ip_port = string.Empty;
string[] Separator = new string[] { "__" };
int list_lines_acc_Current = -1;
string[] lines_acc = File.ReadAllLines(accfilePath);
List<string> list_lines_acc = new List<string>(lines_acc);
List<string> list_lines_acc_new = new List<string>(list_lines_acc);
foreach (string line_acc in list_lines_acc)
{
if (backgroundWorker2.CancellationPending)
{
e.Cancel = true;
break;
}
list_lines_acc_Current++;
string[] line_acc_ar = line_acc.Split(Separator, StringSplitOptions.None);
if (line_acc_ar.Length == 3)
{
string username = line_acc_ar[0];
string password = line_acc_ar[1];
string email = line_acc_ar[2];
string[] email_ar = email.Split('@');
email = email_ar[0] + "%40" + email_ar[1];
list_lines_acc_new[list_lines_acc_Current] += "__" + txtSuffixURL.Text + "__" + txtServerNumber.Text + "__" + "127.0.0.1:2222";
try
{
bool[] RegisterMethod_res = RegisterMethod(bla bla bla);
bool failed = RegisterMethod_res[0];
bool world_full = RegisterMethod_res[1];
if (!failed)
{
UsersCreatedCount_Step++;
backgroundWorker2.ReportProgress(Convert.ToInt32(UsersCreatedCount_Step * (100.0 / UsersCount)));
list_lines_acc_new[list_lines_acc_Current] += "__" + "Y";
}
else
{
if (world_full)
{
list_lines_acc_new[list_lines_acc_Current] += "__" + "N";
e.Cancel = true;
break;
}
else
{
list_lines_acc_new[list_lines_acc_Current] += "__" + "N";
}
}
File.WriteAllLines(accfilePath, list_lines_acc_new.ToArray());
}
catch (Exception ex)
{
list_lines_acc_new[list_lines_acc_Current] += "__" + "N";
File.WriteAllLines(accfilePath, list_lines_acc_new.ToArray());
}
if (UsersCount == UsersCreatedCount_Step)
{
break;
}
}
}
File.WriteAllLines(accfilePath, list_lines_acc_new.ToArray());
}
private bool[] RegisterMethod()
{
bla bla bla
...
...
return RegisterMethod_res;
}
private void backgroundWorker2_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar2.Value = e.ProgressPercentage;
lblUserCreatedCountValue.Text = UsersCreatedCount_Step.ToString();
}
private void backgroundWorker2_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
btnStartRegister.Text = "Start Register";
btnStartRegister.Enabled = true;
if (e.Error != null)
{
MessageBox.Show(e.Error.Message);
return;
}
if (e.Cancelled)
{
lblStatusOfaccMValue.Text = "Cancelled...";
}
else
{
lblStatusOfaccMValue.Text = "Completed";
}
}
#endregion
foreach (string line_acc in list_lines_acc)
行をに置き換えたいParallel.ForEach
。
私はそれを行うことができませんでした...
あなたが見るように、書き込み用のaccファイル(acc.txt )と読み取りと書き込み用のいくつかのリスト(list_lines_acc + list_lines_acc_new )があります。
パラレルモードでは常に問題があります。
編集:
問題はこれらの行についてです->
if (UsersCount == UsersCreatedCount_Step)
{
break;
}
その状態で止めたい。しかし、パラレルモードで
どのように機能するのかわかりませんか?UsersCreatedCount_Step
助けていただければ幸いです。
前もって感謝します