I have a windows form which has the following code
BindingList<TicketResult> tickResults = new BindingList<TicketResult>();
BindingSource bindingSource1 = new BindingSource();
Action<String> call;
private void method(String x)
{
if (this.dataGridView1.InvokeRequired)
{
lock (this)
{
dataGridView1.Invoke(
new MethodInvoker(() =>
{
Debug.WriteLine(x);
tickResults[int.Parse(x)].Row = "first page";
dataGridView1.Refresh();
}));
}
}
}
public Form1()
{
call = method;
ServicePointManager.DefaultConnectionLimit = 48;
InitializeComponent();
tickResults.ListChanged += tickResults_ListChanged;
for (int i = 0; i < 10; i++)
{
TicketResult result = new TicketResult();
tickResults.Add(result);
}
bindingSource1.DataSource = tickResults;
dataGridView1.DataSource = bindingSource1;
for (int i = 0; i < 10; i++)
{
Search s = new Search();
int x = i;
Task.Run(() => s.start(x, this.call));
}
}
I don't understand why the change in tickResults
is not reflected without calling dataGridView1's
Refresh()
method.
Code for other classes which call the "call" delegate in the form are as follows:
class Search : ISearch
{
public async Task<bool> start(int i, Action<String> x)
{
bool result = false;
TicketLogic tixLogic = new TicketLogic();
try
{
await Task.Run(() => tixLogic.processFirstPage(i, x))
.ContinueWith((t) => tixLogic.processSecondPage(i, x))
.ContinueWith((t) => tixLogic.processThirdPage(i, x));
result = true;
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
result = false;
}
return result;
}
public async Task<bool> stop()
{
return false;
}
public async Task<bool> restart()
{
return false;
}
}
class TicketLogic
{
public async Task<bool> processFirstPage(int i, Action<String> x)
{
bool result = false;
try
{
HttpWebRequest request = WebRequest.CreateHttp("http://www.google.com");
WebResponse response = await request.GetResponseAsync();
StreamReader reader = new StreamReader(response.GetResponseStream());
String textResponse = await reader.ReadToEndAsync();
reader.Close();
response.Close();
result = true;
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
result = false;
}
return result;
}
public async Task<bool> processSecondPage(int i, Action<String> x)
{
bool result = false;
try
{
HttpWebRequest request = WebRequest.CreateHttp("http://www.example.com");
WebResponse response = await request.GetResponseAsync();
StreamReader reader = new StreamReader(response.GetResponseStream());
String textResponse = await reader.ReadToEndAsync();
//tixResult.Information = "Second Page";
reader.Close();
response.Close();
x(i.ToString());
result = true;
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
result = false;
}
return result;
}
public async Task<bool> processThirdPage(int i, Action<String> x)
{
bool result = false;
try
{
HttpWebRequest request = WebRequest.CreateHttp("http://www.hotmail.com");
WebResponse response = await request.GetResponseAsync();
StreamReader reader = new StreamReader(response.GetResponseStream());
String textResponse = await reader.ReadToEndAsync();
//tixResult.Information = "Third Page";
reader.Close();
response.Close();
x(i.ToString());
result = true;
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
result = false;
}
return result;
}
}
Before this I tried one more approach, in which I was passing the databound object to a computation Task, where the databound object got manipulated, but even there the result was the same i.e. the changes in the object were not reflected upon the grid until I clicked some Cell in the Grid or minimized/maximized the form.
My question is, why are the changes not being reflected in the Grid without calling datagrid refresh() ??