Reputation: 187
UPDATE: I have improved some things and changed it. Now I have to click a second time, the first time the console writes:
Der Thread 0x180c hat mit Code 0 (0x0) geendet.(Thread stopped)
And on the second checking checkbox it works.
OLD When I click on the checkbox my app should visit URL's. It is working but only if I click multiple times on the checkbox.
So when I want to stop it, it will not work. I have no clue why. It is the same with a button, I have to click it multiple times till it works.
private void chVisitProfile_CheckedChanged(object sender, EventArgs e)
{
if (chVisitProfile.Checked)
{
var threadLoading = new ThreadStart(StartVisiting);
var thread = new Thread(threadLoading);
thread.Start();
}
}
private void StartVisiting()
{
var jsDo = "var urls = '';" +
"$.each($('.tile__link').find('.info__username'), function(i, b){var profilname = '" + "https://www.website.com/#/profile/" + "' +$(this).text().trim() + '" + "@" + "';urls+=profilname;});" +
"function returnURL(){thURL = urls;return thURL;} returnURL();";
var task = chromeControll.EvaluateScriptAsync(jsDo);
task.ContinueWith(t =>
{
if (!t.IsFaulted)
{
var response = t.Result;
var EvaluateJavaScriptResult = response.Success ? (response.Result ?? "null") : response.Message;
richTextBox1.BeginInvoke((Action)(() => richTextBox1.Text = response.Result.ToString()));
}
});
var links = richTextBox1.Text;
string[] exploded = links.Split('@');
for (int i = 1; i < exploded.Length; i++)
{
if(chVisitProfile.Checked == false)
{
break;
}
Thread.Sleep(500);
chromeVisit.Load(exploded[i]);
//lblSeconds.Text = i.ToString();
}
}
Upvotes: 0
Views: 82
Reputation: 11
Why even start the thread if it is not needed? I would suggest something like this:
private void chVisitProfile_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
var threadLoading = new ThreadStart(StartVisiting);
var thread = new Thread(threadLoading);
thread.Start();
}
}
This way the thread fires only as needed. To improve it even more, it can verify if the process is already going (so no need to start a new thread).
Upvotes: 1