Ichbinalex
Erfahrenes Mitglied
Hallo,
Ich baue gerade in C# einen Downloadmanager. Nun habe ich das Problem das wenn ich zwei Threads die downloaden Parallel starten will die Exception:
{"Der Stream unterstützt keine gleichzeitigen E/A-Lese- oder Schreibvorgänge."} auslöst
Code der Downloadmethode:
Ich starte die Methode, dann in verschiedenen Threads, und dann wieder wird die Exception geschmissen. Weiß zufällig jemand wie ich das umgehen kann?
Ich baue gerade in C# einen Downloadmanager. Nun habe ich das Problem das wenn ich zwei Threads die downloaden Parallel starten will die Exception:
{"Der Stream unterstützt keine gleichzeitigen E/A-Lese- oder Schreibvorgänge."} auslöst
Code der Downloadmethode:
Code:
public static void DownloadF(object startPoint, string Url, string Path, DataGridViewProgressCell prgDownload, DataGridView view)
{
try
{
// Put the object argument into an int variable
int startPointInt = Convert.ToInt32(startPoint);
// Create a request to the file we are downloading
webRequest = (HttpWebRequest)WebRequest.Create(Url);
// Set the starting point of the request
webRequest.AddRange(startPointInt);
// Set default authentication for retrieving the file
webRequest.Credentials = CredentialCache.DefaultCredentials;
// Retrieve the response from the server
webResponse = (HttpWebResponse)webRequest.GetResponse();
// Ask the server for the file size and store it
Int64 fileSize = webResponse.ContentLength;
// Open the URL for download
strResponse = webResponse.GetResponseStream();
// Create a new file stream where we will be saving the data (local drive)
if (startPointInt == 0)
{
strLocal = new FileStream(Path, FileMode.Create, FileAccess.Write, FileShare.None);
}
else
{
strLocal = new FileStream(Path, FileMode.Append, FileAccess.Write, FileShare.None);
}
// It will store the current number of bytes we retrieved from the server
int bytesSize = 0;
// A buffer for storing and writing the data retrieved from the server
byte[] downBuffer = new byte[2048];
// Loop through the buffer until the buffer is empty
while ((bytesSize = strResponse.Read(downBuffer, 0, downBuffer.Length)) > 0) //HIER LÖST DER FEHLER AUS
{
// Write the data from the buffer to the local hard drive
strLocal.Write(downBuffer, 0, bytesSize);
// Invoke the method that updates the form's label and progress bar
view.Invoke(new UpdateProgessCallback(UpdateProgress), new object[] { strLocal.Length, fileSize + startPointInt,prgDownload });
if (goPause == true)
{
break;
}
}
}
finally
{
// When the above code has ended, close the streams
strResponse.Close();
strLocal.Close();
view.Invoke(new UpdateStatusCallback(UpdateStatus), new object[] {Status.DOWNLOADED,prgDownload,view });
}