[英]DataGridView and Multi-Threading
I'm trying to convert my foreach
method to multi-threading. 我正在尝试将我的foreach
方法转换为多线程。
I have a datagridview
and the method gets the value from cell[0]
(which contains a url) and sends it to another method which works with httpwebrequest
. 我有一个datagridview
,该方法从cell[0]
(包含一个url)获取值,并将其发送到与httpwebrequest
一起使用的另一个方法。
public void UrlCheck()
{
foreach (DataGridViewRow row in dataUrlList.Rows)
{
string url= row.Cells[0].Value.ToString();
try
{
string get = getHtml(url);
//work with get string removed
if()
{
row.Cells[1].Value = "page info here";
}
else
{
row.Cells[1].Value = "error info here";
}
}
catch
{
}
}
MessageBox.Show("Done.");
}
The above code is working without any problem but sequentially. 上面的代码可以正常工作,但顺序正常。
Then with this i've tried to convert this code to that to be multithreaded : 然后,我尝试将这段代码转换为多线程代码:
Button : 按键:
private void button9_Click(object sender, EventArgs e)
{
int threadcount = Convert.ToInt32(numThreadSearch.Value);
ThreadForSearch = new Thread[threadcount];
checkingForSearch = dataUrlList.Rows.Count;
isRunningForSearch = true;
beenCheckedForSearch = 1;
for (int i = 0; i <= threadcount - 1; i++)
{
ThreadForSearch[i] = new Thread(new ParameterizedThreadStart(MultiThreadMet));
ThreadForSearch[i].Start(i);
}
}
and the multi-threaded method is here : 和多线程方法在这里:
public void MultiThreadMet (object IndexForSearch)
{
int index = (int)IndexForSearch;
DataGridViewRow row = dataUrlList.Rows[index];
while (isRunningForSearch)
{
try
{
if (beenCheckedForSearch >= checkingForSearch)
{
isRunningForSearch = false;
}
if (index >= dataUrlList.Rows.Count)
{
ThreadForSearch[index].Abort();
}
//For just test i'm trying to add "test" in every cell[1] in datagridview
dataUrlList.Invoke(new Action(() => row.Cells[1].Value = "test"));
beenCheckedForSearch++;
}
catch
{
}
}
ThreadForSearch[1].Abort();
}
It gets the thread count to run from a numericUpDown control and if I choose the value 10 from the numericUpDown it puts "test" text in the first 10 cells of datagridview and then stops, if i choose 4(or 2,5,7), then it puts the "test" text in the first 4(2,5,7) cells and stops. 它获取线程计数以从numericUpDown控件运行,如果我从numericUpDown中选择值10,则会在datagridview的前10个单元格中放置“测试”文本,然后停止,如果我选择4(或2,5,7) ,然后将“测试”文本放在前4(2,5,7)个单元格中并停止。
It doesn't continue to the next rows after the thread is finished. 线程完成后,它不会继续到下一行。 So I'm trying to fire 5 threads (I always choose it from numericUpDown) and when a thread finish its work, it must go to next row.. How can i solve this problem ? 因此,我尝试触发5个线程(我总是从numericUpDown中选择它),并且当一个线程完成其工作时,它必须转到下一行。我该如何解决此问题?
Also these variables have been declared: 还声明了这些变量:
Thread[] ThreadForSearch;
int beenCheckedForSearch;
int checkingForSearch;
private bool isRunningForSearch;
from output screen i'm getting 从输出屏幕我得到
Exception thrown: 'System.Threading.ThreadAbortException' in mscorlib.dll
Thanks. 谢谢。
The index counter was not incremented in the thread method. 在线程方法中, 索引计数器未增加。
There is still an overlapping problem in the program that I will let you fix. 我会让您解决该程序中仍然存在的重叠问题。 Assuming there are 50 urls, the problem is that thread 1 will update cells 0 to 49, thread 2 will update cells 1 to 49, and so on. 假设有50个url,问题在于线程1将更新单元格0到49,线程2将更新单元格1到49,依此类推。
public void MultiThreadMet (object IndexForSearch)
{
int index = (int)IndexForSearch;
while (isRunningForSearch)
{
try
{
if (index >= dataUrlList.Rows.Count)
{
return;
}
DataGridViewRow row = dataUrlList.Rows[index];
index++;
//For just test i'm trying to add "test" in every cell[1] in datagridview
dataUrlList.Invoke(new Action(() => row.Cells[1].Value = "test"));
}
catch
{
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.