Click here to Skip to main content
15,892,768 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
This is driving me nuts!

Form1.cs = Press btnClass and it starts Class1.cs

C#
public partial class Form1 : Form
{
    private readonly Class1 cls = new Class1();



C#
private void btnClass_Click(object sender, EventArgs e)
{
    Thread clsThread = new Thread(StartClass1);
    clsThread.Start();
}
private void StartClass1()
{
    // Class1.cs
    cls.BeginClass();
}



Class1.cs = stuff happens then updates label.Text located in Form1

I've tried using delegates and it works, but the problem I need those delegates to send the value from Class1 to Form1 label. :doh:
C#
class Class1
{
    public void BeginClass()
    {
        // Needs to pass value back to label created in Form1
        // ClassDone(); - start events
    }
    public event ClassDoneEventHandler ClassDoneEvent;
    private void ClassDone()
    {
        ClassDoneEvent(new ClassDoneEventArgs());
    }
    # region ClassDone
    public delegate void ClassDoneEventHandler(ClassDoneEventArgs e);
    public class ClassDoneEventArgs : EventArgs
    {
    }
    # endregion
}



Any Help Appreciated!! ;)
Posted
Comments
Sergey Alexandrovich Kryukov 18-Feb-11 16:44pm    
Please don't name it "BeginClass". You cannot begin a class, you can start a thread.
I appreciate you created a wrapper for the thread, so many fail to get benefits of it. Rename Class1 one to "ThreadWrapper", BegingClass to "Start".
Don't consider naming as annoyance, names are very important.
--SA

You can put the values into the ClassDoneEventArgs object

C#
public class ClassDoneEventArgs : EventArgs    
{    
    public object Value { get; set; }
    public ClassDoneEventArgs(object value)
    {
        this.Value = value;
    }
}


And the when you call your event poster, do this:

C#
private void OnMyEvent()
{
   MyEvent(this, new ClassDoneEventArgs(myValue));
}
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 18-Feb-11 16:40pm    
Yes OP can, my 5.
--SA
Genius John Simmons,

You have no idea how much time you just saved me. Here's the code all put together.

C#
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.Threading;



Form1.cs
C#
namespace PassData
{
    public partial class Form1 : Form
    {
        private readonly Class1 cls = new Class1();
        public Form1()
        {
            InitializeComponent();
            cls.ClassDoneEvent += new PassData.Class1.ClassDoneEventHandler(class1_ClassDoneEvent);
        }
        private void class1_ClassDoneEvent(PassData.Class1.ClassDoneEventArgs e)
        {
            //e.Value will be SomeValue
            label1.Invoke(new UpdateLabelTextDelegate(UpdateLabelText), e.Value.ToString());
        }
        private delegate void UpdateLabelTextDelegate(string text);
        private void UpdateLabelText(string text)
        {
            // Actual Label updated with text.
            label1.Text = text;
        }
        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }
        private void btnClass_Click(object sender, EventArgs e)
        {
            Thread clsThread = new Thread(StartClass1);
            clsThread.Start();
        }
        private void StartClass1()
        {
            // Class1.cs
            cls.BeginClass();
        }
    }
}


Class1.cs

C#
namespace PassData
{
    class Class1
    {
        public void BeginClass()
        {
            ClassDone();
        }
        public event ClassDoneEventHandler ClassDoneEvent;
        private void ClassDone()
        {
            ClassDoneEvent(new ClassDoneEventArgs());
        }
        public delegate void ClassDoneEventHandler(ClassDoneEventArgs e);
        public class ClassDoneEventArgs : EventArgs
        {
            public string Value
            {
                get
                {
                    // The value to be passed to Form1.cs
                    return "SomeValue";
                }
            }
        }
    }
}


apaka, appreciate the response as well, going to have to look into that.
 
Share this answer
 
v2
dotnetperls.com/backgroundworker Try using this.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900