后台工作人员更新进度冻结 datagridview

发布于 2024-12-07 07:23:07 字数 1210 浏览 1 评论 0原文

我正在尝试编写一个应用程序来获取各个 AD 组中的用户列表,并将它们显示在表单的数据网格中。 当单击表单上的按钮时,它会触发一个后台工作人员,该工作人员调用一个方法来获取 AD 组的用户列表。 后台工作者返回一个数据表,后台工作者完成的方法将数据网格数据源设置为数据表,一切正常。 当我尝试使用后台工作进度更改方法在填充每个数据表列时更新数据网格时,问题就出现了。当后台工作人员运行时,它工作正常,但当它完成网格并且表单对多个数据变得无响应时,问题就出现了。分钟。 我正在努力弄清楚这是为什么。

代码示例:

backroundWorker_DoWork(.........)
{
    DataTable dt = new DataTable();
    DataRow dr;

    //Get datatable from argument
    DataTable dt2 = e.Argument as DataTable

    //get list of AD groups to check
    for (int i = 0; i > dt2.Rows.Count; i++)
    {
        columnName = dt2.Rows[i][0].ToString();
        groupName = dt2.Rows[i][1].ToString();

        //Call Method to get users from nested AD groups
        GetADUsers(groupName)

        //Add users returned from above method to DataTable dt
        //..........


        //Send DataTable to Report Progress
        backgroundWorker.ReportProgress(0, dt);

    }


    e.Result = dt;

 }


backroundWorker_ProgressChanged(..........)
{
    DataTable dt = e.UserState as DataTable;
    dataGridView1.DataSource = dt;
}


backroundWorker_RunWorkerCompleted(..........)
{
    DataTable dt = e.UserState as DataTable;
    dataGridView1.DataSource = dt;
}

I am trying to write an application to get a list a users in various AD groups and display them in a datagrid on a form.
When a button on my form is clicked it fires a background worker which calls a method to get the user list for an AD group.
The background worker returns a data table and the background worker completed method sets the data grid data source to the data table and everything works OK.
The problem comes when I try to use the background worker progress changed method to update the data grid as each data table column is filled in. It works OK while the background worker is running but when it finishes the grid and the form becomes unresponsive for several minutes.
I'm struggling to figure out why this is.

code example:

backroundWorker_DoWork(.........)
{
    DataTable dt = new DataTable();
    DataRow dr;

    //Get datatable from argument
    DataTable dt2 = e.Argument as DataTable

    //get list of AD groups to check
    for (int i = 0; i > dt2.Rows.Count; i++)
    {
        columnName = dt2.Rows[i][0].ToString();
        groupName = dt2.Rows[i][1].ToString();

        //Call Method to get users from nested AD groups
        GetADUsers(groupName)

        //Add users returned from above method to DataTable dt
        //..........


        //Send DataTable to Report Progress
        backgroundWorker.ReportProgress(0, dt);

    }


    e.Result = dt;

 }


backroundWorker_ProgressChanged(..........)
{
    DataTable dt = e.UserState as DataTable;
    dataGridView1.DataSource = dt;
}


backroundWorker_RunWorkerCompleted(..........)
{
    DataTable dt = e.UserState as DataTable;
    dataGridView1.DataSource = dt;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

转身泪倾城 2024-12-14 07:23:07

在 RunWorkerCompled() 事件处理程序中,您应该使用 e.Result 而不是 e.UserState,即您的代码应该类似于:

DataTable dt = e.Result as DataTable,

作为在 RunWorkerCompleted 事件中读取结果之前的实践,您还应该使用 e.Error != null 语法检查错误,请参阅以下 MSDN 链接中使用后台工作组件的完整 ComputeFibonacci 示例:

http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx

In RunWorkerCompled() event handler, you should use e.Result instead of e.UserState i.e. your code should be like:

DataTable dt = e.Result as DataTable,

As a practice before reading the result in RunWorkerCompleted event, you should also check for error using e.Error != null syntax, see the complete ComputeFibonacci example using background worker component in the below MSDN link:

http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx

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