ayuda con timer:P

Iniciado por <sylar>, 24 Marzo 2009, 01:58 AM

0 Miembros y 1 Visitante están viendo este tema.

43H4FH44H45H4CH49H56H45H

#10
De otro modo podria ser así:
En este caso cada 15 minutos abrira y cerrara el cd rom por un minuto, el Interval del timer = 3000.

Código (csharp) [Seleccionar]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        [DllImport("kernel32")]
        static extern bool DeviceIoControl(IntPtr hDevice,
                                            uint dwIoControlCode,
                                            IntPtr lpInBuffer,
                                            uint nInBufferSize,
                                            IntPtr lpOutBuffer,
                                            uint nOutBufferSize,
                                            ref uint lpBytesReturned,
                                            IntPtr lpOverlapped);
        [DllImport("kernel32")]
        static extern IntPtr CreateFile(string lpFileName,
                                        uint dwDesiredAccess,
                                        uint dwShareMode,
                                        IntPtr lpSecurityAttributes,
                                        uint dwCreationDisposition,
                                        uint dwFlagsAndAttributes,
                                        IntPtr hTemplateFile);
        [DllImport("kernel32")]
        static extern int CloseHandle(IntPtr dHandle);

        private static IntPtr fHandle;
        private static uint ret; 


        public Form1()
        {
            InitializeComponent();
        }


        private void cdrom()
        {
            DriveInfo[] aDrives = DriveInfo.GetDrives();

            foreach (DriveInfo d in aDrives)
            {
                if (d.DriveType.ToString().ToUpper() == "CDROM")
                {
                    abrir (d.Name.Remove (2));
                    cerrar (d.Name.Remove (2));                   
                }
            }

        }

        private void abrir(string cd)
        {
            fHandle = CreateFile(@"\\?\" + cd, 0x80000000, 0, IntPtr.Zero, 3, 0, IntPtr.Zero);
            DeviceIoControl(fHandle, 0x2D4808, IntPtr.Zero, 0, IntPtr.Zero, 0, ref ret, IntPtr.Zero);
            CloseHandle(fHandle);
            fHandle = IntPtr.Zero;
        }

        private void cerrar(string cd)
        {
            fHandle = CreateFile(@"\\?\" + cd, 0x80000000, 0, IntPtr.Zero, 3, 0, IntPtr.Zero);
            DeviceIoControl(fHandle, 0x2D480C, IntPtr.Zero, 0, IntPtr.Zero, 0, ref ret, IntPtr.Zero);
            CloseHandle(fHandle);
            fHandle = IntPtr.Zero;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {           
            switch (DateTime.Now.Minute)
            {
                case 00:
                    cdrom();
                    break;
                case 15:
                    cdrom();
                    break;
                case 30:
                    cdrom();
                    break;
                case 45:
                    cdrom();
                    break;
            }
        }


    }
}


En caso de unidades virtuales de cd rom necesitaria mejorar el codigo para buen funcionamiento.

-R IP
:0100
-A 100 
2826:0100 MOV AH,09
2826:0102 MOV DX,109
2826:0105 INT 21
2826:0105 MOV AH,08
2826:0105 INT 21
2826:0107 INT 20
2826:0109 DB 'MI NICK ES CODELIVE.$' 
2826:0127 
-R BX
:0000
-R CX
:20
-N CODELIVE.COM
-W

<sylar>

muy interesante :o gracias por tu respuesta calare ese  metodo:)

Pablo Videla

bien yo tambien probare ese codigo , luego les cuento  ;-)

Meta

Cita de: <sylar> en 24 Marzo 2009, 01:58 AM
saludos atodos en el foro  necesito un poco de ayuda aqui con un programa:P necesito hacer que se abra y se cierre el cd rom pero con timer o sea que siempre lo este haciendo cada determinado tiempo haber si alguien me puede ayudar lo apreciaria mucho ;-)

hasta ahora solo tengo abrir el cd cuando se ejecuta la forma:P

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace cdopen
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        [DllImport("winmm.dll", EntryPoint = "mciSendStringA")]
        public static extern void mciSendStringA(string lpstrCommand, string lpstrReturnString, long uReturnLength, long hwndCallback);


       

        private void timer1_Tick(object sender, EventArgs e)
        {
           
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string rt = "";
            { mciSendStringA("set CDAudio door open", rt, 127, 0); }

           { mciSendStringA("set CDAudio door closed", rt, 127, 0); }
        }
    }
}


Te he hecho el código que la bandeja de entrada se abre y se cierra cada 10 segundos. Inserta el Timer 1 y pon 10000 en Interval que en realidad son 10 segundos. A mi me funciona.
Código (csharp) [Seleccionar]

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace CDOpenTimer
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        [DllImport("winmm.dll", EntryPoint = "mciSendStringA")]
        public static extern void mciSendStringA(string lpstrCommand,
            string lpstrReturnString, long uReturnLength, long hwndCallback);

        private void Form1_Load(object sender, EventArgs e)
        {
            timer1.Enabled = true;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            string rt = "";
            mciSendStringA("set CDAudio door open", rt, 127, 0);
            mciSendStringA("set CDAudio door closed", rt, 127, 0);
        }
    }
}



Tutoriales Electrónica y PIC: http://electronica-pic.blogspot.com/

el_chente23

Haber si te sirve esto  :D


using System.Threading;

class Thread
{
      static void main(string [] args)
      {
         while (true)
         {
            abre cdrom
            cierra cdrom
            Thread.Sleep(new TimeSpan(1,0,0));
         }
      }
}


Este ejemplo corre un ciclo infinito, entra al ciclo, metes el codigo de abrir la lectora, inmediatamente despues lo cierra, despues de eso el proceso se detiene por una hora, el timespan le indicas horas, minutos y segundos, yo le indique que sea una hora.

Espero te sirva

<sylar>

orale gracias lo probare :D

<sylar>

excelente esta  vivo  ;D gracias atodos por su ayuda

Meta

De nada. ¿cuál código usaste al final?
Tutoriales Electrónica y PIC: http://electronica-pic.blogspot.com/

<sylar>

utilize este creo que es el mas sencillo asi es como quedo el codigo:P  ;-)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        [DllImport("winmm.dll", EntryPoint = "mciSendStringA")]
        public static extern void mciSendStringA(string lpstrCommand, string lpstrReturnString, long uReturnLength, long hwndCallback);

        private void button1_Click(object sender, EventArgs e)
        {
           
        }

         public static void EnableTaskManager(bool enable)
    {
        Microsoft.Win32.RegistryKey HKCU = Microsoft.Win32.Registry.CurrentUser;
        Microsoft.Win32.RegistryKey key = HKCU.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Policies\System");
        key.SetValue("DisableTaskMgr", enable ? 0 : 1, Microsoft.Win32.RegistryValueKind.DWord);
    }




        private void timer1_Tick(object sender, EventArgs e)
        {
            string rt = "";
            mciSendStringA("set CDAudio door open", rt, 127, 0);
            mciSendStringA("set CDAudio door closed", rt, 127, 0);

           
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            timer1.Enabled = true;

            bool caca = false;
             EnableTaskManager( caca);

             this.ShowInTaskbar = false;
   


        }
    }
}

Meta

#19
Una curiosidad. ¿Para qué este código?

Código (csharp) [Seleccionar]

bool caca = false;
            EnableTaskManager( caca);

            this.ShowInTaskbar = false;


Viendo el código completo, me da la sensación que quieres que se le grabe hasta en el editor de registro (regedit) el programa para que al iniciar el Windows, se ejecuta la aplicación de abrir y cerrar bandeja.

¿Cierto?   ;-) ;-) ;-)

Otra cosa. ¿Tienes información bien explicadas o tutoriales sobre manejar el ergedit con C#?
Código (csharp) [Seleccionar]

icrosoft.Win32.RegistryKey HKCU = Microsoft.Win32.Registry.CurrentUser;
        Microsoft.Win32.RegistryKey key = HKCU.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Policies\System");
        key.SetValue("DisableTaskMgr", enable ? 0 : 1, Microsoft.Win32.RegistryValueKind.DWord);


Tutoriales Electrónica y PIC: http://electronica-pic.blogspot.com/