Universal App hangs at CreateFileAsync (possible deadlock?) - c#

I'm making a program that capture photo from camera and perform some actions with it. So I have the following code (handler for camera button and "some actions"):
private async void btnCamera_Click(object sender, RoutedEventArgs e)
{
btnCamera.IsEnabled = false;
var imgFormat = ImageEncodingProperties.CreateJpeg();
var ocrFolder = Windows.Storage.ApplicationData.Current.TemporaryFolder;
var imgFileRaw = await ocrFolder.CreateFileAsync("ocr_raw.jpg", CreationCollisionOption.ReplaceExisting);
var imgFileFull = await ocrFolder.CreateFileAsync("ocr_full.jpg", CreationCollisionOption.ReplaceExisting);
var imgFilePart = await ocrFolder.CreateFileAsync("ocr_part.jpg", CreationCollisionOption.ReplaceExisting);
await camera.CapturePhotoToStorageFileAsync(imgFormat, imgFileRaw);
await camera.StopPreviewAsync();
await SaveImageEx(imgFileRaw, imgFileFull, false);
await SaveImageEx(imgFileRaw, imgFilePart, true);
this.Frame.GoBack();
}
private async Task SaveImageEx(StorageFile SourceFile, StorageFile TargetFile, bool Partial)
{
using (var sourceStream = await SourceFile.OpenAsync(FileAccessMode.Read))
using (var targetStream = await TargetFile.OpenAsync(FileAccessMode.ReadWrite))
{
var decoder = await BitmapDecoder.CreateAsync(BitmapDecoder.JpegDecoderId, sourceStream);
var encoder = await BitmapEncoder.CreateForTranscodingAsync(targetStream, decoder);
bool rotated90Degrees = false;
switch (previewOrientation)
{
case DisplayOrientations.Portrait:
{
encoder.BitmapTransform.Rotation = BitmapRotation.Clockwise90Degrees;
rotated90Degrees = true;
break;
}
case DisplayOrientations.PortraitFlipped:
{
encoder.BitmapTransform.Rotation = BitmapRotation.Clockwise270Degrees;
rotated90Degrees = true;
break;
}
case DisplayOrientations.LandscapeFlipped:
{
encoder.BitmapTransform.Rotation = BitmapRotation.Clockwise180Degrees;
break;
}
case DisplayOrientations.Landscape:
case DisplayOrientations.None:
default:
{
encoder.BitmapTransform.Rotation = BitmapRotation.None;
break;
}
}
if (Partial)
{
encoder.BitmapTransform.Bounds = rotated90Degrees ?
RecognitionArea1.GetImageRegion(decoder.PixelHeight, decoder.PixelWidth) :
RecognitionArea1.GetImageRegion(decoder.PixelWidth, decoder.PixelHeight);
}
await encoder.FlushAsync();
}
}
The problem is that 1 of 5-10 times camera button clicked code hangs at line:
var imgFileRaw = await ocrFolder.CreateFileAsync("ocr_raw.jpg",
CreationCollisionOption.ReplaceExisting);
Please, somebody explain me what is so wrong with my code? I've read lots of examples how to avoid deadlocks with async/await operations and I still don't know where the problem is.
UPDATE. It seems without calling "SaveImageEx" there is no hanging, still don't see anything dangerous inside this method.
UPDATE2. I made minimal hanging example with the code which I'm really using in the app. Just click camera button for several times and you will see deadlock on "CreateFileAsync":
HangSample (Visual Studio 2013 solution)

Related

Compulsary requirement to take and store users photo using Xam.Plugin.Media

I have a PCL type app and I'm using the Xam.Plugin.Media plugin. I need it to ensure a user submits a photo from the camera before they can continue.
To do this I show the camera page from a button click event and I want to ensure that in case the user cancels out of this that the app launches the camera again, this would repeat until a photograph is stored.
Currenty my app falls in the onActivityResumed method of the MainApplication file when the user cancels out of the camera
Attached photo of my code, My code.
private async void TakePicture()
{
await CrossMedia.Current.Initialize();
if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
{
await App.Current.MainPage.DisplayAlert("No Camera", ":( No camera available.", "Aceptar");
}
file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
{
Directory = "Sample",
Name = "test.jpg",
PhotoSize = PhotoSize.Small,
});
//IsRunning = true;
if (file != null)
{
ImageSource = ImageSource.FromStream(() =>
{
var stream = file.GetStream();
return stream;
});
}
IsRunning = false;
}
Aside from the fact that it is usually a bit of a UX issue to force a user into anything nowadays, the question still has some merits.
this is the approach that I would consider, it involves recursion.
private async void TakePicture()
{
await CrossMedia.Current.Initialize();
if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
{
await App.Current.MainPage.DisplayAlert("No Camera", ":( No camera available.", "Aceptar");
}
file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
{
Directory = "Sample",
Name = "test.jpg",
PhotoSize = PhotoSize.Small,
});
//IsRunning = true;
if (file != null)
{
ImageSource = ImageSource.FromStream(() =>
{
var stream = file.GetStream();
return stream;
});
}
else
{
// Recursion - I believe that this would continue until the file is not null, then it would carry on.
TakePicture();
}
IsRunning = false;
}
I can't say I use recursion that often, but I think it might do the trick here.

MediaTranscoder.PrepareFileTranscodeAsync UnauthorizedAccessException

I have a Button on a Page with the following method on the click event of the button:
StorageFile _sourceFile;
private string _sourceToken;
private async void btnSelect_Click(object sender, RoutedEventArgs e)
{
FileOpenPicker fop = new FileOpenPicker();
fop.FileTypeFilter.Add(".mp4");
StorageFile inFile = await fop.PickSingleFileAsync();
_sourceToken = Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.Add(inFile);
_sourceFile = await Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.GetFileAsync(_sourceToken);
mediaElement.AutoPlay = false;
IRandomAccessStream stream = await _outFile.OpenAsync(FileAccessMode.ReadWrite);
mediaElement.SetSource(stream, _outFile.ContentType);
}
If I click play on the MediaElement the video I select plays fine.
I also have another button which has the following code on its click event:
private async void btnExport_Click(object sender, RoutedEventArgs e)
{
StorageFile outFile = await KnownFolders.VideosLibrary.CreateFileAsync("Outfie.mp4", CreationCollisionOption.ReplaceExisting);
MediaEncodingProfile profile = MediaEncodingProfile.CreateMp4(VideoEncodingQuality.HD1080p);
MediaTranscoder transcoder = new MediaTranscoder();
PrepareTranscodeResult prepareOp = await transcoder.PrepareFileTranscodeAsync(_sourceFile, outFile, profile);
if (prepareOp.CanTranscode)
{
var transcodeOp = prepareOp.TranscodeAsync();
transcodeOp.Progress += new AsyncActionProgressHandler<double>(TranscodeProgress);
transcodeOp.Completed += new AsyncActionWithProgressCompletedHandler<double>(TranscodeComplete);
}
else
{
switch (prepareOp.FailureReason)
{
case TranscodeFailureReason.CodecNotFound:
System.Diagnostics.Debug.WriteLine("Codec not found.");
break;
case TranscodeFailureReason.InvalidProfile:
System.Diagnostics.Debug.WriteLine("Invalid profile.");
break;
default:
System.Diagnostics.Debug.WriteLine("Unknown failure.");
break;
}
}
}
Unfortunately the line transcoder.PrepareFileTranscodeAsync throws an UnauthorizedAccessException. But if I use the following instead of _sourceFile it works:
StorageFile sourceFile = await KnownFolders.VideosLibrary.GetFileAsync("sourceFile.mp4");
The error being thrown is:
System.UnauthorizedAccessException: 'Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))'
To be clear, I am selecting files OUTSIDE the KnownFolders Enumeration, hence I am using Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.
Can anyone explain why?
EDIT:
If I change the source file to be the result of a FileOpenPicker then it works. So it begs the question, why is the FutureAccessList not working??
private async void btnExport_Click(object sender, RoutedEventArgs e)
{
StorageFile outFile = await KnownFolders.VideosLibrary.CreateFileAsync("Outfie.mp4", CreationCollisionOption.ReplaceExisting);
FileOpenPicker fop = new FileOpenPicker();
fop.SuggestedStartLocation = PickerLocationId.ComputerFolder;
fop.FileTypeFilter.Add(".mp4");
StorageFile sourceFile = await fop.PickSingleFileAsync();
MediaEncodingProfile profile = MediaEncodingProfile.CreateMp4(VideoEncodingQuality.HD1080p);
MediaTranscoder transcoder = new MediaTranscoder();
PrepareTranscodeResult prepareOp = await transcoder.PrepareFileTranscodeAsync(sourceFile, outFile, profile);
if (prepareOp.CanTranscode)
{
var transcodeOp = prepareOp.TranscodeAsync();
transcodeOp.Progress += new AsyncActionProgressHandler<double>(TranscodeProgress);
transcodeOp.Completed += new AsyncActionWithProgressCompletedHandler<double>(TranscodeComplete);
}
else
{
switch (prepareOp.FailureReason)
{
case TranscodeFailureReason.CodecNotFound:
System.Diagnostics.Debug.WriteLine("Codec not found.");
break;
case TranscodeFailureReason.InvalidProfile:
System.Diagnostics.Debug.WriteLine("Invalid profile.");
break;
default:
System.Diagnostics.Debug.WriteLine("Unknown failure.");
break;
}
}
}
Do you have access to the file you're trying to write to? Maybe it's read-only or created by another user other than yourself? (Right click + Properties on the file in Explorer should give you a clearer picture of the file permissions)
Also, you might get that exception if you're trying to write to a folder whom you don't have access to.
Check your credentials, I would guess it's something related to that.
So it seems the fact I was opening the source file in ReadWrite mode
IRandomAccessStream stream = await _outFile.OpenAsync(FileAccessMode.ReadWrite);
Was the cause of the issues. According to this page
Use read/write mode only when you're ready to write immediately in order to avoid conflicts with other operations.
So I changed to this and all works well
IRandomAccessStream stream = await _outFile.OpenAsync(FileAccessMode.Read);

C# Windows 8.1 Background Task that runs while the device screen is locked

I've done a lot of work over the past week, working with creating background task for a windows universal project. I've having a hard, and starting believe it is not possible triggering a background task to run when the device screen is locked. I'm using SystemTriggerType.Useraway to trigger the background task. I'll post what i've got so far. Any help with this would be awesome!
Here's how I am registering the background task
public static void RegisterTask()
{
try
{
var taskRegistered = false;
var builder = new BackgroundTaskBuilder();
foreach (var task in BackgroundTaskRegistration.AllTasks)
{
if (task.Value.Name == "ResponderBackgroundTask")
{
Debug.WriteLine(task.Value.Name + " Task Already Registered!!");
taskRegistered = true;
break;
}
}
if (!taskRegistered)
{
builder.Name = "ResponderBackgroundTask";
builder.TaskEntryPoint = "BackgroundGps.BackgroundTask";
builder.AddCondition(new SystemCondition(SystemConditionType.UserNotPresent));
builder.AddCondition(new SystemCondition(SystemConditionType.InternetAvailable));
builder.SetTrigger(new SystemTrigger(SystemTriggerType.UserAway, false));
builder.CancelOnConditionLoss = true;
var register = builder.Register();
register.Completed += new BackgroundTaskCompletedEventHandler(OnComplete);
}
}
catch (Exception ex)
{
Debug.WriteLine(ex.ToString());
throw;
}
}
Here is the backgroundtask:
async public void Run(IBackgroundTaskInstance taskInstance)
{
taskInstance.Canceled += new BackgroundTaskCanceledEventHandler(OnCanceled);
Debug.WriteLine("Inside Run.......");
BackgroundTaskDeferral deferral = taskInstance.GetDeferral();
await Geolocate();
deferral.Complete();
}
async static Task<HttpResponseMessage> Geolocate()
{
Debug.WriteLine("Inside Async Geolocate");
HttpResponseMessage response = new HttpResponseMessage();
Geolocator geolocator = new Geolocator();
geolocator.DesiredAccuracy = (PositionAccuracy) 20;
geolocator.DesiredAccuracyInMeters = 30;
var networkStatus = NetworkInformation.GetInternetConnectionProfile();
bool status = true;
while (status)
{
networkStatus = NetworkInformation.GetInternetConnectionProfile();
Geoposition position = await geolocator.GetGeopositionAsync().AsTask();
var latitude = position.Coordinate.Point.Position.Latitude;
var longitude = position.Coordinate.Point.Position.Longitude;
HttpClient client = new HttpClient();
response = await client.GetAsync("http://www.mylocation.com/location?=latitude&longitude");
Debug.WriteLine(response);
if (networkStatus == null)
{
status = false;
}
if (cancelRequest == true)
{
return response;
}
await Task.Delay(15000);
}
return response;
}
I had some issues with background task in UWP project when I've used it for push notifications.
The main issue with UWP BackgroundTasks is that they suppose to be very light and consume not much of CPU time, otherwise the OS shuts it down.
My problem was, that I've tried to access the local database by using very heavy service, which took CPU time and were shutdown by the OS. My logs were cut in the middle of a line, cause my logger won't be fast enough to write the message.
Try to put some logs in your BackgroundTask inorder to see if he raised by the trigger, and look for a heavy operation that can cause it to be canceled.
Web requests can be also the problem...

Handle code after task are done

I'm trying to show a waiting symbol while while a ASYNC task are doing.
I'm really new to this, so if there are better ways to implement this code, please enlighten me :)
But, everything works except the hiding of the pictureBox1 after the code are done and there are now result found. In other words, when there are a result, the pictureBox1 are hidden
Here are the method that runs every time a outlook item are opened
private void FormRegion1_FormRegionShowing(object sender, System.EventArgs e)
{
if (this.OutlookItem is Microsoft.Office.Interop.Outlook.MailItem)
{
Microsoft.Office.Interop.Outlook.MailItem item = (Microsoft.Office.Interop.Outlook.MailItem)this.OutlookItem;
getContactByEmail(item);
}
}
This is the method that I implement the wait stuff
public async Task getContactByEmail(Microsoft.Office.Interop.Outlook.MailItem item)
{
pictureBox1.Visible = true;
using (var client = new System.Net.Http.HttpClient())
{
client.BaseAddress = new Uri("http://api.....");
client.DefaultRequestHeaders.Accept.Clear();
HttpResponseMessage response = await client.GetAsync("tools/getContactByEmail?email=" + item.SenderEmailAddress + "&key=1232");
if (response.IsSuccessStatusCode)
{
SimpleContact contact = await response.Content.ReadAsAsync<SimpleContact>();
lblName.Text = contact.Name;
lblMobile.Text = contact.Phone;
}
pictureBox1.Visible = false;
}
}
Posting the code that fixes this so the exception are not raised
if (response.IsSuccessStatusCode)
{
SimpleContact contact = await response.Content.ReadAsAsync<SimpleContact>();
if (contact != null)
{
lblName.Text = contact.Name;
lblMobile.Text = contact.Phone;
}
pictureBox1.Visible = false;
}
In C# method names are always CamelCase and asynchronous methods are always suffixed Async. Just conventions.
You might want to extract the non UI code to another asynchronous method to avoid going back and forth to the UI thread:
private async void FormRegion1_FormRegionShowing(object sender, System.EventArgs e)
{
if (this.OutlookItem is Microsoft.Office.Interop.Outlook.MailItem)
{
Microsoft.Office.Interop.Outlook.MailItem item = (Microsoft.Office.Interop.Outlook.MailItem)this.OutlookItem;
pictureBox1.Visible = true;
var contact = GetContactByEmailAsync(item);
if (contact != null)
{
lblName.Text = contact.Name;
lblMobile.Text = contact.Phone;
}
pictureBox1.Visible = false;
}
}
public async Task<SimpleContact> GetContactByEmailAsync(Microsoft.Office.Interop.Outlook.MailItem item)
{
using (var client = new System.Net.Http.HttpClient())
{
client.BaseAddress = new Uri("http://api.....");
client.DefaultRequestHeaders.Accept.Clear();
HttpResponseMessage response = await client.GetAsync(
"tools/getContactByEmail?email=" + item.SenderEmailAddress + "&key=1232")
.ConfigureAwait(false);
return (response.IsSuccessStatusCode)
? await response.Content.ReadAsAsync<SimpleContact>();
: null;
}
}
Note: Don't forget proper exception handling!!!

Windows 8.1 share doesn't work when Print Screen is added

I'm trying to implement some logical to share the image of my webView and some extras informations. If I do that without capture screen, it works perfectly:
private async void OnDataRequested(DataTransferManager sender, DataRequestedEventArgs e)
{
if (await GetShareContent(e.Request))
{
if (String.IsNullOrEmpty(e.Request.Data.Properties.Title))
{
e.Request.FailWithDisplayText("Nenhum título adicionado");
}
}
}
private async Task<bool> GetShareContent(DataRequest request, StorageFile file)
{
bool succeeded = false;
string text = "Dados do Arquivo:" + Environment.NewLine + webViewModel.Name;
string dataPackageText = text;
if (!String.IsNullOrEmpty(dataPackageText))
{
DataPackage requestData = request.Data;
requestData.Properties.Title = "Target";
requestData.Properties.Description = webViewModel.Name;
requestData.SetText(dataPackageText);
succeeded = true;
}
else
{
request.FailWithDisplayText("Não há nada para compartilhar");
}
return succeeded;
}
But, if I try the same thing justing adding the captured image, it doesn't work, doesn't show any Excepetion, just the message: "Não há nada para compartilhar agora" (There's nothing to share right now)
I don't know what is going on. The code that doesn't work:
private async void OnDataRequested(DataTransferManager sender, DataRequestedEventArgs e)
{
StorageFile file = await captureScreen();
if (await GetShareContent(e.Request, file))
{
if (String.IsNullOrEmpty(e.Request.Data.Properties.Title))
{
e.Request.FailWithDisplayText("Nenhum título adicionado");
}
}
}
private async Task<StorageFile> captureScreen()
{
RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap();
await renderTargetBitmap.RenderAsync(webView, (int)webView.Width, (int)webView.Height);
Image myImage = new Image();
myImage.Source = renderTargetBitmap;
var file = await App.rootDir.CreateFileAsync("screenCapture.jpg", CreationCollisionOption.ReplaceExisting);
var pixels = await renderTargetBitmap.GetPixelsAsync();
using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.ReadWrite))
{
var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream);
byte[] bytes = pixels.ToArray();
encoder.SetPixelData(BitmapPixelFormat.Bgra8,
BitmapAlphaMode.Ignore,
(uint)webView.Width, (uint)webView.Height,
96, 96, bytes);
await encoder.FlushAsync();
}
return file;
}
I though it could be happening because the image was not read when the share is called, but I'm using await as it should be. And my jpeg is created perfectly.
The OnDataRequested callback needs to take a deferral, using DataRequest.GetDeferral, when calling asynchronous APIs.
private async void OnDataRequested(DataTransferManager sender, DataRequestedEventArgs e)
{
DataRequestDeferral deferral = e.Request.GetDeferral();
// Code to do screen capture...
deferral.Complete();
}
But, per MSDN, "[the share operation] function must return a DataPackage object within 200ms to prevent the operation from timing out". It is definitely possible for the screen capture to take longer than 200 ms. Use the DataPackage.SetDataProvider for operations that may take longer such as screen capture.
private void OnDataRequested(DataTransferManager sender, DataRequestedEventArgs e)
{
DataPackage requestData = e.request.Data;
requestData.Properties.Title = "Target";
requestData.Properties.Description = webViewModel.Name;
// Set up the data provider for a long running share operation...
requestData.SetDataProvider(StandardDataFormats.Bitmap, OnDeferredRequestedHandler);
}
private async void OnDeferredRequestedHandler(DataProviderRequest providerRequest)
{
// Again, get a deferral as an asynchronous method is called
DataProviderDeferral deferral = providerRequest.GetDeferral();
// Code to do screen capture...
deferral.Complete();
}
The Share content source sample on MSDN shows how this can be performed in full detail.

Categories

Resources