0

I'm making a C# Windows Forms application that plays a video at some point, using DirectX Managed code. I want the app to exit right after playing the video, so I tried to handle the Ending event for the video and it raises and exception. Here is the code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.DirectX.DirectSound;
using Microsoft.DirectX.AudioVideoPlayback;
using Microsoft.DirectX;
using System.Diagnostics;

namespace Picture_Button
{
    public partial class Form1 : Form
    {
        Video video = new Video("C:\\Users\\Pushkin\\Desktop\\PPAP.mp4");
        //Video video = new Video("C:\\Users\\Pushkin\\Desktop\\PPAP.mp4");
        private int clicks = 0;
        public Form1()
        {
            InitializeComponent();
            pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
            video.Ending += new System.EventHandler(this.Video_Ending);
            //video.Ending += Video_Ending;
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            clicks++;
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            switch (clicks)
            {
                case 0: pictureBox1.Image = Properties.Resources.Pineapple; break;
                case 1: pictureBox1.Image = Properties.Resources.Apple; break;
                case 2: pictureBox1.Image = Properties.Resources.Pen; break;
                case 3:
                    {
                        video.Owner = this;
                        video.Play();
                        /*video.Dispose();
                        Application.Exit();*/
                    }
                    break;
            }
        }
        private void Video_Ending(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
            video.Dispose();
            Application.Exit();
        }
    }
}

Exception:

System.NullReferenceException occurred
  HResult=-2147467261
  Message=Object reference not set to an instance of an object.
  Source=Microsoft.DirectX.AudioVideoPlayback
  StackTrace:
  at VideoWndProc(HWND__* hWnd, UInt32 uMsg, UInt32 wParam, Int32 lParam)
  InnerException: 

Also, I've noticed that the program works perfectly without the code for the Ending event.

Cosmin Petolea
  • 119
  • 1
  • 10

1 Answers1

1

The exception occurs because the Video component still tries to do things after the event handler returns. So, you cannot dispose of the component in the handler.

Call theClose method of the form in the handler. This will close the form after the handler has returned and after the Video component has finished.

private void Video_Ending(object sender, EventArgs e)
{ 
    Close();
}

Disposing the Video component is optional in this case, because we know that the application terminates after closing the form anyway. (When a process exits, all its resources are freed).

If this form was part of a larger project, it would be a good idea to dispose of the Video component. The correct place for that is the FormClose event of the form.

NineBerry
  • 26,306
  • 3
  • 62
  • 93
  • Could you please add the code? – Cosmin Petolea Jan 16 '17 at 09:42
  • Ok thanks, it works well on my computer, but the release crashes on any other computer, is it related to this or should I make another post for this issue? – Cosmin Petolea Jan 16 '17 at 11:13
  • @CosminPetolea One question for each problem. Make sure to post full error information. Just a hint: You need to make sure that .net framework 3.5 is installed on the other computer and you need to distribute the dlls for "Direct X for Managed Code". Also make sure a valid codec for the video format is installed. – NineBerry Jan 16 '17 at 11:26