Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Events with thread

Posted on 2009-07-14
4
Medium Priority
?
191 Views
Last Modified: 2012-05-07
So then I have two more questions. (sorry)

1. If I run this example I can press subscribe...the clock starts......I press unsubscribe and the clock stops. Why Aint I able to press subscribe again and it will start ticking again?

2. Is it possible to make a "start form" from where I can fire this Form1 I have now? That means I can fire up alot of diffrent Form1


Clock.CS:
______________________________
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
 
namespace Modul3_Malmoe
{
    public class Clock
    {
        // Private Fields holding the hour, minute and second
        private int _hour;
        private int _minute;
        private int _second;
        private bool IsSubscribed;
        private bool _bSubscribe;
 
 
        // The delegate named SecondChangeHandler, which will encapsulate
        // any method that takes a clock object and a TimeInfoEventArgs
        // object as the parameter and returns no value. It's the
        // delegate the subscribers must implement.
        public delegate void SecondChangeHandler(object clock, TimeInfoEventArgs timeInformation);
 
        // The event we publish
        public event SecondChangeHandler SecondChange;
 
 
        public Clock()
        {
            _bSubscribe = true;
        }
 
 
        #region Egenskaper
 
 
        #endregion
 
        // The method which fires the Event
        protected void OnSecondChange(object clock, TimeInfoEventArgs timeInformation)
        {
            // Check if there are any Subscribers
            if (SecondChange != null)
            {
                // Call the Event
                SecondChange(clock, timeInformation);
            }
        }
 
 
        /// <summary>
        /// only changing private field _bSubscribe
        /// </summary>
        public void unsunscribe()
        {
            _bSubscribe = false;
        }
 
 
 
        // Set the clock running, it will raise an
        // event for each new second
        public void Run(System.Windows.Forms.TextBox timeTextBox)
        {
            for (; ; )
            {
 
                // Sleep 1 Second
                Thread.Sleep(1000);
 
                // Get the current time
                System.DateTime dt = System.DateTime.Now;
 
                // If the second has changed
                // notify the subscribers
                if (dt.Second != _second)
                {
                    int hours = dt.Hour;
                    string hh1 = Convert.ToString(hours / 10);
                    string hh2 = Convert.ToString(hours % 10);
                    string hh3 = hh1 + hh2;
 
                    int minute = dt.Minute;
                    string mm1 = Convert.ToString(minute / 10);
                    string mm2 = Convert.ToString(minute % 10);
                    string mm3 = mm1 + mm2;
 
                    int seconds = dt.Second;
                    string ss1 = Convert.ToString(seconds / 10);
                    string ss2 = Convert.ToString(seconds % 10);
                    string ss3 = ss1 + ss2;
 
                    // Create the TimeInfoEventArgs object
                    // to pass to the subscribers
                    TimeInfoEventArgs timeInformation = new TimeInfoEventArgs(Convert.ToInt32(hh3), Convert.ToInt32(mm3), Convert.ToInt32(ss3));
 
                    // update the state
                    _second = dt.Second;
                    _minute = dt.Minute;
                    _hour = dt.Hour;
 
                    // If anyone has subscribed, notify them
                    if (_bSubscribe) // if it is subscribed then show in textBox
                    {
 
                        OnSecondChange(this, timeInformation);
                    }
 
                    else
                        return;  // this will end your for loop
 
                }
 
 
            }
 
 
        }
 
    }
 
}
 
 
 
 
Form1.Cs
_______________________
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;
 
namespace Modul3_Malmoe
{
    public partial class Form1 : Form
    {
 
        // Create a new clock
        Clock theClock;
        private bool flag = true;
 
        // Create the display
        private DisplayClock dc = new DisplayClock();
 
        // Create a Log object and tell it
        LogClock lc = new LogClock();
 
 
        public Form1()
        {
            InitializeComponent();
            theClock = new Clock();
            unsubscribeButton.Enabled = false;
            theClock.SecondChange += new Clock.SecondChangeHandler(theClock_SecondChange);
 
        }
 
        void theClock_SecondChange(object clock, TimeInfoEventArgs timeInformation)
        {
            MethodInvoker mi = delegate()
            {
                timeTextBox.Text = timeInformation.hour.ToString() + "." +
                    timeInformation.minute.ToString() + "." +
                    timeInformation.second.ToString();
                timeTextBox.Parent.Update();
            };
            updateUI(mi);
        }
 
        private void updateUI(MethodInvoker mi)
        {
            if (this.InvokeRequired)
                this.Invoke(mi);
            else
                mi();
        }
 
        private void subscribeButton_Click(object sender, EventArgs e)
        {
            subscribeButton.Enabled = false;
            unsubscribeButton.Enabled = true;
 
            // subscribe to the clock just created                
            dc.Subscribe(theClock);
 
            // to subscribe to the clock
            lc.Subscribe(theClock);
 
            // Get the clock started    
 
            MethodInvoker mi = delegate()
            {
                theClock.Run(timeTextBox);
            };
            mi.BeginInvoke(null, null);
 
 
        }
 
        private void unsubscribeButton_Click(object sender, EventArgs e)
        {
            //theClock.Subscribed = false;
            theClock.unsunscribe();
            unsubscribeButton.Enabled = false;
            subscribeButton.Enabled = true;
 
 
        }
 
 
    }
}

Open in new window

0
Comment
Question by:Mickeys
  • 2
  • 2
4 Comments
 
LVL 11

Expert Comment

by:saragani
ID: 24847373
Well, the first problem would be that Subscribe causing you to register the events buts unsubscribe
doesn't. This means that when you click on subscribe again then you will have the event registered twice.


For this we will do:
        private void unsubscribeButton_Click(object sender, EventArgs e)
        {
            theClock.unsunscribe();
            unsubscribeButton.Enabled = false;
            subscribeButton.Enabled = true;

            // unsubscribe to the clock just created                
            dc.UnSubscribe(theClock);

            // to unsubscribe to the clock
            lc.UnSubscribe(theClock);
        }


This still doesn't solve the problem that you have.


Furthermore, you are doing a loop using for but without a start or end index... so why using a for while you can use a While?




while (_bSubscribe)
            {

                // Sleep 1 Second
                Thread.Sleep(1000);

                // Get the current time
                System.DateTime dt = System.DateTime.Now;

                // If the second has changed
                // notify the subscribers
                if (dt.Second != _second)
                {
                    int hours = dt.Hour;
                    string hh1 = Convert.ToString(hours / 10);
                    string hh2 = Convert.ToString(hours % 10);
                    string hh3 = hh1 + hh2;

                    int minute = dt.Minute;
                    string mm1 = Convert.ToString(minute / 10);
                    string mm2 = Convert.ToString(minute % 10);
                    string mm3 = mm1 + mm2;

                    int seconds = dt.Second;
                    string ss1 = Convert.ToString(seconds / 10);
                    string ss2 = Convert.ToString(seconds % 10);
                    string ss3 = ss1 + ss2;

                    // Create the TimeInfoEventArgs object
                    // to pass to the subscribers
                    TimeInfoEventArgs timeInformation = new TimeInfoEventArgs(Convert.ToInt32(hh3), Convert.ToInt32(mm3), Convert.ToInt32(ss3));

                    // update the state
                    _second = dt.Second;
                    _minute = dt.Minute;
                    _hour = dt.Hour;

                    // If anyone has subscribed, notify them
                    if (_bSubscribe) // if it is subscribed then show in textBox
                    {

                        OnSecondChange(this, timeInformation);
                    }
                }
            }



The reason why the loop doesn't restart is because bSubscribe is false. You set it to tru when you create the clock object, and set it to false when you unsubscribe.
But you don't set it to true again.



So we will change the Run Function (Since it acts like a subscrube):



public void Run()
        {
            _bSubscribe = true;
            while (_bSubscribe)
            {

                // Sleep 1 Second
                Thread.Sleep(1000);

                // Get the current time
                System.DateTime dt = System.DateTime.Now;

                // If the second has changed
                // notify the subscribers
                if (dt.Second != _second)
                {
                    int hours = dt.Hour;
                    string hh1 = Convert.ToString(hours / 10);
                    string hh2 = Convert.ToString(hours % 10);
                    string hh3 = hh1 + hh2;

                    int minute = dt.Minute;
                    string mm1 = Convert.ToString(minute / 10);
                    string mm2 = Convert.ToString(minute % 10);
                    string mm3 = mm1 + mm2;

                    int seconds = dt.Second;
                    string ss1 = Convert.ToString(seconds / 10);
                    string ss2 = Convert.ToString(seconds % 10);
                    string ss3 = ss1 + ss2;

                    // Create the TimeInfoEventArgs object
                    // to pass to the subscribers
                    TimeInfoEventArgs timeInformation = new TimeInfoEventArgs(Convert.ToInt32(hh3), Convert.ToInt32(mm3), Convert.ToInt32(ss3));

                    // update the state
                    _second = dt.Second;
                    _minute = dt.Minute;
                    _hour = dt.Hour;

                    // If anyone has subscribed, notify them
                    if (_bSubscribe) // if it is subscribed then show in textBox
                    {

                        OnSecondChange(this, timeInformation);
                    }
                }
            }
        }


I've removed the Textbox from the arguments of the Run since we don't need it anymore and I've also put a _bSubscribe = true before the loop starts.

This answers Q1.



Q2, yes.
each form will have it's own clock objects so you can have multiple forms.
0
 

Author Comment

by:Mickeys
ID: 24847445
This is great now I start to understand.

So lets say I have done a startup form with a button called START

How can I then fire up multiplie FORM1?

I mean if I press once I want to start one FORM1. If I press again a new one starts.

Is it
Form1 myform = new Form1()

and then????
0
 
LVL 11

Accepted Solution

by:
saragani earned 2000 total points
ID: 24847560
Ok, I was away. I just saw your question

add a new form: Form2
go to program.cs and change it so it will run Form2 instead of Form1

on Form2 add a button and have the following code in the click event:

Form1 myform = new Form1();
myform.Show();
0
 

Author Closing Comment

by:Mickeys
ID: 31603173
Thx for everything.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

783 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