Help with C # Task class

I use WatiN to analyze my website. I have a button that starts the process. I open a browser window and move to where I need to go, then create a new task that calls a method called DoWork .

My problem is that if I call a new method at the end of DoWork to do something else, I get strange results when I try to make the program navigate my site, however, if I don't call this new method from DoWork and just plug in a new method before clicking a button, everything works fine. So my question is, am I incorrectly calling my new method from a background process method, DoWork ?

code:

 IE browser = new IE("http://www.mywebsite.com/"); string startYear; string endYear; int NumRows; Task myThread; public Form1() { InitializeComponent(); } private void Start_Click(object sender, EventArgs e) { startYear = txtStartYear.Text; endYear = txtEndYear.Text; //website navigation work removed for brevity browser.Button(Find.ById("ContentPlaceHolder1_btnApplyFilter")).Click(); int numRows = browser.Div(Find.ById("scroller1")).Table(Find.First()).TableRows.Count -1; NumRows = numRows; lblTotalRows.Text = numRows.ToString(); myThread = Task.Factory.StartNew(() => DoWork()); } public void DoWork() { List<string> myList = new List<string>(NumRows); txtStartYear.Text = startYear; txtEndYear.Text = endYear; for (int i = 1; i < NumRows; i++) { TableRow newTable = browser.Div(Find.ById("scroller1")).Table(Find.First()).TableRows[i]; string coll = string.Format("{0},{1},{2},{3},{4}", newTable.TableCells[0].Text, newTable.TableCells[1].Text, newTable.TableCells[2].Text, newTable.TableCells[3].Text, newTable.TableCells[4].Text); myList.Add(coll); label1.Invoke((MethodInvoker)delegate { label1.Text = i.ToString(); }); } //database work removed for brevity. browser.Button(Find.ById("btnFilter")).Click(); newMethod(); } public void newMethod() { int start = int.Parse(startYear); start++; startYear = start.ToString(); int end = int.Parse(endYear); end++; endYear = end.ToString(); browser.SelectList(Find.ById("selStartYear")).SelectByValue(startYear); browser.SelectList(Find.ById("selEndYear")).SelectByValue(endYear); //removed for brevity } } 

Repeat if I call newMethod from DoWork , the line browser.SelectList(Find.ById("selStartYear")).SelectByValue(startYear) does not work correctly, but if I remove the call to newMethod from DoWork and just close newMethod until the button works fine. I am wondering if this is related to the DoWork background task?

When I say that it does not behave correctly, I mean that when you select an item from the drop-down list, the page is automatically sent back, however the above line of code selects it, but the page does not send back, which should be possible. If I do not call the method in DoWork , I do not have this problem.

+4
source share
2 answers

You are changing the user interface element from a thread other than the UI. You already have code that deals with DoWork through Control.Invoke - you need to do the same for newMethod . It would probably be easiest to call the whole method in the user interface thread:

 // At the end of DoWork Action action = newMethod; label.BeginInvoke(action); 

(I use label.BeginInvoke , since I'm not sure that the browser itself is a β€œnormal” control, but using label anyway will be in the right order. If browser.BeginInvoke compiles, it will be clearer.)

+2
source

I suspect this is a problem with the select list control. When I browse sites, I sometimes select drop-down items from the keyboard. Sometimes this simply does not return, while the mouse always guarantees a return transmission.

I think you might be better off adding an extra button and doing something like browser.Button(Find.ById("btnFilter")).Click(); to trigger a postback.

If the functions in the browser do not perform the correct cross-streams check, then Jon Skeet should help.

0
source

Source: https://habr.com/ru/post/1342244/


All Articles