Linq to Sql 结果导致组合框异步

发布于 2024-12-05 18:30:11 字数 467 浏览 0 评论 0原文

我正在开发一个 winform 应用程序,并将在数据库中发出请求并异步填充我的组合框,但遇到访问控制问题,因为它们来自另一个线程,这是代码。

   this.backWorker.DoWork + = delegate
             {
                 comboBoxUsers.DataSource = repositoryUser.SelectAll();
                 comboBoxUsers.ValueMember = "UserId";
                 comboBoxUsers.DisplayMember = "Name";
             };

             backWorker.RunWorkerAsync ();

我正在研究 envoke 但我在实现它时遇到了困难, 我需要做的就是让 DoWork 事件可见进度条并选择执行此操作。

I'm developing a winform application and would make a request in the database and populate my combobox asynchronously but am having problem of access control because they come from another thread, here's the code.

   this.backWorker.DoWork + = delegate
             {
                 comboBoxUsers.DataSource = repositoryUser.SelectAll();
                 comboBoxUsers.ValueMember = "UserId";
                 comboBoxUsers.DisplayMember = "Name";
             };

             backWorker.RunWorkerAsync ();

I am studying about envoke but I'm having trouble getting to implement this,
I needed to do was leave the DoWork event visible progress bar and select to do this.

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

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

发布评论

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

评论(2

清晨说晚安 2024-12-12 18:30:11

仅在BackgroundWorker上查询您的存储库并通过ProgressChangedEvenHandler将结果返回到UI

   //Set the ComboBox Properties on the Form, not in the worker.
   comboBoxUsers.ValueMember = "UserId";
   comboBoxUsers.DisplayMember = "Name";

   BackgroundWorker = new BackgroundWorker();
   worker.DoWork += Worker_DoWork;
   worker.WorkerReportsProgress = true;
   worker.ProgressChanged += new ProgressChangedEventHandler(Worker_ProgressChanged);

   private void Worker_DoWork(object sender, DoWorkEventArgs e)
   {
        BackgrounderWorker worker = (BackgroundWorker)sender;

        //Query the database
        //Instantiate a custom-class to contain the results
        IList<Users> users = userRepository.SelectAll();
        QueryResults results = new QueryResults(users);
        worker.ReportProgress(0, results);
   }

   //Back In the UI Layer
   private void Worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
   {
       var result = (QueryResult)e.UserState;
       comboBoxUsers.DataSource = result.Users;
   }

Only query your repository on the BackgroundWorker and return the results through the ProgressChangedEvenHandler to the UI

   //Set the ComboBox Properties on the Form, not in the worker.
   comboBoxUsers.ValueMember = "UserId";
   comboBoxUsers.DisplayMember = "Name";

   BackgroundWorker = new BackgroundWorker();
   worker.DoWork += Worker_DoWork;
   worker.WorkerReportsProgress = true;
   worker.ProgressChanged += new ProgressChangedEventHandler(Worker_ProgressChanged);

   private void Worker_DoWork(object sender, DoWorkEventArgs e)
   {
        BackgrounderWorker worker = (BackgroundWorker)sender;

        //Query the database
        //Instantiate a custom-class to contain the results
        IList<Users> users = userRepository.SelectAll();
        QueryResults results = new QueryResults(users);
        worker.ReportProgress(0, results);
   }

   //Back In the UI Layer
   private void Worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
   {
       var result = (QueryResult)e.UserState;
       comboBoxUsers.DataSource = result.Users;
   }
山有枢 2024-12-12 18:30:11

你的委托应该这样写:

this.backWorker.DoWork += delegate(object s, DoWorkEventArgs args)
{
  //...
}

有关如何从另一个线程访问 UI 线程的 UI 控件的详细信息,请参阅此处:

从Backgroundworker DoWork访问窗口控制

该链接有一个明确的答案,这里有一个片段:

this.Invoke(new MethodInvoker(delegate {

  // This code executes on the GUI thread.

}));

your delegate should be written like this:

this.backWorker.DoWork += delegate(object s, DoWorkEventArgs args)
{
  //...
}

for details on how to access UI controls of the UI Thread from another thread see here:

Access windows control from Backgroundworker DoWork

there is a clear answer at that link, here a snippet:

this.Invoke(new MethodInvoker(delegate {

  // This code executes on the GUI thread.

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