Simple Multi Thread and Control Interaction Without Using Worker in C#


Well, I posted a code for simple multi thread and control interaction without using worker in c#. Maybe it will end up somewhere. *grin*

#region attributes
bool FlagStop
{
    get
    {
        return flagStop;
    }
    set
    {
        flagStop = value;
        OnFlagChanged();
    }
}
private bool flagStop;
AgeModel PersonAge = new AgeModel(365);
Thread AgeThread = null;
Thread EventThread = null;
delegate void MyDelegate();
MyDelegate AgeDelegate;
MyDelegate EventDelegate;
#endregion
#region initialization
public Form1()
{
    InitializeComponent();
    FlagStop = false;
    EventDelegate = new MyDelegate(PerformEventRandomizer);
    AgeDelegate = new MyDelegate(PerformAgeAddition);
}
#endregion
#region functions
#region event functions
private void button1_Click(object sender, EventArgs e)
{
    FlagStop = false;
    if (EventThread == null)
    {
        EventThread = new Thread(new ThreadStart(ProcessEvent));
        EventThread.Start();
    }
    else if (EventThread != null && !EventThread.IsAlive)
    {
        EventThread.Interrupt();//.Resume();
    }

    if (AgeThread == null)
    {
        AgeThread = new Thread(new ThreadStart(ProcessAge));
        AgeThread.Start();
    }
    else if (AgeThread != null && !AgeThread.IsAlive)
    {
        AgeThread.Interrupt();
    }
}

private void button2_Click(object sender, EventArgs e)
{
    FlagStop = true;
}

protected virtual void OnFlagChanged()
{
    if (FlagStop)
    {
        ThreadOneStat.Text = "Stop";
        ThreadTwoStat.Text = "Stop";
        if (EventThread != null)
        {
            EventThread.Join();
            EventThread.Abort();
            EventThread = null;
            GC.Collect();
        }
        if (AgeThread != null)
        {
            AgeThread.Join();
            AgeThread.Abort();
            AgeThread = null;
            GC.Collect();
        }
    }
}
#endregion
#region delegate functions
public void PerformAgeAddition()
{
    ThreadOneStat.Text = "Start";
    lblAge.Text = PersonAge.ToString();
    PersonAge.Day += 1;
    if (lblEvent.Text.Equals("0"))
    {
        FlagStop = true;
    }
}

public void PerformEventRandomizer()
{
    ThreadTwoStat.Text = "Start";
    lblEvent.Text = (new Random()).Next(0, 100).ToString();
    Console.WriteLine(lblEvent.Text);
}
#endregion
#region thread functions
public void ProcessAge()
{
    while (!FlagStop)
    {
        this.Invoke(AgeDelegate);
        Thread.Sleep(2000);
    }
}

public void ProcessEvent()
{
    while (!FlagStop)
    {
        this.Invoke(EventDelegate);
        Thread.Sleep(2000);
    }
}
#endregion
#endregion
Advertisements

Author: Aditya Yedija Situmeang

Developer, Lover, Christian, Omnomnomnivore, Gamer.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s