返回值后执行代码行

本文关键字:代码 执行 返回值 | 更新日期: 2023-09-27 18:30:27

我正在尝试在收益返回后执行代码。我有调用另一个方法的 WebMethod,我想在将值返回给 WebMethod 后做一些工作(执行方法 setNextCampaign())这是我的代码:

[WebMethod]
public long NextCall(long UserID)
{
    CampaignManagerExtended cacheCampaignManager = fillCampaignManager(false);
    return cacheCampaignManager.NextCall(UserID).First();
}

public IEnumerable<long> NextCall(long UserID)
        {
            UserExtended ue = null;
            try
            {
                if (!CacheUsers.ContainsKey(UserID))
                    AddUser(UserID);
                ue = CacheUsers.First(p => p.Key == UserID).Value;
                if (ue.NextCampaign != null)
                {
                    QueueItemPersonal qp = ue.NextFromPersonalQueue();
                    if (qp != null)
                    {
                        yield return qp.CampaignPersonID;
                    }
                    else 
                    { 
                        QueueItemScheduled qs = ue.NextCampaign.NextFromScheduleQueue();
                        if (qs != null)
                        {
                            yield return qs.CampaignPersonID;
                        }
                        else 
                        { 
                            QueueItemGeneral qg = ue.NextCampaign.NextFromGeneralQueue();
                            if (qg != null)
                            {
                                yield return qg.CampaignPersonID;
                            }                  
                            else
                                 yield return 0;
                        }
                    }
                    ue.NextCampaign.fillGeneralQueue();
                    ue.setNextCampaign();
                }
                else
                    yield return 0;
            }
            //catch
            //{
            //    yield return 0;
            //}
            finally
            {
            }
        }

有什么想法吗?

返回值后执行代码行

而不是这个

<some stuff>;
yield return <something>;
<some epilog stuff that should be executed after yield>

这样做:

try
{
  <some stuff>;
  yield return <something>;
}
finally
{
  <some epilog stuff that should be executed after yield>
}

只需保存 yeild 直到您准备好返回它:

[WebMethod]
public long NextCall(long UserID)
{
    CampaignManagerExtended cacheCampaignManager = fillCampaignManager(false);
    return cacheCampaignManager.NextCall(UserID).First();
}
public IEnumerable<long> NextCall(long UserID)
{
    int returnID;
    UserExtended ue = null;
    try
    {
        if (!CacheUsers.ContainsKey(UserID))
        {
            AddUser(UserID);
        }
        ue = CacheUsers.First(p => p.Key == UserID).Value;
        if (ue.NextCampaign != null)
        {
            QueueItemPersonal qp = ue.NextFromPersonalQueue();
            if (qp != null)
            {
                returnID =  qp.CampaignPersonID;
            }
            else 
            { 
                QueueItemScheduled qs = ue.NextCampaign.NextFromScheduleQueue();
                if (qs != null)
                {
                    returnID  = qs.CampaignPersonID;
                }
                else 
                { 
                    QueueItemGeneral qg = ue.NextCampaign.NextFromGeneralQueue();
                    if (qg != null)
                    {
                        returnID = qg.CampaignPersonID;
                    }                  
                    else
                    {
                         returnID = 0;
                    }
                }
            }
            ue.NextCampaign.fillGeneralQueue();
            ue.setNextCampaign();
        }
        else
        {
            returnID = 0;
        }
        yield return returnID;
    }
    //catch
    //{
    //    yield return 0;
    //}
    finally
    {
    }
}