变量(count_server)不等于它启动时的值

时间:2017-04-04 16:46:23

标签: c# asp.net variables ftp

我想在请求文件时实现增加一些权重的代码,并且当通过FTP下载文件时,应减去增加的权重。

我知道我在逻辑上错了但却找不到。

现在发生的事情是,在count_server1 / count_server2 / count-server3中还剩下一些金额,它最终没有达到0。

表示不减去分配的实际重量。这就是为什么某些值留在这些变量中的原因。

我正在使用这些变量值来设置进度条。

如果你没有得到我的问题,请评论。

protected void start_download()
{
    //Other actions
    for (int i = 1; i <= filecount; i++)
    {
        balance_load(filename, trnsfrpth);
    }
}

protected void Timer1_Tick(object sender, EventArgs e)
        {
            e1.Style.Add("width", count_server1 + "%");
            e1.InnerHtml = count_server1.ToString();
            e2.Style.Add("width", count_server2 + "%");
            e2.InnerHtml = count_server2.ToString();
            e3.Style.Add("width", count_server3 + "%");
        }

int weight;
public void count_weight()
{
    if (extension == ".jpeg" || extension == ".bmp" || extension == ".jpg" || extension == ".gif" || extension == ".png")
    {
        weight = 1;
    }
    if (extension == ".mp3")
    {
        weight = 5;
    }
    if (extension == ".mp4" || extension == ".avi" || extension == ".mov" || extension == ".mkv" || extension == ".wmv" || extension == ".flv")
    {
        weight = ((int)filesize / 2);
    }
}

int P;
public void balance_load(string filename, string trnsfrpth)
{
    count_weight();
    P = weight;

    if ((count_server1 <= count_server2) && (count_server1 <= count_server3))
    {
        //FTP request creation and providing credentials
        count_server1 += P;
        ftpClient_s1.DownloadFileCompleted += DownloadFileCompleted_s1();
        ftpClient_s1.DownloadFileAsync(new Uri(path), trnsfrpth);
    }
    else if (count_server2 <= count_server1 && count_server2 <= count_server3)
    {
        //FTP request creation and providing credentials
        count_server2 += P;
        ftpClient_s2.DownloadFileCompleted += DownloadFileCompleted_s2();
        ftpClient_s2.DownloadFileAsync(new Uri(path), trnsfrpth);
    }
    else if (count_server3 <= count_server1 && count_server3 <= count_server2)
    {
        //FTP request creation and providing credentials
        count_server3 += P;
        ftpClient_s3.DownloadFileCompleted += DownloadFileCompleted_s3();
        ftpClient_s3.DownloadFileAsync(new Uri(path), trnsfrpth);
    }
    else
    {}
}

public AsyncCompletedEventHandler DownloadFileCompleted_s1()
{
    Action<object, AsyncCompletedEventArgs> action = (sender, e) =>
    {
        count_server1 -= P;
    };
    return new AsyncCompletedEventHandler(action);
}

public AsyncCompletedEventHandler DownloadFileCompleted_s2()
{
    Action<object, AsyncCompletedEventArgs> action = (sender, e) =>
    {
        count_server2 -= P;
    };
    return new AsyncCompletedEventHandler(action);
}

public AsyncCompletedEventHandler DownloadFileCompleted_s3()
{
    Action<object, AsyncCompletedEventArgs> action = (sender, e) =>
    {
        count_server3 -= P;
    };
    return new AsyncCompletedEventHandler(action);
}

2 个答案:

答案 0 :(得分:0)

我会使用第三方库来执行FTP作业,因为它可以很好地处理进度和多线程。见http://www.componentpro.com/ftp.net/

答案 1 :(得分:0)

好的,我找到了解决方案。

而是直接在&#34; Action&#34;中分配值并将其直接使用,可以先赋值,然后再使用它。因为问题可能与价值重写有关。

所以, 而不是这个

public AsyncCompletedEventHandler DownloadFileCompleted_s1()
{
    Action<object, AsyncCompletedEventArgs> action = (sender, e) =>
    {
        count_server1 -= P;
    };
    return new AsyncCompletedEventHandler(action);
}

我将代码更改为:

public AsyncCompletedEventHandler DownloadFileCompleted_s1()
{
    int P_temp=P;
    Action<object, AsyncCompletedEventArgs> action = (sender, e) =>
    {
        count_server1 -= P_temp;
    };
    return new AsyncCompletedEventHandler(action);
}

同样的事情

AsyncCompletedEventHandler DownloadFileCompleted_s2()
AsyncCompletedEventHandler DownloadFileCompleted_s3()