我有一个按钮“开始”的胜利表格,当我点击它时,开始逐行读取 .txt 文件并将请求发布到服务器。我的问题是-当我单击“开始”以从文件中读取 50 行然后停止并等待再次单击“开始”时,我该怎么做。线程是这里唯一可能的解决方案吗?这是我的按钮:
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;
}
}
这是我的发帖请求:
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();
这里最重要的不是一键读取整个文件“开始”。必须等待另一个“开始”。阅读 50 行并等待单击“开始”以阅读第二个 50 行。希望现在更清楚。