performance degradation after 30-40 mins c# - c#

I'm trying to download files from a remote location. But right before the download, I get my file locations from a web service, also on a remote location.
The thing is, I get a degrading performance over time. The downloaded file numbers decrease from around 2k in 3 minutes to 300-400 in the same time after an hour or two and I have 250k files.
Is the service or the download a problem? Or both?
I download files as below after I get the names from the service,
try
{
using (WebClient client = new WebClient())
{
if (File.Exists(filePath + "/" + fileName + "." + ext))
{
return "File Exists: " + filePath + "/" + fileName + "." + ext;
}
client.DownloadFile(virtualPath, filePath + "/" + fileName + "." + ext);
return "Downloaded: " + filePath + "/" + fileName + "." + ext;
}
}
catch (Exception e) {
return"Problem Downloading " + fileName + ": " + e.Message;
}

if (File.Exists(filePath + "/" + fileName + "." + ext))
Bottleneck is probably here.
When you get hella tons of files in a single folder checking if file with this name already exists might need some time to complete.
So you might want to store files in different folders

The problem was the information put on the richTextBox and label.
The rtb was appended with info regarding whatever happened to each individual element. The Label showed at which element we were. Apparently the cpu can't handle it and this becomes a major problem when running for prolonged time. It ate so much cpu that it eventually killed the application. Removing or limiting their output solved almost all problems.
On the other hand, the slight degradation (1.5k to 1.2k per minute after 2.5 hours) of the download that still exists is still a mystery.

Related

Set ImageURL without knowing the file extension c#

I have a folder where images are uploaded and named 'Photo1', 'Photo2', etc depending on how many photos are uploaded. In another panel on the same aspx page I want to display the uploaded photos. I will know the filenames but the extension could be .png, .jpg, or .jpeg.
How can I set the ImageUrl path when I know only the filename and not the extension?
Here is an example of how I've attempted it so far--
ASPX Page Code:
<asp:Image ID="Image1" runat="server" />
Code Behind:
Image1.ImageUrl = Server.MapPath("~/RepairPhotos/" + order_id + "." + unit_id + ".RepairPhoto1.*");
This method says I've used an invalid character. Although, I've tested it by going into the folder and finding the extension then entering the line as:
Image1.ImageUrl = Server.MapPath("~/RepairPhotos/" + order_id + "." + unit_id + ".RepairPhoto1.jpg");
and that didn't work either, so there may be more wrong here than just the unknown character in the extensions place.
string jpg = "~/RepairPhotos/" + order_id + "." + unit_id + ".RepairPhoto1.jpg"
string png = "~/RepairPhotos/" + order_id + "." + unit_id + ".RepairPhoto1.png"
if (File.Exists(Server.MapPath(jpg)))
{
}
else if(File.Exists(Server.MapPath(png)))
{
}
If you don't have the file's extension, you may need to read the first few bytes of that file to determine if an image is in what format.
This snippet may help: https://gist.github.com/ChuckSavage/dc079e21563ba1402cf6c907d81ac1ca

File.Copy Unauthorized access C#

I've started to encounter a problem with File.Copy. This works fine for my data creation script, managing to duplicate thousands of files with no issues. My problem occurs when trying to create temp files later in my code.
I have added the code sample below that isn't working correctly. I've tried numerous different ways to try to resolve this to no avail. What I am doing is copying some user data files created in a directory on the C drive into a temp folder inside that user data folder.
Code
foreach (string originalFile in OriginalDataFileNames)
{
string tempFile = originalFile;
TempDataFiles.Add(tempFile);
Console.WriteLine("GlobalDataCtrl: Original Data File: " + XWSDataDirectory + "\\" + tempFile);
Console.WriteLine("GlobalDataCtrl: Saved Temp Data File: " + tempPath + "\\" + tempFile);
File.Copy(XWSDataDirectory + "\\" + originalFile, tempPath + "\\" + tempFile);
}
Exit Error
The program '[6256] XtremeWrestlingSim.vshost.exe' has exited with code -1073741819 (0xc0000005) 'Access violation'.
Any help is appreciated, thanks in advance!
SOLUTION:
FileStream outputFS = null;
FileStream inputFS = null;
outputFS = new FileStream(tempPath + "\\" + tempFile, FileMode.CreateNew, FileAccess.ReadWrite);
using (inputFS = new FileStream(XWSDataDirectory + "\\" + originalFile, FileMode.Open))
{
inputFS.CopyTo(outputFS);
}
outputFS.Close();
inputFS.Close();
Not sure how nicely formatted this is, but it works. Replace File.Copy with the above code.
You are using File.Create just before you call File.Copy, I think that is the issue, it is leaving an open stream.
Maybe removing the File.Create call will solve the issue. If not you could get the returned value (which is a stream) and close it before trying to copy.
The file is opened with read/write access and must be closed before it can be opened by another application.
See remarks https://msdn.microsoft.com/en-us/library/ms143361(v=vs.110).aspx

Spreadsheetlight save as working too slow

I am working on a winform with spreadsheetlight library.I want to create a excel file with specific name under a specific folder when user clicked on "Save to Excel" button. Ihave tried the below code so far.
string path = System.IO.Path.GetTempPath();
sl.SaveAs(path + "\\" + dosyaismi + ".xlsx");
FileInfo fi = new FileInfo(path + "\\" + dosyaismi + ".xlsx");
if (fi.Exists)
{
System.Diagnostics.Process.Start(#path + "\\" + dosyaismi + ".xlsx");
}
else
{
MessageBox.Show("Dosya bulunamadı!");
}
When program run and clicked that button first time it takes 2 minutes to create file.But without closing program I clicked that button again with different filename it creates file immediately.
I searched for 2 hours but ı didn't find any solution.Can anyone help what is the problem?

Batch Image Manipulation keeps hanging

I use the following code to scale and crop all images in a folder.
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(file);
string fileExtension = Path.GetExtension(file);
string filePath = Path.GetDirectoryName(file);
string newFileName = string.Empty;
long fileSize = new FileInfo(file).Length;
if (fileSize > fileSizeLimit)
{
string tempFile = System.IO.Path.GetTempFileName();
File.Copy(file, tempFile, true);
Bitmap sourceImage = (Bitmap)System.Drawing.Image.FromFile(tempFile);
System.Drawing.Image imgPhoto = ScaleCrop(sourceImage, sourceImage.Width / 4, sourceImage.Height / 4, AnchorPosition.Top);
Bitmap bitImage = new Bitmap(imgPhoto);
File.Delete(file);
newFileName = filePath + "\\" + fileNameWithoutExtension + "_" + DateTime.Now.ToString("yyyyMMddHHmmss") + "_" + CoilWarehouseProcessed + fileExtension;
bitImage.Save(newFileName, System.Drawing.Imaging.ImageFormat.Jpeg);
imgPhoto.Dispose();
bitImage.Dispose();
}
If I run the application locally (in debug mode in VS2010) and point it to a network drive then all images are processed every time.
If I run it from a our local webserver the problem is that the app may process no images, it may process 5, it may process 1, it never does all of the images in a given folder, only ever some of them... then it hangs in the clients browser.
There are no events to view via the event log... the application does not crash or error in anyway... the fact that it will process an image proves it's not a permissions issue.
Any ideas why this is happening?
EDIT: Thanks to wazdev, but I ended up testing a less intrusive (and also don't like dependencies relying on 3rd party software) solution, and it all seems good so far... Basically I changed it so that when it copies the stream to produce a new image 'System.Drawing.Image imgPhoto = ...' to use a using statement to ensure that the 'temp' image is disposed of. I also moved the delete of the original (uncropped / unscaled image) file to be the last operation (In tests it has worked fine, only time will tell once more users come online and concurrency is tested):
string tempFile = System.IO.Path.GetTempFileName();
File.Copy(file, tempFile, true);
Bitmap sourceImage = (Bitmap)System.Drawing.Image.FromFile(tempFile);
System.Drawing.Image imgPhoto = ScaleCrop(sourceImage, sourceImage.Width / 4, sourceImage.Height / 4, AnchorPosition.Top);
Bitmap bitImage;
using (var bmpTemp = new Bitmap(imgPhoto))
{
bitImage = new Bitmap(bmpTemp);
}
newFileName = filePath + "\\" + fileNameWithoutExtension + "_" + DateTime.Now.ToString("yyyyMMddHHmmss") + "_" + CoilWarehouseProcessed + fileExtension;
bitImage.Save(newFileName, System.Drawing.Imaging.ImageFormat.Jpeg);
imgPhoto.Dispose();
bitImage.Dispose();
File.Delete(file);
EDIT2: It's been live now for a few days and i've tested it every day and it is working well.. Here's all that I did;
Basically inside the ScaleCrop() call there was a GC.Collect and a Wait For Pending Finalisers() call. I removed the wait for pending call and moved the GC.Collect() to after the File.Delete().
I've seen this behaviour in the past when RAM was exhausted resulting in paging to disk. I've found a great deal of success in utilising the ImageResizing.net libraries:
http://imageresizing.net/
I updated my code to use this library and have never looked back.
HTH
(I have no affiliation with imageresizing.net - I'm just a very happy user)

Asp.net finding and downloading file

I have File Upload form which users use to upload certain files on the server. The files are uploaded in path that look like this.
"G:\\VS\\Ticketing System2\\UploadedFiles\\" + ProjectId + "\\" + ticketId + "\\TicketFiles\\";
After that I have a repeater which displays some data and have a hyperlink. I want to name the hyperlink"Download files(" + fileCount + ")" and onclicked it should display a regular Save As window. Can you give me some code to do that. I've never done something like this.
To get the number of files in the directory, you need to use IO. Then
string path = #"G:\\VS\\Ticketing System2\\UploadedFiles\\" + ProjectId + "\\" + ticketId + "\\TicketFiles\\";
int numFiles = Directory.GetFiles(path).Length;
Then in your page_load, just change to .Text of the link to include the numFiles variable

Categories

Resources