Delegates, returning values to main form

Posted on 2004-11-20
Last Modified: 2008-03-04
i'm stuck and can't find solution. I'm a novice in C#...

I have two files in my project...this is just a simplified example, my real app is more complicated :)
1. Main FORM that has a textbox and a Button on it.
2. and annother file "CLASS" that has defined a function that generates random values (for example).

now... On a click of a button I create a thread that calls function form that class file like this.

Class1 delaj = new Class1();
Thread t = new Thread(new ThreadStart(delaj.GenerateNumbers));
t.Start(); is my problem. How can I pass generated value back to main form, each time that is generated, so that I can append generated value in my textbox? I know how to create event handler so that I can trigger an event each time that number is generated. I can't figure it out how to get that geneated value.
Question by:gruntar
    LVL 48

    Accepted Solution

    Take a look at this article, it is close to your case. It passes strings, replace strings with numbers.


    public delegate void DelegateNumber(int n);

    public class MainForm : System.Windows.Forms.Form
        public DelegateNumber m_DelegateNumber;

        m_DelegateNumber = new DelegateNumber(this.AddNumber);

        private void AddNumber(int n)
          // add number to textbox here

    Thread code (assuming that m_form is reference to the form):

    int n = 5;   // some number

    m_form.Invoke(m_form.m_DelegateNumber, new Object[] {n});


    m_form.BeginInvoke(m_form.m_DelegateNumber, new Object[] {n});
    LVL 96

    Expert Comment

    by:Bob Learned
    Why don't you create a static function so that you don't need an instantiated class to retrieve a random number?  It would be like String.Format, Array.Sort, etc.

    LVL 9

    Author Comment

    TheLearnedOne, can you give me an example for my case?

    Featured Post

    Hire Top Freelancers to Complete C# Projects

    Source the talented Expert Exchange community
    for top quality work on your C# projects.

    Hire the best. Collaborate easily. Get quality work.

    Join & Write a Comment

    Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
    Article by: Najam
    Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now