简体   繁体   English

使用Web服务将客户端标记为脱机的最佳方法

[英]Best way to mark Client Offline using Web-Service

Currently I'm working with a Web-Service where Client's Continually should update Data . 目前,我正在使用Web服务,其中“客户的持续”应该更新Data。 So the WebService has a List<Client> clients; 因此,Web服务具有List<Client> clients; where it stores Connected Client's : 它存储连接的客户端的位置:

[WebMethod]
public string Connect(Size desktopsize)
{
  Client clienti = new Client();
  clienti.ID = "Client_" + Counter.ToString();
  clienti.Desktopsize = desktopsize;
  clienti.Lastupdate = DateTime.Now;
  Counter++;
  clients.Add(clienti);
  return clienti.ID;
}

so every client has an ID ,and continue Updating it's Data's. 因此每个客户都有一个ID,然后继续更新它的数据。 I need to mark a Client Offline ,than when the Last Update of a specific Client was 1 minute ago .(Im also updating the UpdateTime every time when a value is changed like : 我需要将客户端标记为脱机,而不是将特定客户端的“最近更新”设置为1分钟前。(每次更改值时,我也会更新UpdateTime,例如:

public bool SingleClick
{
  get
  {
    bool tmpBolean = singleclick;
    singleclick = false;
    return tmpBolean;
  }
  set
  {
    this.lastupdate = DateTime.Now;
    singleclick = value;
  }
}

First i used to create a Thread at Client 首先,我曾经在客户端创建线程

private void CheckOnlinestate()
{
  while (isRunning)
  {
    TimeSpan ts = DateTime.Now - lastupdate;
    if (ts.TotalMinutes >= 1)
    {
      isRunning = false;
      this.Dispose();
    }
  }
} 

than at WebService a thread which monitor's if Client should remove from list: 而不是WebService上的一个线程,该线程监视Client是否应从列表中删除:

public void CheckClients()
{
  while (true)
  {
    foreach (Client c in clients)
    {
      if (c.ShouldDispose)
      {
        clients.Remove(c);
      }
    }
    Thread.Sleep(200);
  }
}

So the issue is ,how to use this method into a Thread Correctly ,should i Create and Start the thread at WebService Constructor ,or there is a better way to do that. 因此,问题是,如何正确地在线程中使用此方法,应该在WebService Constructor上创建并启动线程,还是有一种更好的方法。 I cant imagine the best way how to remove a specific Client from List<Client> clients; 我无法想象如何从List<Client> clients;删除特定客户端的最佳方法List<Client> clients;

You can use System.Web.HttpRuntime.Cache class instead of implementing your own. 您可以使用System.Web.HttpRuntime.Cache类,而不是实现自己的类。

There are some other problems with your code. 您的代码还有其他一些问题。 Accesses to Counter and clients are uncontrolled which are shared among threads. Counterclients访问不受控制,这些访问在线程之间共享。 You should use some sync. 您应该使用一些同步。 mechanism such as lock while accessing these objects ( You can forget my comment about clients if you already declared it as ConcurrentDictionary ) 访问这些对象时使用诸如lock机制( 如果您已经将其声明为ConcurrentDictionary则可以忘记我对clients评论

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

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