如何在C#中DataTable.Rows.Count n对象的基础? [英] How to create n Object base on DataTable.Rows.Count in C#?
问题描述
下面只有所有的代码创建两个进程。虽然我需要创建n个进程打开Chrome浏览器,并运行 OpenNRowsInData(用户,PWD)
根据 dtUser.Rows.Count $ C $ 。C>,因为这是动态的。
和我不知道为什么 OpenNRowsInData(用户,PWD);
总是只得到第一个行。
我有1-7行的数据表
系列。
通常情况下,我通常使用的是创建两个对象。
静态myObject的[]浏览器=新myObject的[] {
新myObject的(Browsers.Chrome,0),
新myObject的(Browsers.Chrome,0)
};
线程T1,T2;
T1 =新主题(新的ThreadStart(AllCase))
{
NAME =线程1
};
t1.Start();
T2 =新主题(新的ThreadStart(AllCase))
{
NAME =线程2
};
t2.Start();
在类 AllCase
:
静态INT [] stepRun = {0,0};
私人无效AllCase()
{
INT IDX = int.Parse(Thread.CurrentThread.Name.Replace(线程,)) - 1;
开关(stepRun [IDX])
{
的情况下0:
的foreach(在dtUser.Rows的DataRow行)
{
用户=行[用户]的ToString()。
PWD =行[PWD]的ToString();
OpenNRowsInData(用户,PWD);
}
中断;
案例1:
ClickBuy();
中断;
}
}
和在另一个,使用并行处理我之前。目前,我不想使用它。
Parallel.ForEach(
dtUser.AsEnumerable(),
项= GT; OpenNRowsInData(项目[用户]的ToString(),项目[PWD]的ToString()));
我想只列用户
和 PWD
。
使用代码试了,我试着在添加参数动作()
的方法,如:动作(字符串用户,字符串PWD)
公共静态无效的ForEach< T>(这个IEnumerable的< T>源,动作< T>动作)
{
如果(来源== NULL)抛出新的ArgumentNullException(源 );
如果(动作== NULL)抛出新的ArgumentNullException(行动);
的foreach(源牛逼的项目)
{
动作(项目);
}
}
更新:
添加类 OpenNRowsInData(用户名字符串,字符串密码)
:
私人无效OpenNRowsInData(用户名字符串,字符串密码)
{
INT IDX = int.Parse(Thread.CurrentThread.Name.Replace(主题, )) - 1;
试
{
浏览器[IDX] .DeleteAllCookies();
浏览器[IDX] .GoToUrl(链接);
浏览器[IDX] .waittingID(txtUserName,15);
浏览器[IDX] .FindElementById(txtUserName)的SendKeys(用户名)。
浏览器[IDX] .FindElementById(txtpassword)的SendKeys(密码)。
浏览器[IDX] .FindElementById(btnlogin)点击();
stepRun [IDX] = 1;
AllCase();
}
}
和我不知道为什么OpenNRowsInData(用户,PWD);总是只得到第一行。
块引用>
这似乎对我来说,你的第二个威胁是太快了。之前,首先可以完成,将执行
ClickBuy();
所以只有第一行可以加工
尝试做到这一步其他
AllCase后(0,0);
AllCase(1,0);
私人无效AllCase(INT情况下,诠释browserNr)
{
开关(情况)
{
的情况下0:
的foreach(DataRow的行dtUser.Rows)
{
用户=行[用户]的ToString()。
PWD =行[PWD]的ToString();
OpenNRowsInData(用户,PWD,browserNr);
}
中断;
案例1:
ClickBuy();
中断;
}
}
私人无效OpenNRowsInData(用户名字符串,字符串密码,诠释browserNr)
{
试
{
浏览器[browserNr] .DeleteAllCookies();
浏览器[browserNr] .GoToUrl(链接);
浏览器[browserNr] .waittingID(txtUserName,15);
浏览器[browserNr] .FindElementById(txtUserName)的SendKeys(用户名)。
浏览器[browserNr] .FindElementById(txtpassword)的SendKeys(密码)。
浏览器[browserNr] .FindElementById(btnlogin)点击();
}
}
如果你想这样做对两种浏览器尝试运行阵列上的循环:
为(INT J = 0; J< browser.Length; J ++)
{
AllCase(0,j)的;
AllCase(1,j)的;
}
这一次并行计算实际上将还清。因为你可以做同样的东西在同一时间两个浏览器,但似乎你不应该这样做既
AllCase
- 在同浏览器的情况下,在同一时间,因为他们是连续
编辑:
在您的编辑看来,当你在你的循环去,你在
OpenNRowsInData
设定第一次在这个位置0
stepRun [IDX] = 1;
然后再调用
AllCase();
这时候就会执行
案例1:
ClickBuy();
中断;
这是第二次迭代将再次尝试对
$ B开关$ b开关(stepRun [IDX])
但这次它会找到一个
1
在stepRun $ c中的位置
0
$ C>。对于所有的
,永不回头的情况下调用OpenNRowsInData
。
这就是为什么你只得到了第一行
编辑2:
如果你想在同一时间在两个浏览器,你可以使执行
AllCase
的两起案件和这个常规您可以在2个独立的线程中运行:公共无效doEverything(INT browserIndex)
{
AllCase(0,browserIndex);
AllCase(1,browserIndex);
}
和这样开始线程:
为(INT J = 0; J< browser.Length; J ++)
{
INT brInd = j的;
线程t =新主题(()=> doEverything(brInd));
t.Start();
}
您也可以通过整个浏览器对象的方法,并通过它进一步下跌到
OpenNRowsInData
,并用它还有All code below only to create two processes. While I need to create n process to open Chrome browser and run
OpenNRowsInData(user, pwd)
based ondtUser.Rows.Count
because this is dynamic.And I don't know why
OpenNRowsInData(user, pwd);
always only get first rows.I have a
DataTable
range from 1-7 rows.Typically, I was usually using to create two objects.
static myObject[] browser = new myObject[] { new myObject(Browsers.Chrome, 0), new myObject(Browsers.Chrome, 0) }; Thread t1, t2; t1 = new Thread(new ThreadStart(AllCase)) { Name = "Thread1" }; t1.Start(); t2 = new Thread(new ThreadStart(AllCase)) { Name = "Thread2" }; t2.Start();
In class
AllCase
:static int[] stepRun = { 0, 0 }; private void AllCase() { int idx = int.Parse(Thread.CurrentThread.Name.Replace("Thread", "")) - 1; switch (stepRun[idx]) { case 0: foreach (DataRow row in dtUser.Rows) { user = row["user"].ToString(); pwd = row["pwd"].ToString(); OpenNRowsInData(user, pwd); } break; case 1: ClickBuy(); break; } }
And in another, before I using parallel process. Currently, I don't want to use it.
Parallel.ForEach( dtUser.AsEnumerable(), items => OpenNRowsInData(items["user"].ToString(), items["pwd"].ToString()));
I want to get only columns
user
andpwd
.Tried with code, I tried to add a parameter in
action()
method like:action(string user, string pwd)
.public static void ForEach<T>(this IEnumerable<T> source, Action<T> action) { if (source == null) throw new ArgumentNullException("source"); if (action == null) throw new ArgumentNullException("action"); foreach (T item in source) { action(item); } }
Updated:
Add class
OpenNRowsInData(string username, string password)
:private void OpenNRowsInData(string username, string password) { int idx = int.Parse(Thread.CurrentThread.Name.Replace("Thread", "")) - 1; try { browser[idx].DeleteAllCookies(); browser[idx].GoToUrl(link); browser[idx].waittingID("txtUserName", 15); browser[idx].FindElementById("txtUserName").SendKeys(username); browser[idx].FindElementById("txtpassword").SendKeys(password); browser[idx].FindElementById("btnlogin").Click(); stepRun[idx] = 1; AllCase(); } }
解决方案And I don't know why OpenNRowsInData(user, pwd); always only get first rows.
It seems for me that your second threat is too fast. Before the first can finish it will execute
ClickBuy();
so only the first row can be processed.try to do it one step after the other
AllCase(0, 0); AllCase(1, 0); private void AllCase(int case, int browserNr) { switch (case) { case 0: foreach (DataRow row in dtUser.Rows) { user = row["user"].ToString(); pwd = row["pwd"].ToString(); OpenNRowsInData(user, pwd, browserNr); } break; case 1: ClickBuy(); break; } } private void OpenNRowsInData(string username, string password, int browserNr) { try { browser[browserNr].DeleteAllCookies(); browser[browserNr].GoToUrl(link); browser[browserNr].waittingID("txtUserName", 15); browser[browserNr].FindElementById("txtUserName").SendKeys(username); browser[browserNr].FindElementById("txtpassword").SendKeys(password); browser[browserNr].FindElementById("btnlogin").Click(); } }
If you want to do it for both browsers try running a loop on the array:
for(int j = 0; j < browser.Length;j++) { AllCase(0, j); AllCase(1, j); }
This time parallelisation would actually pay off. Because you could do the same stuff in both browsers at the same time, but it seems you should not do both
AllCase
- cases in the same browser at the same time, because they are consecutiveEDIT:
After your edit it seems that when you go in your loop the first time you set in
OpenNRowsInData
at the position0
stepRun[idx] = 1;
and then call again
AllCase();
this time it will execute
case 1: ClickBuy(); break;
on the second iteration it will try again to switch on
switch (stepRun[idx])
but this time it will find a
1
on the position0
instepRun
. For all and never go back in the case to callOpenNRowsInData
. This is why you get only the first row.EDIT 2:
if you want to do it at the same time in both browsers you can make a routine that executes the two cases of
AllCase
and this routine you can run in 2 separate threads:public void doEverything(int browserIndex) { AllCase(0, browserIndex); AllCase(1, browserIndex); }
and start the threads like this:
for(int j = 0; j < browser.Length;j++) { int brInd = j; Thread t= new Thread(()=>doEverything(brInd)); t.Start(); }
you could also pass the entire browser object to the method and pass it further down to
OpenNRowsInData
and use it there这篇关于如何在C#中DataTable.Rows.Count n对象的基础?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!