private void windowLoaded(object sender, RoutedEventArgs e)
        {
            IntPtr handle = new WindowInteropHelper(this).Handle;

            System.Threading.Thread updateThread = new System.Threading.Thread(delegate() {
                ctx = CreateEngine(handle, currentCallback);
                Dispatcher.Invoke(() => {
                    renderTarget      = new D3DImage();
                    drawTarget.Source = renderTarget;
                    renderTarget.Lock();
                    renderTarget.SetBackBuffer(D3DResourceType.IDirect3DSurface9, ctx.surface);
                    renderTarget.Unlock();
                });
                while (running)
                {
                    lock (ctxlock)
                    {
                        if (recordMode)
                        {
                            RecordFrame(ctx.handle);
                        }
                        else
                        {
                            if (!clientOnly)
                            {
                                DrawBackbuffer(ctx.handle);
                            }
                        }
                    }
                    try
                    {
                        Dispatcher.Invoke(() =>
                        {
                            renderTarget.Lock();
                            renderTarget.AddDirtyRect(new Int32Rect(0, 0, renderTarget.PixelWidth, renderTarget.PixelHeight));
                            renderTarget.Unlock();
                        });
                    }catch (Exception er)
                    {
                    }
                }
            });
            updateThread.Name = "EncodeThread";
            updateThread.Start();
        }
 void Reset()
 {
     lock (ctxlock)
     {
         renderTarget.Lock();
         renderTarget.SetBackBuffer(D3DResourceType.IDirect3DSurface9, IntPtr.Zero);
         renderTarget.Unlock();
         if (ctx.handle != IntPtr.Zero)
         {
             FreeEngine(ctx.handle);
             ctx.handle = IntPtr.Zero;
         }
         ctx = CreateEngine(new WindowInteropHelper(this).Handle, currentCallback);
         renderTarget.Lock();
         renderTarget.SetBackBuffer(D3DResourceType.IDirect3DSurface9, ctx.surface);
         renderTarget.Unlock();
     }
 }
        void StartVideo()
        {
            context     = MainWindow.CreateEngine(new WindowInteropHelper(this).Handle, cb);
            cimg.Source = img;
            img.Lock();
            img.SetBackBuffer(D3DResourceType.IDirect3DSurface9, context.surface);
            img.Unlock();
            System.Threading.Thread mthread = new System.Threading.Thread(delegate() {
                BinaryReader mreader = new BinaryReader(str);
                while (true)
                {
                    try
                    {
                        switch (mreader.ReadByte())
                        {
                        case 0:
                            //Video frame
                            long timestamp = mreader.ReadInt64();
                            byte[] packet  = mreader.ReadBytes(mreader.ReadInt32());
                            unsafe
                            {
                                fixed(byte *pptr = packet)
                                {
                                    MainWindow.SendPacket(context.handle, new IntPtr(pptr), packet.Length);
                                }
                            }
                            break;

                        case 12:
                            Dispatcher.Invoke(() => {
                                if (trustedComputer.IsChecked.Value)
                                {
                                    System.Windows.Forms.Cursor.Hide();
                                }
                            });
                            break;

                        case 13:
                            Dispatcher.Invoke(() => {
                                if (trustedComputer.IsChecked.Value)
                                {
                                    System.Windows.Forms.Cursor.Show();
                                }
                            });
                            break;

                        case 14:
                            ulong forme = mreader.ReadUInt64();
                            Dispatcher.Invoke(() => {
                                if (trustedComputer.IsChecked.Value)
                                {
                                    if (ulongforcursors.ContainsKey(forme))
                                    {
                                        LoadCursor(forme);
                                    }
                                    else
                                    {
                                        BinaryWriter mwriter = new BinaryWriter(str);
                                        mwriter.Write((byte)16);
                                        mwriter.Write(forme);
                                    }
                                }
                            });
                            break;

                        case 15:
                            {
                                ulong foranothercursor = mreader.ReadUInt64();
                                MemoryStream cstr      = new MemoryStream(mreader.ReadBytes(mreader.ReadInt32()));
                                Dispatcher.Invoke(() => {
                                    Cursor curses = new Cursor(cstr);
                                    ulongforcursors[foranothercursor] = curses;
                                    LoadCursor(foranothercursor);
                                });
                            }
                            break;

                        case 16:
                            break;     //RESERVED. DO NOT USE.

                        default:
                            throw new Exception("Unsupported operation");
                            break;
                        }
                    }
                    catch (Exception er)
                    {
                        Dispatcher.Invoke(Close);
                        break;
                    }
                }
            });
            mthread.Start();
        }