custom event

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?d2Vic211cmY=?=

    #1

    custom event

    dear newsgroup member,

    I want to access a label in a form (form1.cs) from a class file
    (periodicUpload .cs). The label in form1.cs is a status Indicator.
    PeriodicUpload. cs is a timer class file. From this class file I upload every
    minute a file to a web server. in the Main form (form1.cs) the label must
    changed when an error occurs in PeriodicUpload. cs.

    I read something about delegate and event but I don't know how to implement
    this.
    I am just start with programming with c#.

    can some one help me with this please




  • Marc Gravell

    #2
    Re: custom event

    What do you mean "PeriodicUpload .cs is a timer class file"?

    Essentially, there are a handful of ways of doing a periodic upload.
    The simplest (and most appropriate if you are starting) is simply to
    drop a win-form Timer control onto your form1, with a duration of 1
    minute, and handle the Tick event. In the designer you can wire this
    up by just double-clicking on the Timer.

    You would also need to add a try/catch around the upload, and update
    the label (with the error message) in the "catch".

    This isn't the /absolute/ best approach (it would be better to do the
    upload on a worker thread), but if you are new to C# it is a
    reasonable first attempt; otherwise you will get distracted into
    oddities like thread-affinity, callbacks, etc.

    Marc

    Comment

    • =?Utf-8?B?d2Vic211cmY=?=

      #3
      Re: custom event

      Hi Marc,

      Thx for your reply. But this timer tick event occurs in the class file
      "PeriodicUpload .cs" and not on the form1.cs. I have also a try and catch
      block
      where i fire the event for the label. But I am doing something wrong en get
      the warning message "Illegal Cross Thread Calls" in VS2005. I get this
      because I have send the form1 object as a parameter to the constructor of
      PeriodicUpload and this is not the approperiate way to do so in VS2005. What
      i want to know is, how to do it with delegate and events.

      I will add the source file here: look at "WebStatGen " in periodicUpload. cs
      this not the appropriate way to do this in VS2005

      Form1.cs source file:
      ----------------------------------------------------
      using System;
      using System.IO;
      using FtpLib;
      using System.Collecti ons.Generic;
      using System.Componen tModel;
      using System.Data;
      using System.Drawing;
      using System.Text;
      using System.Windows. Forms;

      namespace WinWebQ
      {
      public partial class WebStatGen : Form
      {
      public static string llabel;
      PeriodicUpload periodicUpload = null;
      public WebStatGen()
      {
      InitializeCompo nent();
      FtpConfigManage r fcm = new
      FtpConfigManage r(Application.S tartupPath);
      periodicUpload = new PeriodicUpload( fcm.create() ,
      @"c:\yoyo\cms_s tats\CSV_test.h tml", this);
      }


      private void button1_Click(o bject sender, EventArgs e)
      {

      }

      private void label1_Click(ob ject sender, EventArgs e)
      {

      }

      private void btn_Start_Click (object sender, EventArgs e)
      {
      btn_Stop.Enable d = true;
      btn_Start.Enabl ed = false;
      periodicUpload. Start();

      }

      private void fTPConfigToolSt ripMenuItem_Cli ck(object sender,
      EventArgs e)
      {
      FTP_Options FTP_Form = new FTP_Options();
      FTP_Form.ShowDi alog(this);
      FtpConfigManage r ftpConfigFactor y = new
      FtpConfigManage r(Application.S tartupPath);
      FTPConfig fc = ftpConfigFactor y.create();
      periodicUpload. changeCongifura tion(fc);
      }

      private void exitToolStripMe nuItem_Click(ob ject sender, EventArgs e)
      {
      WebStatGen Gen_Form = new WebStatGen();
      Application.Exi t();
      }
      public void Statusss(int status)
      {
      if (status == 0)
      {
      this.label2.Tex t = "Status OK";
      this.label2.For eColor = System.Drawing. Color.Green;
      }
      else
      {
      this.label2.Tex t = "Status NOK";
      this.label2.For eColor = System.Drawing. Color.Red;
      }
      }
      private void btn_Stop_Click( object sender, EventArgs e)
      {
      btn_Stop.Enable d = false;
      btn_Start.Enabl ed = true;
      periodicUpload. Stop();

      }
      }
      }
      ---------- End form1.cs --------------------------------

      PeriodicUpload. cs
      --------------------------------------
      using System;
      using System.Collecti ons.Generic;
      using System.Text;
      using System.Timers;
      using FtpLib;

      namespace WinWebQ
      {
      class PeriodicUpload
      {
      private static FTPFactory ff;
      private Timer timer = null;
      private static String fileToUpload = null;
      private FTPConfig config;
      private static WebStatGen gg; // object call

      private static void TimerEventHandl er( object source,
      ElapsedEventArg s e )
      {
      readInputFile rif = new readInputFile() ;
      rif.readfile();

      try
      {
      gg.Statusss(1);
      ff.upload(fileT oUpload);
      }
      catch (Exception)
      {
      gg.Statusss(1);

      ff.setRemoteHos t("ftp.casblokk er.nl");
      ff.setRemoteUse r("wicom");
      ff.setRemotePas s("12345");
      ff.login();
      ff.chdir("../WebQ");
      ff.setBinaryMod e(true);

      ff.upload(fileT oUpload);
      }
      }

      public PeriodicUpload( FTPConfig _config, String _fileToUpload,
      WebStatGen _gg)
      {
      config = _config;
      gg = _gg;
      fileToUpload = _fileToUpload;
      InitFTP();
      InitTimer();
      }

      public void changeCongifura tion(FTPConfig _config)
      {
      config = _config;
      timer.Interval = config.timeout;
      }

      private void InitTimer()
      {
      timer = new Timer();
      timer.Elapsed += new ElapsedEventHan dler(TimerEvent Handler);
      timer.Interval = config.timeout;
      }


      public void Start()
      {
      ff.setRemoteHos t(config.hostna me);
      ff.setRemoteUse r(config.userna me);
      ff.setRemotePas s(config.passwo rd);
      ff.login();
      ff.chdir(config .remotedir);
      ff.setBinaryMod e(true);
      timer.Start();
      timer.Enabled = true;
      }

      private static void InitFTP()
      {
      ff = new FTPFactory();
      ff.setDebug(tru e);

      }

      public void Stop()
      {
      timer.Enabled = false;
      ff.close();
      }
      }


      }

      ------------- End PeriodicUpload. cs ---------------

      "Marc Gravell" wrote:
      What do you mean "PeriodicUpload .cs is a timer class file"?
      >
      Essentially, there are a handful of ways of doing a periodic upload.
      The simplest (and most appropriate if you are starting) is simply to
      drop a win-form Timer control onto your form1, with a duration of 1
      minute, and handle the Tick event. In the designer you can wire this
      up by just double-clicking on the Timer.
      >
      You would also need to add a try/catch around the upload, and update
      the label (with the error message) in the "catch".
      >
      This isn't the /absolute/ best approach (it would be better to do the
      upload on a worker thread), but if you are new to C# it is a
      reasonable first attempt; otherwise you will get distracted into
      oddities like thread-affinity, callbacks, etc.
      >
      Marc
      >

      Comment

      • KWienhold

        #4
        Re: custom event

        On 6 Dez., 00:06, websmurf <websm...@discu ssions.microsof t.comwrote:
        Hi Marc,
        >
        Thx for your reply. But this timer tick event occurs in the class file
        "PeriodicUpload .cs" and not on the form1.cs. I have also a try and catch
        block
        where i fire the event for the label. But I am doing something wrong en get
        the warning message "Illegal Cross Thread Calls" in VS2005. I get this
        because I have send the form1 object as a parameter to the constructor of
        PeriodicUpload and this is not the approperiate way to do so in VS2005. What
        i want to know is, how to do it with delegate and events.
        >
        I will add the source file here: look at "WebStatGen " in periodicUpload. cs
        this not the appropriate way to do this in VS2005
        >
        Form1.cs source file:
        ----------------------------------------------------
        using System;
        using System.IO;
        using FtpLib;
        using System.Collecti ons.Generic;
        using System.Componen tModel;
        using System.Data;
        using System.Drawing;
        using System.Text;
        using System.Windows. Forms;
        >
        namespace WinWebQ
        {
        public partial class WebStatGen : Form
        {
        public static string llabel;
        PeriodicUpload periodicUpload = null;
        public WebStatGen()
        {
        InitializeCompo nent();
        FtpConfigManage r fcm = new
        FtpConfigManage r(Application.S tartupPath);
        periodicUpload = new PeriodicUpload( fcm.create() ,
        @"c:\yoyo\cms_s tats\CSV_test.h tml", this);
        }
        >
        private void button1_Click(o bject sender, EventArgs e)
        {
        >
        }
        >
        private void label1_Click(ob ject sender, EventArgs e)
        {
        >
        }
        >
        private void btn_Start_Click (object sender, EventArgs e)
        {
        btn_Stop.Enable d = true;
        btn_Start.Enabl ed = false;
        periodicUpload. Start();
        >
        }
        >
        private void fTPConfigToolSt ripMenuItem_Cli ck(object sender,
        EventArgs e)
        {
        FTP_Options FTP_Form = new FTP_Options();
        FTP_Form.ShowDi alog(this);
        FtpConfigManage r ftpConfigFactor y = new
        FtpConfigManage r(Application.S tartupPath);
        FTPConfig fc = ftpConfigFactor y.create();
        periodicUpload. changeCongifura tion(fc);
        }
        >
        private void exitToolStripMe nuItem_Click(ob ject sender, EventArgs e)
        {
        WebStatGen Gen_Form = new WebStatGen();
        Application.Exi t();
        }
        public void Statusss(int status)
        {
        if (status == 0)
        {
        this.label2.Tex t = "Status OK";
        this.label2.For eColor = System.Drawing. Color.Green;
        }
        else
        {
        this.label2.Tex t = "Status NOK";
        this.label2.For eColor = System.Drawing. Color.Red;
        }
        }
        private void btn_Stop_Click( object sender, EventArgs e)
        {
        btn_Stop.Enable d = false;
        btn_Start.Enabl ed = true;
        periodicUpload. Stop();
        >
        }
        }}
        >
        ---------- End form1.cs --------------------------------
        >
        PeriodicUpload. cs
        --------------------------------------
        using System;
        using System.Collecti ons.Generic;
        using System.Text;
        using System.Timers;
        using FtpLib;
        >
        namespace WinWebQ
        {
        class PeriodicUpload
        {
        private static FTPFactory ff;
        private Timer timer = null;
        private static String fileToUpload = null;
        private FTPConfig config;
        private static WebStatGen gg; // object call
        >
        private static void TimerEventHandl er( object source,
        ElapsedEventArg s e )
        {
        readInputFile rif = new readInputFile() ;
        rif.readfile();
        >
        try
        {
        gg.Statusss(1);
        ff.upload(fileT oUpload);
        }
        catch (Exception)
        {
        gg.Statusss(1);
        >
        ff.setRemoteHos t("ftp.casblokk er.nl");
        ff.setRemoteUse r("wicom");
        ff.setRemotePas s("12345");
        ff.login();
        ff.chdir("../WebQ");
        ff.setBinaryMod e(true);
        >
        ff.upload(fileT oUpload);
        }
        }
        >
        public PeriodicUpload( FTPConfig _config, String _fileToUpload,
        WebStatGen _gg)
        {
        config = _config;
        gg = _gg;
        fileToUpload = _fileToUpload;
        InitFTP();
        InitTimer();
        }
        >
        public void changeCongifura tion(FTPConfig _config)
        {
        config = _config;
        timer.Interval = config.timeout;
        }
        >
        private void InitTimer()
        {
        timer = new Timer();
        timer.Elapsed += new ElapsedEventHan dler(TimerEvent Handler);
        timer.Interval = config.timeout;
        }
        >
        public void Start()
        {
        ff.setRemoteHos t(config.hostna me);
        ff.setRemoteUse r(config.userna me);
        ff.setRemotePas s(config.passwo rd);
        ff.login();
        ff.chdir(config .remotedir);
        ff.setBinaryMod e(true);
        timer.Start();
        timer.Enabled = true;
        }
        >
        private static void InitFTP()
        {
        ff = new FTPFactory();
        ff.setDebug(tru e);
        >
        }
        >
        public void Stop()
        {
        timer.Enabled = false;
        ff.close();
        }
        }
        >
        }
        >
        ------------- End PeriodicUpload. cs ---------------
        >
        >
        >
        "Marc Gravell" wrote:
        What do you mean "PeriodicUpload .cs is a timer class file"?
        >
        Essentially, there are a handful of ways of doing a periodic upload.
        The simplest (and most appropriate if you are starting) is simply to
        drop a win-form Timer control onto your form1, with a duration of 1
        minute, and handle the Tick event. In the designer you can wire this
        up by just double-clicking on the Timer.
        >
        You would also need to add a try/catch around the upload, and update
        the label (with the error message) in the "catch".
        >
        This isn't the /absolute/ best approach (it would be better to do the
        upload on a worker thread), but if you are new to C# it is a
        reasonable first attempt; otherwise you will get distracted into
        oddities like thread-affinity, callbacks, etc.
        >
        Marc- Zitierten Text ausblenden -
        >
        - Zitierten Text anzeigen -
        To get PeriodicUpload to fire a custom status event you would have to
        declare a Delegate type for it somewhere (I normally put these outside
        of the actual classes, in the namespace):

        public delegate void StatusChangedEv entHandler (object sender,
        StatusChangedEv entArgs e);

        This also requires that you create a StatusChangedEv entArgs-class that
        holds the relevant information for this event. You could also just
        have an int here, but with its own class it is easier to return more
        information if something changes later on.
        Then you would declare the event inside the PeriodicUpload class like
        this:

        public event StatusChangedEv entHandler StatusChanged;

        After that you could raise the event from inside of PeriodicUpdate
        like this:

        if (StatusChanged != null)
        {
        StatusChanged(t his, new StatusChangedEv entArgs(1));
        }

        The nullity check is required to make sure that someone is actually
        subscribed to your event and there is a delegate instance to invoke.
        If you use .Net 2.0 you could also assign a default delegate instance
        when declaring the event like this:

        public event StatusChangedEv entHandler StatusChanged = delegate
        (object sender, StatusChangedEv entArgs e) { };

        This should cover the basics of implementing your own events, however,
        I am not quite sure that this will help in your case. As far as I can
        see this code should work (although passing a reference to the form in
        the constructor is not elegant). Since you get the "Illegal Cross
        Thread Calls" exception, one would assume that there is some form of
        multi-threading going on, since it is in fact not allowed to alter the
        GUI (i.e. changing the text of your label) from a different thread
        in .Net 2.0. In .Net 1.1 it was possible, but still a bad idea (which
        is why it is illegal altogether now).

        hth,
        Kevin Wienhold

        Comment

        Working...