有两种情况我正在使用作家。
private void downloadFile(Int32 fileNr)
{
FileStream writer = null;
m_currentFileSize = 0;
fireEventFromBgw(Event.FileDownloadAttempting);
FileInfo file = this.Files[fileNr];
FileInfo fileFirstDateTime = this.Files[0];
FileInfo fileLastDateTime = this.Files[19];
Int64 size = 0;
Byte[] readBytes = new Byte[this.PackageSize];
Int32 currentPackageSize;
System.Diagnostics.Stopwatch speedTimer = new System.Diagnostics.Stopwatch();
Int32 readings = 0;
Exception exc = null;
if (form1.checkBox1.Text == "Download satellite images")
{
LocalDirectorySettings(file, writer);
}
else
{
writer = new FileStream(this.LocalDirectory + "\\" + file.Name, System.IO.FileMode.Create);
}
HttpWebRequest webReq;
HttpWebResponse webResp = null;
try
{
webReq = (HttpWebRequest)System.Net.WebRequest.Create(this.Files[fileNr].Path);
webResp = (HttpWebResponse)webReq.GetResponse();
size = webResp.ContentLength;
}
catch (Exception ex) { exc = ex; }
m_currentFileSize = size;
fireEventFromBgw(Event.FileDownloadStarted);
if (exc != null)
{
bgwDownloader.ReportProgress((Int32)InvokeType.FileDownloadFailedRaiser, exc);
}
else
{
m_currentFileProgress = 0;
while (m_currentFileProgress < size && !bgwDownloader.CancellationPending)
{
while (this.IsPaused) { System.Threading.Thread.Sleep(100); }
speedTimer.Start();
currentPackageSize = webResp.GetResponseStream().Read(readBytes, 0, this.PackageSize);
m_currentFileProgress += currentPackageSize;
m_totalProgress += currentPackageSize;
fireEventFromBgw(Event.ProgressChanged);
try
{
writer.Write(readBytes, 0, currentPackageSize);
}
catch(Exception eee)
{
string myeee = eee.ToString();
}
readings += 1;
if (readings >= this.StopWatchCyclesAmount)
{
m_currentSpeed = (Int32)(this.PackageSize * StopWatchCyclesAmount * 1000 / (speedTimer.ElapsedMilliseconds + 1));
speedTimer.Reset();
readings = 0;
}
}
speedTimer.Stop();
writer.Close();
webResp.Close();
if (!bgwDownloader.CancellationPending) { fireEventFromBgw(Event.FileDownloadSucceeded); }
}
fireEventFromBgw(Event.FileDownloadStopped);
}
当在这部分中,如果它执行了其他部分,它的工作正常:
if (form1.checkBox1.Text == "Download satellite images")
{
LocalDirectorySettings(file, writer);
}
else
{
writer = new FileStream(this.LocalDirectory + "\\" + file.Name, System.IO.FileMode.Create);
}
如果它正在行:没关系:
writer = new FileStream(this.LocalDirectory + "\\" + file.Name, System.IO.FileMode.Create);
但是一旦我改变了复选框,它就完成了IF的第一部分:
LocalDirectorySettings(file, writer);
这是LocalDirectorySettings方法代码:
public void LocalDirectorySettings(FileInfo file, FileStream writer)
{
try
{
var startIndex = file.Path.IndexOf("region=") + "region=".Length;
var length = file.Path.IndexOf("&", startIndex) - startIndex;
var code = file.Path.Substring(startIndex, length);
var countryName = form1.codeToFullNameMap[code];
string firstDT = ParseDateTime(this.Files[0]);
string lastDT = ParseDateTime(this.Files[19]);
string subDirectoryDT = firstDT + "---" + lastDT;
string countryPath = Path.Combine(form1.mainPath, countryName);
LocalDirectory = countryPath + "\\" + subDirectoryDT;
if (!Directory.Exists(LocalDirectory))
{
Directory.CreateDirectory(LocalDirectory);
}
string fileName = this.LocalDirectory + "\\" + "test.png"; //file.Name + "---" + countFilesNames++.ToString("D6") + ".png";
writer = new FileStream(fileName, System.IO.FileMode.Create); // Set a breakpoint here.
}
catch (Exception err)
{
string ggg = err.ToString();
}
}
即使在LocalDirectorySettings方法中有一行包含writer的实例:
writer = new FileStream(fileName, System.IO.FileMode.Create);
我正在使用一个断点,它确实在LocalDirectorySettings中到达这一行但是当它继续到该部分时:
try
{
writer.Write(readBytes, 0, currentPackageSize);
}
catch(Exception eee)
{
string myeee = eee.ToString();
}
对于捕获部分来说,写作者是空的。并且使用断点我看到作者是空的。
为什么当它到达LocalDirectorySettings后,编写器在它之后为空?
答案 0 :(得分:1)
在LocalDirectorySettings
中,您要为函数参数指定新的FileStream
。这不会影响downloadFile
中的writer变量。
使writer
成为一个成员变量,这样两种方法就可以共享它。
FileStream writer = null;
private void downloadFile(Int32 fileNr)
{
// Do stuff with writer
}
public void LocalDirectorySettings(FileInfo file)
{
// Do stuff with writer
}
答案 1 :(得分:1)
设置参数,writer
方法中名为LocalDirectorySettings()
的局部变量对其他局部变量(也称为writer
)没有影响在来电者中,downloadFile()
。
我强烈建议反对使用类字段来共享此对象(如另一个答案所建议的那样)。没有理由这样做会让你的课堂变得杂乱无章。相反,要么通过引用传递参数,要么更好地让LocalDirectorySettings()
方法返回对象。 E.g:
void downloadFile(int fileNr)
{
FileStream writer;
//...
if (form1.checkBox1.Text == "Download satellite images")
{
writer = LocalDirectorySettings(file);
}
else
{
writer = new FileStream(this.LocalDirectory + "\\" + file.Name, System.IO.FileMode.Create);
}
//...
}
public FileStream LocalDirectorySettings(FileInfo file)
{
try
{
//...
string fileName = this.LocalDirectory + "\\" + "test.png"; //file.Name + "---" + countFilesNames++.ToString("D6") + ".png";
return new FileStream(fileName, System.IO.FileMode.Create);
}
catch (Exception err)
{
string ggg = err.ToString();
throw; // you don't actually handle the exception, so rethrowing
// is the appropriate action here. Your only alternative
// would be to return "null" and have the caller check for
// that return value explicitly.
}
}