I have win form with one button “start” and when i click on it start reading .txt file row by row and post requests to a server.
My question is –
How can i make when i click on “start” to read 50 rows from file then stop and wait for another click on “start”.
Is threading the only possible solution here?
This is my button:
private void btnStart_Click(object sender, EventArgs e)
{
List<string> List = LoadFromFile("FILE");
int dialogid = 0;
foreach (string g in List)
{
dialogid++;
Dictionary<string, string> parameters = new Dictionary<string, string>();
parameters.Add("number", g);
parameters.Add("dialogid", dialogid.ToString());
if (InvokeService(this.tbWebServiceURL.Text, parameters) == false)
{
MessageBox.Show("ERROR!", "ERROR");
return;
}
}
And here is my post request:
private bool InvokeService(string ServiceURL, Dictionary<string, string> parameters)
{
try
{
string data = "";
int cnt = 0;
byte[] dataStream = Encoding.UTF8.GetBytes(data);
WebRequest webRequest = WebRequest.Create("URL");
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.ContentLength = dataStream.Length;
foreach (KeyValuePair<string, string> kvk in parameters)
{
webRequest.Headers.Add(kvk.Key, kvk.Value);
}
WebResponse response = webRequest.GetResponse();
Stream dataStreamResponse = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStreamResponse);
string responseFromServer = reader.ReadToEnd();
Console.WriteLine(responseFromServer);
reader.Close();
dataStreamResponse.Close();
response.Close();
Most important here is not read whole file with one click on “start”. Must wait for another “start”. Read 50 rows and wait to click on ‘start” to read second 50 rows.
Hope now is more clear.
BackgroundWorker outline – not tested: