我正在尝试创建一个Web表单应用程序,该应用程序通过单击表单上的按钮从数据库中获取数据,然后使用该数据点击REST服务,最后存储具有OK响应的项目。但是该服务大约需要 3-4 秒来响应每个请求,总点击量约为:80,000,一旦我开始点击 REST 服务,表单应用程序就会开始"无响应"。应用程序的 PFB 代码。任何帮助都会很棒。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Configuration;
using RestSharp;
using Newtonsoft.Json;
namespace Test_Data_Generator
{
public partial class Form1 : Form
{
string BaseDevURL = string.Empty;
string BaseSIURL = string.Empty;
string _currentProductItemID = string.Empty;
List<StringValue> ValidProductItemsIDs = new List<StringValue>();
private IRestResponse _response;
private ServiceActions _RESTservice;
private Dictionary<string, string> _requestHeaders;
private DataTable ProductItemIDs;
BindingList<StringValue> blist = new BindingList<StringValue>();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
_requestHeaders = new Dictionary<string, string>
{
{"ContentType", "application/json"},
{"Accept", "application/json"}
};
}
private void btnGetTestData_Click(object sender, EventArgs e)
{
//Populate the data table on the basis of radio button which is checked
if (!(rbDev.Checked) && !(rbSI.Checked))
System.Windows.Forms.MessageBox.Show("Please check a radio button for desired environment");
else if (rbDev.Checked)
{
ProductItemIDs = DataBase.GetValues(rbDev.Text);
BaseDevURL = ConfigurationManager.AppSettings["Service DEV"];
}
else if (rbSI.Checked)
{
ProductItemIDs = DataBase.GetValues(rbSI.Text);
BaseSIURL = ConfigurationManager.AppSettings["Service SI"];
}
try
{
dgvDBdata.DataSource = ProductItemIDs;
lblDbValuesCount.Text = ProductItemIDs.Rows.Count.ToString() + " rows";
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show("Exception caught : " + ex.Message);
}
}
private void btnGetDataFromRESTService_Click(object sender, EventArgs e)
{
dgvValidPIDs.DataSource = blist;
//Populate the data table on the basis of radio button which is checked
if (!(rbDev.Checked) && !(rbSI.Checked))
System.Windows.Forms.MessageBox.Show("Please check a radio button for desired environment");
else if (rbDev.Checked)
{
_PITSservice = new ServiceActions(BaseDevURL);
}
else if (rbSI.Checked)
{
_PITSservice = new ServiceActions(BaseSIURL);
}
try
{
//Submit Rest request for each ProductItem ID retrieved from DB
for (int i = 1; i < 100; i++ )
{
_currentProductItemID = ProductItemIDs.Rows[i].ToString();
_response = _RESTservice.SubmitRestRequest(
null,
"ProductItemSummaries?ProductItemId=" + _currentProductItemID,
"Get",
DataFormat.Json,
DataFormat.Json,
_requestHeaders);
if (_response.StatusCode.ToString().Contains("OK"))
blist.Add(new StringValue(_currentProductItemID));
dgvValidPIDs.Refresh();
}
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show("Exception caught : " + ex.Message);
}
}
}
public class StringValue
{
string _value;
public StringValue(string s)
{
_value = s;
}
public string Value { get { return _value; } set { _value = value; } }
}
}
在窗体上使用 BackgroundWorker 并挂接名为 DoWork
、 ProgressChanged
RunWorkerCompleted
的事件。
将代码从 btnGetDataFromRESTService_Click
移动到 DoWork
事件,并将此行添加到按钮事件而不是 this.backgroundWorker1.RunWorkerAsync();
。
这将释放您的 UI 线程。您需要从事件中管理控件的状态。