HI guys,
I have a problem with my app. Users take a lot of picture for a job about 3000 pictures and when the job is finished, they send all images to the server. I send all picture across WebAPI.
The app crashes after about 400 pictures and I can't understand why.
Basically I have a list with the picture to send and with a foreach
I call the function that sends an image. I tried to use SemaphoreSlim
but it doesn't useful for this case.
Answers
There is probably a problem with your code.
Probably out of memory exception (app or server). And also sending 3000 pictures is a lot of web api load (and bandwidth). You need to have few things in your app to make this work correctly.
Hey,
How about another approach? Why not use dropbox?
Here is a good post on how to use the dropbox in Xamarin:
http://xamariniac.hlinteractive.se/index.php/2016/10/02/xamarin-forms-dropbox-integration-app-folder/
Thanks @nadjib
I have a list of files to send from my device to my server.
For that I want to create a list of tasks: for each file I should invoke a function that sends to my webapi that file via
PUT
In a function I have a
foreach
that callsUploadPhoto
. I think there are too many tasks at the same time then I want to send a file, wait the result from the webapi and then send next file and so on.Show me your foreach loop?
And also the part the get bytes from a file
Also it will be a (lot) better idea to send Stream instead of byte[]. The reason is that when you use byte[] to load a file content, all the file content will be loaded to memory, while Stream will load into memory only small chunk of the file in a reusable buffer. Think of it like you're streaming to your web server.
This article is interested but unfortunately I can't use Dropbox in my case: all pictures are protected by privacy and I don't want to have copies of them somewhere else apart from my server.
Ah I see, Hmm how about ftp?
Check out this discussion:
https://forums.xamarin.com/discussion/90148/upload-image-to-a-ftp-server-using-pcl-xamarin-forms
Everything starts from
Send
that receives the list ofImages
(Images
is a model with info about each image).picture.IsExist
andpicture.GetImageAsByte
check\read directly in the file system the image (or file). The procedure is working well if you send few images (less then 400)Ok, interesting too. Do you know how many images can it send?
Yeah. The problem is you're having too much tasks executed at the same time. And there's a little bit of overcomplication in code, like:
You could simplify it to:
tasks.Add(SendPictureIfExists(....));
or:
tasks = list.Select(img=>SendPictureIfExists(img.PropertyId, img.FileName, img.Section)).ToList();
Perhaps you should batch the tasks by 3 a time:
Thanks @nadjib I'm trying...
Now the app sends images...
One thing I don't understand is why a
ThreadPool
starts or end randomly. Now when the app sent 380 pictures it seems blocks.I changed the code, now it is
With this code I can send 481 pictures...
We need to get the exception. Can you manage to get the exception?
Also you could perhaps assign null to bytes[] variables after you finish uploading, then each 20 pictures uploads or so call GC.Collect()
No really or I don't know what you mean
With this code I can send 509