繁体   English   中英

线程竞赛和lock关键字

[英]thread races and the lock keyword

我的应用程序具有一个登录区域,该登录区域使用线程访问数据库而不冻结UI。

在登录期间,系统进行一些操作以打开主窗口,并在另一个线程中加载用户数据(和权限)。

但是有时,当数据库挂起时,系统会在加载用户之前完成加载。

系统执行的最后一个操作是应用权限(将线程与用户数据一起加载)。

我还有一个静态的User对象,该对象保存进行登录的用户的数据,例如其权限等。

当应用程序在加载用户之前从静态用户对象获取数据时,就会出现问题。 权限为空,未正确应用,从而导致不良行为。

如何确保静态用户已加载,并且仅在极端的网络/数据库延迟情况下冻结UI?

我尝试使用lock ,但是它根本不起作用。

这是我的代码:

int id = User.GetId(user, password); // returns 0 if the user does not exists

if (id != 0)
{
     new Thread(() => 
     {
          lock (User.CurrentUser) // Try to lock the static CurrentUser object
          { 
              User.LoadCurrentUser(id);  // Loads the CurrentUser object
          }

      }).Start();

      InitializeSystem(); //Make some work on the system. After that, this
                          //will get the User.CurrentUser object to apply the
                          //user's permissions.
  }

方法User.LoadCurrentUser(id); 填补用户。

我知道锁不起作用,因为我放了Thread.Sleep(5000); 进入锁,系统将在2秒内以错误的权限启动。

不考虑删除线程,因为它会破坏我的UI。

编辑

我需要使用.Net Framework 4.0来确保Windows XP兼容性。

这是await或链接任务的理想人选。

等待版本:

async void YourLoginButtonEventHandler() {
    int id = User.GetId(user, password); // returns 0 if the user does not exists

    if (id != 0) {
        await Task.Run(() => User.LoadCurrentUser(id));  // Loads the CurrentUser object
        InitializeSystem();
    }
}

或者,您可以将它们链接起来:

if (id != 0) {
    Task.Run(() => User.LoadCurrentUser(id))
        .ContinueWith(t => InitializeSystem(), TaskScheduler.FromCurrentSynchronizationContext);
}

注意:可能是几个语法错误。我直接在这里输入。

我基本上想向您展示的是..您正在艰难地解决这个问题。 对于这种事情有更新的构造..学习它们:)

您可以使用Thread.Join阻塞主线程,直到另一个线程完成或超时。

int id = User.GetId(user, password); // returns 0 if the user does not exists

if (id != 0)
{
 var newThread = new Thread(() => 
 {
      lock (User.CurrentUser) // Try to lock the static CurrentUser object
      { 
          User.LoadCurrentUser(id);  // Loads the CurrentUser object
      }

  }).Start();

  InitializeSystem(); 

  if (newThread.Join(someTimeOut))
     // use the User.CurrentUser object to apply the user's permissions.
  else
     // thread timed out, waiting for user load takes too long...

 }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM