CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2008
    Posts
    161

    [RESOLVED] Threads and Delegates

    Recently i have been learning how to create a multi-threaded application.

    I have three items: textbox1, texbox2, and label1

    In the non - UI thread i need the information from textbox1, and textbox2 for the thread to run successfully.

    i plan on using a for loop to do a certain command a certain amount of times specified by my for loop ('<=10').

    I also need to update label1.text on every time it goes through the for loop,
    every time it goes through the for loop i need to update my int count by increments of 1 ('count++').
    so 'label1.text = "Count: " + count.ToString();'

    I have read a few articles on using delegates to do this but i need help "Understanding" it.

    Can someone show me a quick example on how to do that?

    I have created my own thread i do not use the background worker.

    Thanks,

    Pale.

  2. #2
    Join Date
    May 2007
    Location
    Denmark
    Posts
    623

    Re: Threads and Delegates

    I made something very similar not long ago:

    Non-UI class:
    Code:
    public delegate void CountChangedHandler();
    public event CountChangedHandler CountChanged;
    UI-class
    Code:
    private int count = 0;
    
    //Put this in the constructor
    Driver.CountChangedHandler myCountChangedHandler = Increment;
    driver.CountChanged += myCountChangedHandler;
    
    public delegate void UpdatePostCount();
    
    private void Increment()
    {
        try
        {
            txtCount.Invoke(new UpdatePostCount(UpdateText));
        }
        catch (Exception x)
        {
        }
    }
    
    private void UpdateText()
    {
        try
        {
            count++;
            label1.Text = count.ToString();
        }
        catch
        {
        }
    }
    It's not a bug, it's a feature!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured