Foro de elhacker.net

Programación => .NET (C#, VB.NET, ASP) => Mensaje iniciado por: antrixro en 26 Agosto 2010, 17:16 pm



Título: [Duda c#] Incio de funcion automaticamente tras carga el from
Publicado por: antrixro en 26 Agosto 2010, 17:16 pm
Hola, he echo una aplicación que lee un xml y baja un fichero de internet el cual obtiene leyendo el xml.

Mi problema es, si pongo la función el main_Load (como yo llame a la funcion que se ejecuta cuando carga el programa), no se muestra el from ni la barra de carga.

Mi duda es, ¿Como puedo hacer que la funcion de descarga se ejecute sola una vez cargado el from?

Un Saludo y Gracias


Título: Re: [Duda c#] Incio de funcion automaticamente tras carga el from
Publicado por: [D4N93R] en 26 Agosto 2010, 18:39 pm
Hola,

Sobreescribe el método Onload, del formulario:
Código
  1. protected override void OnLoad(EventArgs e)
  2. {
  3.  
  4. }
  5.  

Otra cosa que puedes hacer es subscribirte al load del formulario, no se si eso es lo que necesitas hacer..

Un saludo!


Título: Re: [Duda c#] Incio de funcion automaticamente tras carga el from
Publicado por: antrixro en 26 Agosto 2010, 19:08 pm
Gracias por responder, al sobrescribir  el método OnLoad ya se muestra automáticamente el form pero la descarga no comienza =S

Así tengo mi código:
Código
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Net;
  10. using System.IO;
  11. using System.Xml;
  12.  
  13. namespace WindowsFormsApplication1
  14. {
  15.    public partial class main : Form
  16.    {
  17.        public string fileName;
  18.        public string fileType;
  19.        public string fileUrl;
  20.        public string rName;
  21.        public string rUrl;
  22.        public string rDesc;
  23.        public string typeRar = "rar";
  24.  
  25.        public main()
  26.        {
  27.            InitializeComponent();
  28.            main_readXml("http://[url]/file.xml");
  29.            this.Text = rName;
  30.        }
  31.        protected override void OnLoad(EventArgs e)
  32.        {
  33.  
  34.        }
  35.        private void main_Load(object sender, EventArgs e)
  36.        {
  37.            main_Download();
  38.        }
  39.  
  40.  
  41.        public void main_Download()
  42.        {
  43.            if (!System.IO.File.Exists(fileName))
  44.            {
  45.                WebRequest req = WebRequest.Create(fileUrl);
  46.                WebResponse response = req.GetResponse();
  47.                Stream stream = response.GetResponseStream();
  48.  
  49.  
  50.                //Download in chuncks
  51.                byte[] buffer = new byte[1024];
  52.  
  53.                //Get Total Size
  54.                int dataLength = (int)response.ContentLength;
  55.  
  56.                //With the total data we can set up our progress indicators
  57.                progressBar1.Maximum = dataLength;
  58.                //lbProgress.Text = "0/" + dataLength.ToString();
  59.  
  60.                //this.Text = "Downloading...";
  61.                Application.DoEvents();
  62.  
  63.                //Download to memory
  64.                //Note: adjust the streams here to download directly to the hard drive
  65.                FileStream memStream = new FileStream(fileName, FileMode.Create);
  66.  
  67.                while (true)
  68.                {
  69.                    //Try to read the data
  70.                    int bytesRead = stream.Read(buffer, 0, buffer.Length);
  71.  
  72.                    if (bytesRead == 0)
  73.                    {
  74.                        //Finished downloading
  75.                        progressBar1.Value = progressBar1.Maximum;
  76.                        // lbProgress.Text = dataLength.ToString() + "/" + dataLength.ToString();
  77.  
  78.                        Application.DoEvents();
  79.                        break;
  80.                    }
  81.                    else
  82.                    {
  83.                        //Write the downloaded data
  84.                        memStream.Write(buffer, 0, bytesRead);
  85.  
  86.                        //Update the progress bar
  87.                        if (progressBar1.Value + bytesRead <= progressBar1.Maximum)
  88.                        {
  89.                            progressBar1.Value += bytesRead;
  90.                            // lbProgress.Text = progressBar1.Value.ToString() + "/" + dataLength.ToString();
  91.  
  92.                            progressBar1.Refresh();
  93.                            Application.DoEvents();
  94.                        }
  95.                    }
  96.                }
  97.  
  98.                if (fileType == typeRar)
  99.                {
  100.                    Unrar rar = new Unrar();
  101.  
  102.                    rar.Open(fileName, Unrar.OpenMode.Extract);
  103.  
  104.                    rar.DestinationPath = "";
  105.                    while (rar.ReadHeader())
  106.                    {
  107.                        rar.Extract();
  108.                    }
  109.  
  110.                    rar.Close();
  111.  
  112.                }
  113.                //   Application.Exit();
  114.  
  115.  
  116.            }
  117.            else
  118.            {
  119.                Application.Exit();
  120.            }
  121.  
  122.        }
  123.  
  124.        private void main_readXml(string xml_file)
  125.        {
  126.            XmlTextReader xml_document = new XmlTextReader(xml_file);
  127.            XmlDocument xDoc = new XmlDocument();
  128.            xDoc.Load(xml_document);
  129.  
  130.            XmlNodeList config = xDoc.GetElementsByTagName("config");
  131.  
  132.            XmlNodeList actus = ((XmlElement)config[0]).GetElementsByTagName("actus");
  133.            XmlNodeList files = ((XmlElement)actus[0]).GetElementsByTagName("file");
  134.            foreach (XmlElement nodo in files)
  135.            {
  136.                int i = 0;
  137.                XmlNodeList sName = nodo.GetElementsByTagName("name");
  138.                XmlNodeList sType = nodo.GetElementsByTagName("type");
  139.                XmlNodeList sUrl = nodo.GetElementsByTagName("url");
  140.                fileName = sName[i].InnerText;
  141.                fileType = sType[i].InnerText;
  142.                fileUrl = sUrl[i].InnerText;
  143.            }
  144.  
  145.  
  146.            foreach (XmlElement nodo in config)
  147.            {
  148.                int i = 0;
  149.                XmlNodeList pName = nodo.GetElementsByTagName("sName");
  150.                XmlNodeList pUrl = nodo.GetElementsByTagName("web");
  151.                XmlNodeList pDesc = nodo.GetElementsByTagName("desc");
  152.                rName = pName[i].InnerText;
  153.                rUrl = pUrl[i].InnerText;
  154.                rDesc = pDesc[i].InnerText;
  155.            }
  156.        }
  157.    }
  158. }
  159.  


Título: Re: [Duda c#] Incio de funcion automaticamente tras carga el from
Publicado por: [D4N93R] en 26 Agosto 2010, 19:38 pm
Ehm, Bueno, pon  main_Download(); dentro de protected override void OnLoad(EventArgs e) ;)


Título: Re: [Duda c#] Incio de funcion automaticamente tras carga el from
Publicado por: antrixro en 26 Agosto 2010, 19:43 pm
Hola, al ponerlo se ejecuta antes de salir el form =S


Título: Re: [Duda c#] Incio de funcion automaticamente tras carga el from
Publicado por: 43H4FH44H45H4CH49H56H45H en 27 Agosto 2010, 12:00 pm
Debes hacerlo con un Thread, hace un tiempo hice un ejemplo de como utilizar un Thread con un ProgressBar, debe estar mas abajo o en la 2da pagina.