0
while (true)
{
    lock (groupslocker)
    {
        if (groups.Count == 0)
            break;
        else
            gr = groups.Dequeue();
    }
    string url = rand_url();
    int h = od_group_join(gr, url);
    if (h == -1)
    {
        // How to return to this line string url = rand_url(); ???
    }
    else
      .........
}

如何返回if (h == -1)到线string url = rand_url();???

4

4 回答 4

3

听起来你想要一个循环:

int h;
string url;
do
{
   url = rand_url();
   h = od_group_join(gr, url);
} while (h == -1);

我会重命名您的方法和变量,但是很难从当前名称中猜测出合理的含义。我强烈建议您使用更有意义的名称,并遵循 .NET 命名约定。

于 2012-12-05T10:45:01.903 回答
1
int h;
string url;
do
{
    url = rand_url();
    h = od_group_join(gr, url);
}
while(h == -1);
于 2012-12-05T10:45:28.673 回答
0

您可以使用递归方法调用。将那部分代码移动到一个操作中(我将它命名为SubOperation

static void SubOperation()
{
    string url = rand_url();
    int h = od_group_join(gr, url);
    if (h == -1)
    {
        // How to return to this line string url = rand_url(); ???
        SubOperation();
    }
    else
        .........
}

然后修改原代码改为调用新的操作?

        while (true)
        {
            lock (groupslocker)
            {
                if (groups.Count == 0)
                    break;
                else
                    gr = groups.Dequeue();
            }
            SubOperation();
        }
于 2012-12-05T10:54:32.063 回答
0
while (true)
{
    lock (groupslocker)
    {
        if (groups.Count == 0)
            break;
        else
            gr = groups.Dequeue();
    }
    string url = rand_url();
    int h = od_group_join(gr, url);
    while (h == -1)
    {
        url = rand_url();
        h = od_group_join(gr, url);
    }

     .........
}
于 2012-12-05T10:45:47.413 回答