需要后台工作人员帮助
我有执行网络服务请求的代码。 在执行此请求时,我需要一个进度条才能独立移动。
我的问题是,我只需要每隔 1 或 2 秒运行一次进度更新,并检查请求的进度是否已完成。
NetBasisServicesSoapClient client = new NetBasisServicesSoapClient();
TransactionDetails[] transactions = new TransactionDetails[dataGridView1.Rows.Count - 1];
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
transactions[i] = new TransactionDetails();
transactions[i].TransactionDate = (string)dataGridView1.Rows[i].Cells[2].Value;
transactions[i].TransactionType = (string)dataGridView1.Rows[i].Cells[3].Value;
transactions[i].Shares = (string)dataGridView1.Rows[i].Cells[4].Value;
transactions[i].Pershare = (string)dataGridView1.Rows[i].Cells[5].Value;
transactions[i].TotalAmount = (string)dataGridView1.Rows[i].Cells[6].Value;
}
CostbasisResult result = client.Costbasis(dataGridView1.Rows[0].Cells[0].Value.ToString(), dataGridView1.Rows[0].Cells[1].Value.ToString(), transactions, false, "", "", "FIFO", true);
string result1 = ConvertStringArrayToString(result.Details);
I have code that does a web-service request.
While doing this request I need a progress-bar to be moving independently.
My problem is that I just need to say run a progress update every 1 or 2 seconds and check to see if progress of the request has been completed.
NetBasisServicesSoapClient client = new NetBasisServicesSoapClient();
TransactionDetails[] transactions = new TransactionDetails[dataGridView1.Rows.Count - 1];
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
transactions[i] = new TransactionDetails();
transactions[i].TransactionDate = (string)dataGridView1.Rows[i].Cells[2].Value;
transactions[i].TransactionType = (string)dataGridView1.Rows[i].Cells[3].Value;
transactions[i].Shares = (string)dataGridView1.Rows[i].Cells[4].Value;
transactions[i].Pershare = (string)dataGridView1.Rows[i].Cells[5].Value;
transactions[i].TotalAmount = (string)dataGridView1.Rows[i].Cells[6].Value;
}
CostbasisResult result = client.Costbasis(dataGridView1.Rows[0].Cells[0].Value.ToString(), dataGridView1.Rows[0].Cells[1].Value.ToString(), transactions, false, "", "", "FIFO", true);
string result1 = ConvertStringArrayToString(result.Details);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我一直使用后台工作者,它们非常适合处理长时间的操作。
从你的代码中
,你需要做的就是调用该方法:
它就会完成这项工作。 如果您需要让主线程了解进度,您可以在
您需要更改的
bkgWorkerMyRequest_DoWork
方法中 使用ProgressChanged
事件代码记住:
alt text http:// www.balexandre.com/temp/2010-04-07_1200.png
但是,在尝试调试方法
GetStuffDone
时,您会遇到困难,因为调试多线程应用程序有点困难,所以,我所做的是,在没有工作人员的情况下调试所有内容,然后应用工作人员。
对我来说效果很好,如果您需要更多帮助,请告诉我。
添加
我不知道您在工作人员中获取网格,抱歉,为此,只需将网格作为参数发送并使用它,请更改:
I use Background Workers all the time, they are great for processing long time actions.
from your code
all you need to do is call the method:
and it will do the job. If you need to let the main Thread to be aware of progress, you can use the
ProgressChanged
eventin the
bkgWorkerMyRequest_DoWork
method you need to change the code to haveRemember:
alt text http://www.balexandre.com/temp/2010-04-07_1200.png
You will, however get stuck when trying to Debug the method
GetStuffDone
as it's that kinda hard debugging multi threaded applicationsSo, what I do is, debug everything without workers and then apply the workers.
Works fine for me, let me know if you need any more help on this.
added
I didn't aware that you were getting the Grid in the worker, sorry, for this, just send the grid as a argument and use it, please change:
创建一个BackgroundWorker(调用实例“bgw”)并输入“bgw.DoWork += ”,然后输入TAB TAB。然后,Visual Studio 生成 DoWork 事件处理程序方法。将上面的代码复制到处理程序中,瞧。
我认为您报告进度没有意义,因为您的进度是由您无法影响的 Web 服务请求的持续时间决定的(并且您不能将其分解为更小的任务)。因此,只需显示“正在工作”对话框并启动后台任务来调用 Web 服务。完成后关闭“正在工作”对话框。
Create a BackgroundWorker (call the instance "bgw") and type "bgw.DoWork += " followed by TAB TAB. Visual Studio then generates the DoWork event handler method. Copy the code above into the handler and voila.
I don't think it makes sense for you to report progress, since your progress is determined by the duration of the web service request over which you have no influence (and you cannot break it into smaller tasks). As such, just display the "doing work" dialog and initiate the background task to call the web service. When it's done close the "doing work" dialog.