2012-02-20 96 views
1

我需要在Windows應用程序中實現帶寬限制功能。有對SO兩個線程:C#窗口應用程序中的帶寬限制

不過,這是Web應用程序。我需要它的Windows應用程序。 如何在Windows中實現它? 我可以使用上面提到的Windows應用程序鏈接嗎?

這裏是我使用的代碼:

// Apply bandwidth control 
int uploadLimit = GlobalClass.GetFileUploadLimit(); 

if (uploadLimit > 0) 
{ 
    long bps = uploadLimit * 1024; 
    const int BufferSize = 8192; 
    MemoryStream mstream = new MemoryStream();//Stream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read, BufferSize); 

    // Openup source stream. 
    using (FileStream sourceStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read, BufferSize)) 
    { 
    // Create throttled destination stream. 
    ThrottledStream destinationStream = new ThrottledStream(mstream, bps); 
    byte[] buffer = new byte[BufferSize]; 
    int readCount = sourceStream.Read(buffer, 0, BufferSize); 

    while (readCount > 0) 
    { 
     destinationStream.Write(buffer, 0, readCount); 
     readCount = sourceStream.Read(buffer, 0, BufferSize); 
     client.FileUpload(Convert.ToInt16(userId), System.IO.Path.GetFileName(fileName), buffer); 
     //Webservice: Here is the problem 
    } 
    } 
} 

在上面的代碼,有我使用上傳文件的Web服務。此Web服務一次獲取整個文件的字節數。所以在這種情況下,我不能上傳大塊文件。任何人都可以建議我以某種方式來完成這個任務嗎?或者我可以在服務中進行更改以接受塊中的數據嗎?

+1

您發佈的第二個鏈接有一個指向ThrolledStream示例的鏈接。這應該適用於您的Windows應用程序。 – akhisp 2012-02-20 06:47:30

+0

我從鏈接中使用了相同的限制類。請參閱上面的代碼。 – 2012-02-20 06:54:03

+0

您沒有編寫上傳的代碼,顯然沒有什麼可以改變它的工作方式。 – 2012-02-20 11:03:05

回答

0

是的,您可以在WinForms/WPF應用程序中使用ThrottledStream。

+1

檢查我的問題,並請檢查我能做些什麼? – 2012-02-20 06:54:32