我如何计算百分比进度,我一直得到负数?

时间:2015-09-09 03:39:14

标签: c# .net winforms

我得到的结果始终是负数。

在新表格中,我添加了一个长变量:

long totalBytes = 0;
public static int fileuploadpercentages = 0;

然后我有这个事件:

private void videosInsertRequest_ProgressChanged(IUploadProgress obj)
{            
    stringProgressReport[1] = obj.Status.ToString();
    if (stringProgressReport[1] == "Uploading")
    {
        fileuploadpercentages = (int)Math.Round(((double)obj.BytesSent) / totalBytes * 100);
        uploadstatus = "uploading file";
    }
}

当我在线上使用断点时:

fileuploadpercentages = (int)Math.Round(((double)obj.BytesSent) / totalBytes * 100);

我看到BytesSent = 786432 然后我继续。

现在我看到BytesSent = 1572864

Next BytesSent = 2359296

问题是所有的时间总是为0

我想得到的是百分比进度1%2%3%...如果不可能跳1%2%3%然后其他跳跃但是我想获得fileuploadpercentages百分比,直到100%上传进度。

我在fileuploadpercentages中得到的负数是:-2147483648

此事件是我正在使用的youtube文件上传的一部分。 这是我对youtube的上传方法:在这个方法中,我注册到1videosInsertRequest_ProgressChanged1事件。

UserCredential uc = null;
private void UploadVideo(string FileName, string VideoTitle, string VideoDescription)
{
    try
    {
        var youtubeService = new YouTubeService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = Assembly.GetExecutingAssembly().GetName().Name
        });

        video.Snippet = new VideoSnippet();
        video.Snippet.Title = VideoTitle;
        video.Snippet.Description = VideoDescription;
        video.Snippet.Tags = new string[] { "tag1", "tag2" };
        video.Status = new VideoStatus();
        video.Status.PrivacyStatus = "public";
        using (var fileStream = new FileStream(FileName, FileMode.Open))
        {
            const int KB = 0x400;
            var minimumChunkSize = 256 * KB;

            var videosInsertRequest = youtubeService.Videos.Insert(video,
                        "snippet,status", fileStream, "video/*");
            videosInsertRequest.ProgressChanged +=videosInsertRequest_ProgressChanged;
            videosInsertRequest.ResponseReceived +=
            videosInsertRequest_ResponseReceived;
            // The default chunk size is 10MB, here will use 1MB.
            videosInsertRequest.ChunkSize = minimumChunkSize * 3;
            dt = DateTime.Now;
            videosInsertRequest.Upload();
        }
    }
    catch (Exception errors)
    {
        string errorss = errors.ToString();
    }
}

2 个答案:

答案 0 :(得分:0)

完成此事:

totalBytes = fileStream.Length;
videosInsertRequest.Upload();

答案 1 :(得分:0)

totalBytes = fileStream.Length;

之前初始化videosInsertRequest.Upload();