Click here to Skip to main content
15,924,036 members
Home / Discussions / C#
   

C#

 
GeneralRe: How get html source code of WebPage? Pin
aleXXXka22-Feb-09 7:50
aleXXXka22-Feb-09 7:50 
Questionupdating a label from a thread in a different class Pin
liamderice19-Feb-09 10:59
liamderice19-Feb-09 10:59 
AnswerRe: updating a label from a thread in a different class Pin
DaveyM6919-Feb-09 11:12
professionalDaveyM6919-Feb-09 11:12 
GeneralRe: updating a label from a thread in a different class Pin
liamderice20-Feb-09 4:48
liamderice20-Feb-09 4:48 
GeneralRe: updating a label from a thread in a different class Pin
DaveyM6920-Feb-09 6:02
professionalDaveyM6920-Feb-09 6:02 
GeneralRe: updating a label from a thread in a different class Pin
liamderice20-Feb-09 9:50
liamderice20-Feb-09 9:50 
GeneralRe: updating a label from a thread in a different class Pin
DaveyM6920-Feb-09 10:06
professionalDaveyM6920-Feb-09 10:06 
GeneralRe: updating a label from a thread in a different class Pin
DaveyM6920-Feb-09 10:27
professionalDaveyM6920-Feb-09 10:27 
This works.
using System;
using System.Threading;
using System.Windows.Forms;

public partial class FormMain : Form
{
    public FormMain()
    {
        InitializeComponent();
        Shown += new EventHandler(Form1_Shown);
    }
    void Form1_Shown(object sender, EventArgs e)
    {
        Thread CopyThread = new Thread(new ThreadStart(DoStuff));
        CopyThread.Start();
    }
    void UpdateLabel(string text)
    {
        label1.Text = text;
    }
    void DoStuff()
    {
        Files files = new Files();
        files.TextUpdate += new EventHandler<TextEventArgs>(files_TextUpdate);
        files.CountToTen();
        files.TextUpdate -= files_TextUpdate;
    }
    void files_TextUpdate(object sender, TextEventArgs e)
    {
        BeginInvoke(new MethodInvoker(
            delegate() { UpdateLabel(e.Text); })
            );
    }
}

public class Files
{
    public event EventHandler<TextEventArgs> TextUpdate;
    public void CountToTen()
    {
        for (int i = 0; i <= 10; i++)
        {
            OnTextUpdate(new TextEventArgs(String.Format("Counted to {0}", i)));
            Thread.Sleep(1000);
        }
    }
    protected virtual void OnTextUpdate(TextEventArgs e)
    {
        EventHandler<TextEventArgs> eh = TextUpdate;
        if (eh != null)
            eh(this, e);
    }
}

public class TextEventArgs : EventArgs
{
    private string _Text;
    public TextEventArgs(string text)
    {
        _Text = text;
    }
    public string Text
    {
        get { return _Text; }
    }
}


Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

AnswerRe: updating a label from a thread in a different class Pin
yuxuanji19-Feb-09 15:01
yuxuanji19-Feb-09 15:01 
AnswerRe: updating a label from a thread in a different class Pin
Luc Pattyn19-Feb-09 15:54
sitebuilderLuc Pattyn19-Feb-09 15:54 
QuestionGeneric Control WinForms and Toolbox/Designer Pin
DaveyM6919-Feb-09 10:54
professionalDaveyM6919-Feb-09 10:54 
AnswerRe: Generic Control WinForms and Toolbox/Designer Pin
Luc Pattyn19-Feb-09 11:34
sitebuilderLuc Pattyn19-Feb-09 11:34 
GeneralRe: Generic Control WinForms and Toolbox/Designer Pin
DaveyM6919-Feb-09 11:44
professionalDaveyM6919-Feb-09 11:44 
AnswerRe: Generic Control WinForms and Toolbox/Designer Pin
Luc Pattyn19-Feb-09 11:56
sitebuilderLuc Pattyn19-Feb-09 11:56 
QuestionI need help whit my programme Pin
ZRF6919-Feb-09 10:43
ZRF6919-Feb-09 10:43 
AnswerRe: I need help whit my programme Pin
Deresen19-Feb-09 11:36
Deresen19-Feb-09 11:36 
AnswerRe: I need help whit my programme Pin
Mycroft Holmes19-Feb-09 13:49
professionalMycroft Holmes19-Feb-09 13:49 
AnswerRe: I need help whit my programme Pin
DaveyM6919-Feb-09 14:23
professionalDaveyM6919-Feb-09 14:23 
QuestionPlease help me with this simple C question Pin
meixiang619-Feb-09 10:00
meixiang619-Feb-09 10:00 
AnswerRe: Please help me with this simple C question Pin
Luc Pattyn19-Feb-09 10:35
sitebuilderLuc Pattyn19-Feb-09 10:35 
Questionproblem whit WS Pin
E_Gold19-Feb-09 9:59
E_Gold19-Feb-09 9:59 
AnswerRe: problem whit WS Pin
Dave Kreskowiak19-Feb-09 11:50
mveDave Kreskowiak19-Feb-09 11:50 
GeneralRe: problem whit WS Pin
E_Gold19-Feb-09 19:53
E_Gold19-Feb-09 19:53 
GeneralRe: problem whit WS Pin
Dave Kreskowiak20-Feb-09 1:47
mveDave Kreskowiak20-Feb-09 1:47 
QuestionAuto Synch with Website Pin
Richard Andrew x6419-Feb-09 9:55
professionalRichard Andrew x6419-Feb-09 9:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.