Exception: The operation has timed-out.

N

news.microsoft.com

I experienced the following exception occasionally when the program
downloads the image from the Web site. Who can give me some solution for it?
Thanks in advance.


----------
Exception:
Message: The operation has timed-out.
Source: System
TargetSite: Int32 Read(Byte[], Int32, Int32)
StackTrace:
at System.Net.ConnectStream.Read(Byte[] buffer, Int32 offset, Int32
size)
at RTC7.ImageSystem.ViewPictureInRecord.Copy(Stream stream, String
filename)
at RTC7.ImageSystem.ViewPictureInRecord.LoadImageFromServer(String sUrl),


//-------------
// Codes:
public string LoadImageFromServer(string sUrl)
{
bool isAborted = false;
string strURL_ = sUrl; //this.GetUrlOfPicture();
string strLocalTmpFile = "";
Stream stream = null;
WebRequest webreq = null;
WebResponse webres = null;
try
{
Uri uri = new Uri(strURL_);

webreq = WebRequest.CreateDefault(uri);

webreq.Timeout = 60000;
webres = webreq.GetResponse();

if(webres != null)
stream = webres.GetResponseStream();
else
return null;
//...
}
catch(Exception ex)
{
// ...
}
finally
{
if(webres != null)
webres.Close();
if(stream != null)
stream.Close();
}
return strLocalTmpFile;
}

public bool Copy(Stream stream, string filename)
{
const int conBufferLeng = 20000;
FileStream fstream= null;
byte[] buffer = new byte[conBufferLeng];
int iByteCountInBuffer = conBufferLeng;
bool blnOkay = false;
try
{
if(stream != null)
{
if(File.Exists(filename) == false)
fstream = File.Create(filename);
else
fstream = File.OpenWrite(filename);
if(fstream != null && fstream.CanWrite == true)
{
if(stream.CanSeek == true)
stream.Position = 0;
do
{
lock(this.syncObject)
iByteCountInBuffer = stream.Read(buffer,0,conBufferLeng);
fstream.Write(buffer,0,iByteCountInBuffer);
}
while(iByteCountInBuffer > 0); //conBufferLeng);
blnOkay = true;
} // If destination is okay.
} // If source is okay.
}
finally
{
if(fstream != null)
fstream.Close();
buffer = null;
}
return blnOkay;
}
 
K

Kevin Spencer

Try again?
Show the user a message?

You have an external dependency. You are attempting to download an image
from a location you have no control over. It is a network resource. If the
operation times out, the response took longer than the 60 seconds you
instructed the WebRequest to wait for. Perhaps the image is way large.
Perhaps the server quit responding. Perhaps a packet was dropped. Maybe some
guy in a car ran into the wrong telephone pole. Who knows? It's an external
dependency. You have to handle this sort of contingency. How you handle it
is your requirement to decide.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
A watched clock never boils.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,699
Latest member
AnneRosen

Latest Threads

Top