开发者

Delegates:Inform the operations to the upper layer

This question is related to c#.The scenario is that when i click the button the operations like File reading,Data manipulation ,and file dumping are going to be happened.After the completion of each operation i will update the status(i.e,File reading completed,data manipulation completed) in the label which is in UI(FORM-frmTesting)

The button click event is

namespace frmTesting
{
    public partial class Form1 : Form
    {
        private void button1_Click_1(object sender, EventArgs e)
        {
            class1 l_objClass1 = new class1();
            l_objClass1.DoOperation();
        }
    }

    public class class1
    {
        public int DoOperation()
    开发者_如何转开发    {
            ReadTextFile();
            ParsingData();
            SaveTextFile();
            return 0;
        }
        private int ReadTextFile()
        {

            //Read the text File   
            return 0;
        }
        private int ParsingData()
        {
            // Data manipulation
            return 0;
        }
        private int SaveTextFile()
        {
            // save the file   
            return 0;
        }
    }
}

Is it possible to do it using delegates?


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace WindowsFormsApplication3
{
    public delegate void MyDelagate();

    class Class1
    {
        public event MyDelagate _myDelegate;
        private String s1 = String.Empty;

        public String s
        {
            get
            {
                return s1;
            }

            set
            {
                s1 = value;
                if(_myDelegate != null)
                    _myDelegate();
            }
        }
        public int DoOperation()
        {
            s = "Started";
            ReadTextFile();
            ParsingData();
            SaveTextFile();
            s = "Completed";
            return 0;
        }
        private int ReadTextFile()
        {
            s = "Read Text File";
            Thread.Sleep(3000);            
            return 0;
        }
        private int ParsingData()
        {
            s = "Parsing Data";
            Thread.Sleep(3000);
            return 0;
        }
        private int SaveTextFile()
        {
            s = "Save Text File";
            Thread.Sleep(3000);
            return 0;
        }
    }
}

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;

namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        Class1 x = new Class1();

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            x._myDelegate += new MyDelagate(UpdateStatus);
            x.DoOperation();
        }

        void UpdateStatus()
        {
            label1.Text = x.s;
            Validate();
        }
    }
}


Sure.

You would set up your delegate property on your Class1.

After you create your Class1(or you could do this in your constructor) you assign your function to the delegate property.

When your operations are happening/completed, they check to see if the delegate is not null, then execute the delegate with whatever event arguments they want (status level, completion status, that sort of thing).

Then your Form1's function that got passed to the delegate, would handle the processing of the arguments and assign values to the text fields.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜