需要后台工作人员帮助

发布于 2024-08-27 13:45:32 字数 1182 浏览 2 评论 0原文

我有执行网络服务请求的代码。 在执行此请求时,我需要一个进度条才能独立移动。

我的问题是,我只需要每隔 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦回梦里 2024-09-03 13:45:34

我一直使用后台工作者,它们非常适合处理长时间的操作。

从你的代码中

#region Background Work of My Request

    private void ProcessMyRequest()
    {            
        if (!bkgWorkerMyRequest.IsBusy)
        {
            lblMessageToUser.Text = "Processing Request...";
            btnProcessRequest.Enabled = false;
            bkgWorkerMyRequest.RunWorkerAsync();
        }
    }
    private void bkgWorkerMyRequest_DoWork(object sender, DoWorkEventArgs e)
    {
        // let's process what we need in a diferrent thread than the UI thread
        string r = GetStuffDone();
        e.Result = r;
    }
    private void bkgWorkerMyRequest_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        string myResult = (String)e.Result;    

        lblMessageToUser.Text = myResult;
        btnProcessRequest.Enabled = true;
    }

#endregion

    private function string GetStuffDone() 
    {
        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);
        return ConvertStringArrayToString(result.Details);
    }

,你需要做的就是调用该方法:

ProcessMyRequest();

它就会完成这项工作。 如果您需要让主线程了解进度,您可以在

private void bkgWorkerMyRequest_ProgressChanged(
    object sender, ProgressChangedEventArgs e)
{
    progressBar1.Value = e.ProgressPercentage;
}

您需要更改的bkgWorkerMyRequest_DoWork方法中 使用ProgressChanged事件代码

//reports a percentage between 0 and 100
bkgWorkerMyRequest.ReportProgress(i * 10); 

记住:

alt text http:// www.balexandre.com/temp/2010-04-07_1200.png

但是,在尝试调试方法 GetStuffDone 时,您会遇到困难,因为调试多线程应用程序有点困难,

所以,我所做的是,在没有工作人员的情况下调试所有内容,然后应用工作人员。

对我来说效果很好,如果您需要更多帮助,请告诉我。


添加

我不知道您在工作人员中获取网格,抱歉,为此,只需将网格作为参数发送并使用它,请更改:

bkgWorkerMyRequest.RunWorkerAsync(dataGridView1);

string r = GetStuffDone((GridView)e.Argument);

private function string GetStuffDone(GridView dataGridView1)

I use Background Workers all the time, they are great for processing long time actions.

from your code

#region Background Work of My Request

    private void ProcessMyRequest()
    {            
        if (!bkgWorkerMyRequest.IsBusy)
        {
            lblMessageToUser.Text = "Processing Request...";
            btnProcessRequest.Enabled = false;
            bkgWorkerMyRequest.RunWorkerAsync();
        }
    }
    private void bkgWorkerMyRequest_DoWork(object sender, DoWorkEventArgs e)
    {
        // let's process what we need in a diferrent thread than the UI thread
        string r = GetStuffDone();
        e.Result = r;
    }
    private void bkgWorkerMyRequest_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        string myResult = (String)e.Result;    

        lblMessageToUser.Text = myResult;
        btnProcessRequest.Enabled = true;
    }

#endregion

    private function string GetStuffDone() 
    {
        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);
        return ConvertStringArrayToString(result.Details);
    }

all you need to do is call the method:

ProcessMyRequest();

and it will do the job. If you need to let the main Thread to be aware of progress, you can use the ProgressChanged event

private void bkgWorkerMyRequest_ProgressChanged(
    object sender, ProgressChangedEventArgs e)
{
    progressBar1.Value = e.ProgressPercentage;
}

in the bkgWorkerMyRequest_DoWork method you need to change the code to have

//reports a percentage between 0 and 100
bkgWorkerMyRequest.ReportProgress(i * 10); 

Remember:

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 applications

So, 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:

bkgWorkerMyRequest.RunWorkerAsync(dataGridView1);

string r = GetStuffDone((GridView)e.Argument);

private function string GetStuffDone(GridView dataGridView1)
陌路终见情 2024-09-03 13:45:34

创建一个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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文