[Duda c#] Incio de funcion automaticamente tras carga el from

Iniciado por antrixro, 26 Agosto 2010, 17:16 PM

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

antrixro

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

[D4N93R]

Hola,

Sobreescribe el método Onload, del formulario:
Código (csharp) [Seleccionar]

protected override void OnLoad(EventArgs e)
{

}


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

Un saludo!

antrixro

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 (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.Net;
using System.IO;
using System.Xml;

namespace WindowsFormsApplication1
{
    public partial class main : Form
    {
        public string fileName;
        public string fileType;
        public string fileUrl;
        public string rName;
        public string rUrl;
        public string rDesc;
        public string typeRar = "rar";

        public main()
        {
            InitializeComponent();
            main_readXml("http://[url]/file.xml");
            this.Text = rName;
        }
        protected override void OnLoad(EventArgs e)
        {

        }
        private void main_Load(object sender, EventArgs e)
        {
            main_Download();
        }


        public void main_Download()
        {
            if (!System.IO.File.Exists(fileName))
            {
                WebRequest req = WebRequest.Create(fileUrl);
                WebResponse response = req.GetResponse();
                Stream stream = response.GetResponseStream();


                //Download in chuncks
                byte[] buffer = new byte[1024];

                //Get Total Size
                int dataLength = (int)response.ContentLength;

                //With the total data we can set up our progress indicators
                progressBar1.Maximum = dataLength;
                //lbProgress.Text = "0/" + dataLength.ToString();

                //this.Text = "Downloading...";
                Application.DoEvents();

                //Download to memory
                //Note: adjust the streams here to download directly to the hard drive
                FileStream memStream = new FileStream(fileName, FileMode.Create);

                while (true)
                {
                    //Try to read the data
                    int bytesRead = stream.Read(buffer, 0, buffer.Length);

                    if (bytesRead == 0)
                    {
                        //Finished downloading
                        progressBar1.Value = progressBar1.Maximum;
                        // lbProgress.Text = dataLength.ToString() + "/" + dataLength.ToString();

                        Application.DoEvents();
                        break;
                    }
                    else
                    {
                        //Write the downloaded data
                        memStream.Write(buffer, 0, bytesRead);

                        //Update the progress bar
                        if (progressBar1.Value + bytesRead <= progressBar1.Maximum)
                        {
                            progressBar1.Value += bytesRead;
                            // lbProgress.Text = progressBar1.Value.ToString() + "/" + dataLength.ToString();

                            progressBar1.Refresh();
                            Application.DoEvents();
                        }
                    }
                }

                if (fileType == typeRar)
                {
                    Unrar rar = new Unrar();

                    rar.Open(fileName, Unrar.OpenMode.Extract);

                    rar.DestinationPath = "";
                    while (rar.ReadHeader())
                    {
                        rar.Extract();
                    }

                    rar.Close();

                }
                //   Application.Exit();


            }
            else
            {
                Application.Exit();
            }

        }

        private void main_readXml(string xml_file)
        {
            XmlTextReader xml_document = new XmlTextReader(xml_file);
            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(xml_document);

            XmlNodeList config = xDoc.GetElementsByTagName("config");

            XmlNodeList actus = ((XmlElement)config[0]).GetElementsByTagName("actus");
            XmlNodeList files = ((XmlElement)actus[0]).GetElementsByTagName("file");
            foreach (XmlElement nodo in files)
            {
                int i = 0;
                XmlNodeList sName = nodo.GetElementsByTagName("name");
                XmlNodeList sType = nodo.GetElementsByTagName("type");
                XmlNodeList sUrl = nodo.GetElementsByTagName("url");
                fileName = sName[i].InnerText;
                fileType = sType[i].InnerText;
                fileUrl = sUrl[i].InnerText;
            }


            foreach (XmlElement nodo in config)
            {
                int i = 0;
                XmlNodeList pName = nodo.GetElementsByTagName("sName");
                XmlNodeList pUrl = nodo.GetElementsByTagName("web");
                XmlNodeList pDesc = nodo.GetElementsByTagName("desc");
                rName = pName[i].InnerText;
                rUrl = pUrl[i].InnerText;
                rDesc = pDesc[i].InnerText;
            }
        }
    }
}

[D4N93R]

Ehm, Bueno, pon  main_Download(); dentro de protected override void OnLoad(EventArgs e) ;)

antrixro

Hola, al ponerlo se ejecuta antes de salir el form =S

43H4FH44H45H4CH49H56H45H

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.

-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