PhotoCamera API: how to properly dispose it? - c#

I'm using the PhotoCamera API to build a QR code scanning page (using ZXing). However, this page is only a small part of the app, and therefore it is not always shown. Thus, the application navigates between this page and some other pages with common controls.
The issue is that sometimes, after a scan, the whole application is slowed down to 30fps instead of 60fps with no real reason. I suspect that the camera is still running in the background, and the frame sync locks the app to 30fps, which is my issue: how to properly dispose of a page that uses the PhotoCamera API?
My XAML:
<Grid Background="Black">
<ProgressBar x:Name="PBar" IsIndeterminate="True" VerticalAlignment="Center" />
<Rectangle x:Name="ScanRect">
<Rectangle.Fill>
<VideoBrush x:Name="ScanVideoBrush" />
</Rectangle.Fill>
</Rectangle>
</Grid>
My C# to stop the scan process:
private void StopScan() {
if (analysisTimer != null) {
analysisTimer.Stop();
analysisTimer = null;
}
if (focusTimer != null) {
focusTimer.Stop();
focusTimer = null;
}
if (camera != null) {
camera.Dispose();
camera.Initialized -= OnCameraInitialized;
camera = null;
}
// Following two lines are a try to dispose stuff
// as much as possible, but the app still lags
// sometimes after a scan...
ScanVideoBrush.SetSource(new MediaElement());
ScanRect.Fill = new SolidColorBrush(Colors.Black);
}
Note: I'm testing the app on a Lumia 920.

Calling cam.Dispose(); should dispose the image source stream and free resources used by the Camera object, so that's fine.
Are you sure you are releasing memory, i.e unsubscribing from the PhotoCamera class Events?
When is your StopScan method called? A good practice could be to call it in OnNavigatingFrom of the PhoneApplicationPage.
Here is the code sample from MSDN that disposes the PhotoCamera:
protected override void OnNavigatingFrom(System.Windows.Navigation.NavigatingCancelEventArgs e)
{
if (cam != null)
{
// Dispose camera to minimize power consumption and to expedite shutdown.
cam.Dispose();
// Release memory, ensure garbage collection.
cam.Initialized -= cam_Initialized;
cam.CaptureCompleted -= cam_CaptureCompleted;
cam.CaptureImageAvailable -= cam_CaptureImageAvailable;
cam.CaptureThumbnailAvailable -= cam_CaptureThumbnailAvailable;
cam.AutoFocusCompleted -= cam_AutoFocusCompleted;
CameraButtons.ShutterKeyHalfPressed -= OnButtonHalfPress;
CameraButtons.ShutterKeyPressed -= OnButtonFullPress;
CameraButtons.ShutterKeyReleased -= OnButtonRelease;
}
}
Source

Related

How to avoid multiple event fire when WPF page is opened again?

I have a frame and few pages in my WPF application.
My navigation is controlled by buttons. On each button I have click handler that creates new page with some parameters:
private void ButtonProductionAuto_OnClick(ref TechModbus, RoutedEventArgs e)
{
FrameMain.Content = new PageProductionAuto(someobject, this);
}
private void ButtonProductionManual_OnClick(ref TechModbus, RoutedEventArgs e)
{
FrameMain.Content = new PageProductionManual(someobject, this);
}
When I'm switching between pages - previous pages still exist in memory and they react on some custom events.
(edit)
This is my code related with events:
public PageProductionAuto(ref TechModbus modbus, MainWindow wnd)
{
// ...
wnd.KeyDown += Wnd_KeyDown;
wnd.KeyUp += Wnd_KeyUp;
m.OnReadFinished += Modbus_OnReadFinished;
// ...
}
How can I dispose these pages or how can I avoid double-fire on my events when page is opened second time?
You should unregister the events on leaving the page.
GarbageCollector will then "dispose" (it's actually not a dispose) by itsself when there are no more references on those objects(PageProductionAuto and PageProductionManual).
Quoting MS:
The reason WPF controls don't implement IDisposable is because they have nothing to dispose. They have no handle to clean up, and no unmanaged resources to release. To ensure your memory is cleaned up, just make sure nothing has a reference to the controls once you're finished with them.
Your question is incomplete. But I can answer the question about "how to avoid multiple instances" part of it. To dispose your pages, you have to detach your events, remove them from the "openedPages" collection, and dispose where possible.
List<object> openedPages = new List<object>();
private void ButtonProductionAuto_OnClick(object sender, RoutedEventArgs e)
{
var page = openedPages.FirstOrDefault(p => p.GetType().Equals(typeof(PageProductionAuto)));
if(page == null)
{
page = new PageProductionAuto(someobject, this);
opendPages.Add(page);
}
else
{
page.SetObjects(someobject, this); // create a method to set "someObject" to your page.
}
FrameMain.Content = page;
}
What I did to avoid this was I have a window with 2 frames in it. I placed one of each source XAML in each frame so frame 1 was XAML 1 and frame 2 was XAML 2. Then I just edited the visibility to collapsed and visible. Then you don't have any page changes or instances getting created. You just create the original 2 instances.

Kill my application while it's using a threaded dll call

I have a structural problem resulting in an System.ObjectDisposedException with a dll.
More specifically it's a µEye Camera driver that is advised to capture data from a camera. I got two events from this camera that are fired asynchronously:
OnFrameEvent
OnSequenceEvent
The latter tells me that my capture is beeing completed and I can continue in saving my images.
This is the code that does the work:
private void onSequenceEvent(object sender, EventArgs e)
{
uEye.Camera Camera = sender as uEye.Camera;
SequenceCount++;
Camera.Acquisition.Stop();
int s32SeqID;
statusRet = Camera.Memory.Sequence.GetLast(out s32SeqID);
Invoke((MethodInvoker)delegate ()
{
lblStatus.Text = "Save Images...";
this.pbCapture.Value = 0;
});
Rectangle src = new Rectangle();
Rectangle dst = new Rectangle();
src.X = AOI_Size.X;
src.Y = AOI_Size.Y;
src.Width = AOI_Size.Width;
src.Height = AOI_Size.Height;
dst.X = 0; dst.Y = 0; dst.Width = AOI_Size.Width; dst.Height = AOI_Size.Height;
Bitmap bitmap_source = new Bitmap(MySensorInfo.MaxSize.Width, MySensorInfo.MaxSize.Height);;
Bitmap bitmap_destination = new Bitmap(dst.Width, dst.Height);;
Graphics g = Graphics.FromImage(bitmap_destination);
for (int i = 1; i < s32SeqID; i++)
{
Camera.Memory.ToBitmap(i, out bitmap_source);
g.DrawImage(bitmap_source, dst, src, GraphicsUnit.Pixel);
bitmap_destination.Save(PathToSave + i.ToString() + ".bmp");
this?.Invoke((MethodInvoker)delegate ()
{
pbOverallProgress.Value++;
pbCapture.Value++;
});
}
bitmap_source.Dispose();
g.Dispose();
this.CloseForm?.Invoke(1);
}
However - as you may expect the Invoke call will cause the ObjectDisposedException as the "Cancel"-Button is beeing pressed:
this?.Invoke((MethodInvoker)delegate ()
{
pbOverallProgress.Value++;
pbCapture.Value++;
});
Here's the code of the Cancel-Button:
private void btn_Exit_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Do you really want to cancel?", "Abort", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
this.CloseForm?.Invoke(0);
}
}
private void UEye_Dialog_Form_CloseForm(int exitCode)
{
this?.Invoke((MethodInvoker)delegate ()
{
if (Camera != null)
{
Camera.EventFrame -= onFrameEvent;
Camera.EventSequence -= onSequenceEvent;
}
Camera?.Acquisition.Stop();
Camera = null;
ReturnCode = exitCode;
this.Close();
});
}
UEye_Dialog_Form_CloseForm(int exitCode) is a event where CloseForm is it's delegate.
I hope this was not too much information :)
This exception only occure if the image is beeing saved, not if I'm waiting for the sequence to be captured.
Sure I could pack the UI Update code within a try-catch-block or check if the forms state is Disposed/Disposing. But for my little programming skills it looks like a structural problem.
Thank you for your help :)
The tricky part is that you're doing multi-threading without synchronization.
Invoke presents one such synchronization point, which is fine. However, as you've found out, it doesn't work after the handle is disposed - this makes perfect sense; Invoke simply sends a window message to the given handle, and when the handle no longer exists there's noöne to deal with the message (not to mention what would Close (which just sends WM_CLOSE etc.) do when the window no longer exists).
Solving this is quite tricky, actually. Even if you check if the form is disposed before trying the Invoke, it might still be disposed between the check and the Invoke itself. locking would allow you to handle the synchronization, but you'd need to lock in DestroyHandle or perhaps an event like Closing - the key is to make sure that whatever signal you send is safely tied to whether Invoke is safe right now. Of course, you don't really want to use Invoke - you need BeginInvoke, otherwise you're guaranteed to have a deadlock when Invoke needs to wait for the UI thread which is currently waiting for the lock to be released. Not good :)
Being prepared for an ObjectDisposedException might be the best solution. But looking through the reference source code, it doesn't look like it's 100% correct either - it's consistent on a single thread, but you're not calling Invoke on the UI thread (obviously).

WPF Please Wait Dialog

I'm developing a WPF desktop application in c# 4.0 which has to handle a lot of long-running operations (loading data from the DB, calculating simulations, optimizing routes, etc.).
When these long-running operations run in the background I want to show a Please-Wait dialog. When the Please-Wait dialog is shown the application should be locked, but to just disable the application window isn't a good idea because all the DataGrids would lose their status (SelectedItem).
What I have so far works but there are some problems:
A new WaitXUI is created using the Create-factory method. The Create method expects caption text and a refernce to the host control that should be locked. The Create method sets the StartupLocation of the window, the caption text and the host to lock:
WaitXUI wait = WaitXUI.Create("Simulation running...", this);
wait.ShowDialog(new Action(() =>
{
// long running operation
}));
With the overloaded ShowDialog method the WaitXUI can then be displayed. The ShowDialog overload does expect an Action which wraps the long running operation.
In the ShowDialog overload I just start the Action in its own thread and then disable the host control (set Opacity to 0.5 and set IsEnabled to false) and call ShowDialog of the base class.
public bool? ShowDialog(Action action)
{
bool? result = true;
// start a new thread to start the submitted action
Thread t = new Thread(new ThreadStart(delegate()
{
// start the submitted action
try
{
Dispatcher.UnhandledException += Dispatcher_UnhandledException;
Dispatcher.Invoke(DispatcherPriority.Normal, action);
}
catch (Exception ex)
{
throw ex;
}
finally
{
// close the window
Dispatcher.UnhandledException -= Dispatcher_UnhandledException;
this.DoClose();
}
}));
t.Start();
if (t.ThreadState != ThreadState.Stopped)
{
result = this.ShowDialog();
}
return result;
}
private new bool? ShowDialog()
{
DisableHost();
this.Topmost = true;
return base.ShowDialog();
}
private void DisableHost()
{
if (host != null)
{
host.Dispatcher.Invoke(new Action(delegate()
{
this.Width = host.Width - 20;
host.Cursor = Cursors.Wait;
host.IsEnabled = false;
host.Opacity = 0.5;
}));
}
}
Here are the problems with this:
Disabling the host control results in lost of status information (SelectedItems...)
The WaitXUI sometimes is shown for only some milliseconds when the thread ends a few ms after the WaitXUI is shown
Sometimes the dialog doesn't appear at all although the thread is still running
These are the main problems which come to my mind at the moment. How can this concept be improved, or what other methods can be employed to address this problem?
Thanks in advance!
A little lateral thinking always helps when developing WPF applications. You can fulfil your requirements easily with just a Grid, a Rectangle, a bool property (which you could already have) and a BooleanToVisibilityConverter and you won't have to disable any controls.
The idea is simple. Add a white Rectangle in front of your view content with its Opacity property set between 0.5 and around 0.75. Data bind its Visibility property to the bool property in your view model or code behind and plug in the BooleanToVisibilityConverter:
<Grid>
<Grid>
<!--Put your main content here-->
</Grid>
<Rectangle Fill="White" Opacity="0.7" Visibility="{Binding IsWaiting,
Converter={StaticResource BooleanToVisibilityConverter}}" />
<!--You could add a 'Please Wait' TextBlock here-->
</Grid>
Now when you want to disable the controls, you just set the bool property to true and the Rectangle will make the UI appear faded:
IsWaiting = true;
Don't really need to create own implementation, I think it's redundant.
take a look into already created component, like BusyIndicator, for similar needs. which is vital and effective. .
more info from codeplex

Global MediaElement will not restart once stopped, wp7

I have an app that runs background music at the application level so that the music doesn't stop when the user navigates through pages. However, I also make use of a VideoBrush. As I found out, I cannot have the two running at the same time as the VideoBrush will crash when setting its source.
I found that if I set the source of the MediaElement to null when the user attempts to use the VideoBrush, that everything works. Sure the music stops, much to my chagrin, but no error happens.
However, when the user taps away from the VideoBrush, I am trying to make the music start back up (beginning is fine) to no avail. Simply put, I am having trouble getting the music to start up again.
Here is my code:
App.xaml
<Application.Resources>
<MediaElement x:Key="GlobalMedia" Source="minutelongsong.mp3"
MediaEnded="MediaElement_MediaEnded" Visibility="Collapsed" />
</Application.Resources>
App.xaml.cs
public static MediaElement GlobalMediaElement
{
get { return Current.Resources["GlobalMedia"] as MediaElement; }
}
private void Application_Launching(object sender, LaunchingEventArgs e)
{
var AppMediaElement = App.GlobalMediaElement;
AppMediaElement.Position = TimeSpan.Zero;
AppMediaElement.Play();
}
private void MediaElement_MediaEnded(object sender, RoutedEventArgs e)
{
var AppMediaElement = App.GlobalMediaElement;
AppMediaElement.Position = TimeSpan.Zero;
AppMediaElement.Play();
}
And now the page that is making use of the VideoBrush.
MainPage.xaml
<Canvas x:Name="viewfinderCanvas" Width="480" Height="800" HorizontalAlignment="Center" VerticalAlignment="Center" Visibility="Collapsed">
<Canvas.Background>
<VideoBrush x:Name="videoBrush" Stretch="Fill">
<VideoBrush.RelativeTransform>
<CompositeTransform x:Name="previewTransform"
CenterX=".5"
CenterY=".5" />
</VideoBrush.RelativeTransform>
</VideoBrush>
</Canvas.Background>
</Canvas>
MainPage.xaml.cs
private void Button_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
var AppMediaElement = App.GlobalMediaElement;
AppMediaElement.Pause();
AppMediaElement.Stop();
AppMediaElement.Source = null; //set it to null to allow the cam to be set.
if ((PhotoCamera.IsCameraTypeSupported(CameraType.Primary)))
{
viewfinderCanvas.Visibility = Visibility.Visible;
cam = new PhotoCamera(CameraType.Primary);
if (Orientation == PageOrientation.PortraitUp || Orientation == PageOrientation.PortraitDown || Orientation == PageOrientation.Portrait)
{
videoBrush.RelativeTransform = new CompositeTransform() { CenterX = 0.5, CenterY = 0.5, Rotation = 90 };
}
videoBrush.SetSource(cam);
}
When the user exits out of the camera VideoBrush by hitting an on screen button, this code is fired. It disposes the cam, and tries to get the music playing again if the user allows the music. However the music will not play, even with this code.
private void zoomout_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (cam != null)
{
cam.Dispose();
}
viewfinderCanvas.Visibility = Visibility.Collapsed;
if (allowingamemusic == true)
{
var AppMediaElement = App.Current.Resources["GlobalMedia"] as MediaElement;
AppMediaElement.Source = new Uri("minutelongsong.mp3", UriKind.RelativeOrAbsolute);
AppMediaElement.Position = TimeSpan.Zero;
AppMediaElement.Play(); //despite this here, it will not play. No error thrown.
}
}
I switch back and forth between capturing an image and audio in the same page on my phone app.
You were very close, but you also have to set the videoBrush source to something else or it won't let go of the resources.
So when you want to use the MediaElement, then first dispose of the camera like this:
cam.Dispose();
viewfinderBrush.SetSource(new MediaElement()); //so it will let go of cam
cam = null;
//now set source for MediaElemet and do whatever
And when you use the camera again:
mediaElement.Stop();
mediaElement.Source = null; //or else setting the source for the video brush will fail
//now set source for videoBrush and do whatever
Old question, but hopefully this will help someone... it took me a few tries to figure it out.
After writing this last night, and finally testing it this morning, I see what may have happened but am still having the problem of the music not replaying once it's stopped.
MediaElement.MediaFailed was for the entire time being called. I found out that it was calling: AG_E_NETWORK_ERROR. This error is thrown when Zune is running and my device is USB connected to the same computer.
It was suggested that I deploy my app to my phone, close zune, and disconnect the USB. I tried this, and the music still did not play once I tried to replay it. This also happened on the emulator too.
The same AG_E_NETWORK_ERROR is still being thrown.
Since then, I have given up on this as I spent a few hours not finding out how to make this happen. So, I have since gone with the MediaPlayer class and will be using that instead.
------ If anyone solves this problem, I will give you the checkmark for a correct answer.

Is there anyway to know when the screen has been updated/refreshed (OpenGL or DirectX maybe?)

I currently have an application I'm writing in c# (using .NET) that requires me to start a timer as soon as a user sees an image on screen up until they respond with a key press.
Now I realise that practically this is very difficult given the monitor input lag and response time, the time the keyboard takes to physically send the message, the OS to process it, etc.
But I'm trying my best to reduce it down to mostly a constant error (the response time results will be used to compare one user to the next so a constant error isn't really an issue). However annoying hurdle is the variable caused by the monitor refresh rate, as I gather when my onPaint message is called and done with, it doesn't mean the image has actually been processed and sent from the graphics buffer?
Unfortunately time restrictions and other commitments would realistically restrict me to continuing this task in c# for windows.
So what I was wondering was if either handling all the drawing in OpenGL or DirectX or better still for me if it is possible to just using either OpenGL or DirectX to create an event when the screen is updated?
Another suggestion given to me previously was regarding V-Sync, if I switch this off is the image sent as soon as it is drawn? as opposed to sending images at a set rate synchronised to the monitor refresh rate?
You must render your graphic in a separate thread in order to:
Use vertical synchronisation to have a precise timing of the effective display of your image.
Get the precise timing of your user input (since user interface is not on the same thread than the render loop.
Initialise Direct3D to enable the VSync during render :
// DirectX example
presentParams.SwapEffect = SwapEffect.Discard;
presentParams.BackBufferCount = 1;
presentParams.PresentationInterval = PresentInterval.One;
device = new Device(...
Perform the render in a separate thread:
Thread renderThread = new Thread(RenderLoop);
renderThread.Start();
shouldDisplayImageEvent = new AutoResetEvent();
Then use the following render loop:
void RenderLoop()
{
while(applicationActive)
{
device.BeginScene();
// Other rendering task
if (shouldDisplayImageEvent.WaitOne(0))
{
// Render image
// ...
userResponseStopwatch = new Stopwatch();
userResponseStopwatch.Start();
}
device.EndScene();
device.Present();
}
}
Then handle the user input :
void OnUserInput(object sender, EventArgs e)
{
if (userResponseStopwatch != null)
{
userResponseStopwatch.Stop();
float userResponseDuration = userResponseStopwatch.ElapsedMillisecond - 1000 / device.DisplayMode.RefreshRate - displayDeviceDelayConstant;
userResponseStopwatch = null;
}
}
You now use the shouldDisplayImageEvent.Set() event trigger to display the image as needed and start the stop watch.
First enable the VSync on your application idle loop :
// DirectX example
presentParams.SwapEffect = SwapEffect.Discard;
presentParams.BackBufferCount = 1;
presentParams.PresentationInterval = PresentInterval.One;
device = new Device(...
Application.Idle += new EventHandler(OnApplicationIdle);
// More on this here : http://blogs.msdn.com/tmiller/archive/2005/05/05/415008.aspx
internal void OnApplicationIdle(object sender, EventArgs e)
{
Msg msg = new Msg();
while (true)
{
if (PeekMessage(out msg, IntPtr.Zero, 0, 0, 0))
break;
}
// Clearing render
// ...
if (displayImage)
{
// Render image
// ...
renderTime = DateTime.now();
}
device.Present();
}
With the vsync enabled, the device.Present function block until the next frame synchronisation, so if you compute the time between renderTime and the user input time and remove the display device delay + 16.67ms you should get your user response delay.

Categories

Resources