Click here to Skip to main content
15,895,709 members
Articles / Desktop Programming / Windows Forms

BSEtunes

Rate me:
Please Sign up or sign in to vote.
4.67/5 (11 votes)
24 Apr 2010CPOL4 min read 64.8K   4.3K   58  
BSEtunes is a MySQL based, full manageable, networkable single or multiuser jukebox application
using System;
using System.Data;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

using BSE.Charts;

namespace BSE.Platten.Statistik
{
	/// <summary>
	/// Zusammenfassung f�r CCalculateStatistic.
	/// </summary>
	public class CCalculateStatistic
	{
        #region Delegates

        private delegate void DelegateGetSpielzeit(string[][] aSpielzeit);
        private delegate void DelegateGetStatistik(string[][] aStatistik);
        private delegate void DelegateGetChart(ChartItem[] chartItems);

        #endregion

        #region EventsPublic

        public event System.EventHandler CancelCalculating;
		public event System.EventHandler CompleteCalculating;

		#endregion

		#region FieldsPrivate
		
		private System.Windows.Forms.Cursor m_cursor;
		private System.Windows.Forms.ListView m_lstvSpielzeit;
		private System.Windows.Forms.ListView m_lstvStatistik;
		private Chart m_chart;
		private bool m_cancelCalculating;
		private System.Threading.Thread m_threadCalculateStatistic;
		private System.Timers.Timer m_timer = new System.Timers.Timer();
		private System.Data.DataSet m_dataSetStatisticInformations;

		#endregion

		#region MethodsPublic

		public CCalculateStatistic(
			System.Data.DataSet dataSetStatisticInformations,
			System.Windows.Forms.ListView lstvSpielzeit,
			System.Windows.Forms.ListView lstvStatistik,
			Chart chart,
			System.Windows.Forms.Cursor cursor
			)
		{
			this.m_dataSetStatisticInformations = dataSetStatisticInformations;
            if (this.m_dataSetStatisticInformations != null)
            {
                this.m_lstvSpielzeit = lstvSpielzeit;
                this.m_lstvStatistik = lstvStatistik;
                this.m_chart = chart;
                this.m_cursor = cursor;

                this.m_threadCalculateStatistic =
                    new System.Threading.Thread(
                    new System.Threading.ThreadStart(this.CalculateStatistik));

                this.m_threadCalculateStatistic.Start();

                this.m_timer.Elapsed += new System.Timers.ElapsedEventHandler(this.Timer_Elapsed);
                this.m_timer.Enabled = true;
                this.m_timer.Start();
            }
		}

		public System.Threading.Thread Thread()
		{
			return this.m_threadCalculateStatistic;
		}

		public void CancelCalculate()
		{
            if (this.m_cancelCalculating == false)
            {
                this.m_cancelCalculating = true;
                if (this.m_threadCalculateStatistic.ThreadState == System.Threading.ThreadState.Running)
                {
                    if (this.m_timer != null)
                    {
                        this.m_timer.Stop();
                    }
                    this.m_threadCalculateStatistic.Abort();
                }
                if (CancelCalculating != null)
                {
                    CancelCalculating(this, new System.EventArgs());
                }
            }
		}

		#endregion

		#region MethodsProtected
		#endregion

		#region MethodsPrivate

		private void CalculateStatistik()
		{
			this.m_cursor = Cursors.WaitCursor;
			int iSpielzeitSecond = 0;
			double iSpielzeitMinute = 0;
			double iSpielzeitHour = 0;
			double iSpielzeitDay = 0;
			double iSpielzeitWeek = 0;
			double iSpielzeitYear = 0;

			if (this.m_dataSetStatisticInformations.Tables["Spielzeit"].Rows[0]["Spielzeit"] != System.DBNull.Value)
			{
				iSpielzeitSecond = (int)this.m_dataSetStatisticInformations.Tables["Spielzeit"].Rows[0]["Spielzeit"];
				iSpielzeitMinute = Convert.ToDouble(iSpielzeitSecond) / 60;
				iSpielzeitHour = Convert.ToDouble(iSpielzeitSecond) / 60 / 60;
				iSpielzeitDay = Convert.ToDouble(iSpielzeitSecond) / 60 / 60 / 24;
				iSpielzeitWeek = Convert.ToDouble(iSpielzeitSecond) / 60 / 60 / 24 / 7;
				iSpielzeitYear = Convert.ToDouble(iSpielzeitSecond) / 60 / 60 / 24 / 365;
			}
			string[][]  arraySpielzeit = new string[6][];
            arraySpielzeit[0] = new String[] { "Die Spielzeit betr�gt:", iSpielzeitSecond.ToString("F"), "Sekunden" };
            arraySpielzeit[1] = new String[] { string.Empty, iSpielzeitMinute.ToString("F"), "Minuten" };
            arraySpielzeit[2] = new String[] { string.Empty, iSpielzeitHour.ToString("F"), "Stunden" };
            arraySpielzeit[3] = new String[] { string.Empty, iSpielzeitDay.ToString("F"), "Tage" };
            arraySpielzeit[4] = new String[] { string.Empty, iSpielzeitWeek.ToString("F"), "Wochen" };
            arraySpielzeit[5] = new String[] { string.Empty, iSpielzeitYear.ToString("F"), "Jahre" };

            this.m_lstvSpielzeit.Invoke(new DelegateGetSpielzeit(GetSpielzeit), new Object[] { arraySpielzeit });

			int iCountPlatten = (int)this.m_dataSetStatisticInformations.Tables["CountPlatten"].Rows[0]["CountPlatten"];
			int iCountRecordedPlatten = (int)this.m_dataSetStatisticInformations.Tables["CountRecordedPlatten"].Rows[0]["CountRecordedPlatten"];
			int iCountTracks = (int)this.m_dataSetStatisticInformations.Tables["CountTracks"].Rows[0]["CountTracks"];

			string[][]  arrayStatistik = new string[3][];
			arrayStatistik[0] = new String[]
                {"Im System befinden sich",iCountPlatten.ToString(),"Platten"};
			arrayStatistik[1] = new String[]
                {"davon sind",iCountRecordedPlatten.ToString(),"aufgenommen"};
			arrayStatistik[2] = new String[]
                {"das sind",iCountTracks.ToString(),"mp3 Lieder"};

            this.m_lstvStatistik.Invoke(new DelegateGetSpielzeit(GetStatistik), new Object[] { arrayStatistik });

            DataRowCollection dataRows = this.m_dataSetStatisticInformations.Tables["TitelGroupByMedium"].Rows;
            ChartItem[] chartItems = new ChartItem[dataRows.Count];
            int iCounter = 0;
            foreach (DataRow dataRow in dataRows)
            {
                ChartItem chartItem = new ChartItem();
                chartItem.Text = dataRow["Medium"].ToString();
                chartItem.Value = float.Parse(dataRow["Count"].ToString());
                chartItems[iCounter] = chartItem;
                iCounter++;
            }
            this.m_chart.Invoke(new DelegateGetChart(GetChart), new Object[] { chartItems });
			
			this.m_cursor = Cursors.Default;
		}

		private void GetSpielzeit(string[][] aSpielzeit)
		{
            if (aSpielzeit != null)
            {
                int iArrayLength;
                iArrayLength = aSpielzeit.Length;

                for (int i = 0; i < iArrayLength; i++)
                {
                    this.m_lstvSpielzeit.Items.Add(new ListViewItem(aSpielzeit[i]));
                    ListViewItem listViewItem = this.m_lstvSpielzeit.Items[i];
                    listViewItem.UseItemStyleForSubItems = false;
                    listViewItem.SubItems[1].ForeColor = Color.Red;
                }
            }
		}

		private void GetStatistik(string[][] aStatistik)
		{
			if (aStatistik  != null)
			{
				int iArrayLength;
				iArrayLength = aStatistik.Length;

				for (int i = 0; i < iArrayLength; i++)
				{
                    this.m_lstvStatistik.Items.Add(new ListViewItem(aStatistik[i]));
                    ListViewItem listViewItem = this.m_lstvStatistik.Items[i];
                    listViewItem.UseItemStyleForSubItems = false;
                    listViewItem.SubItems[1].ForeColor = Color.Red;
				}
			}
		}
        private void GetChart(ChartItem[] chartItems)
        {
            if (chartItems != null)
            {
                foreach (ChartItem chartItem in chartItems)
                {
                    this.m_chart.ChartItems.Add(chartItem);
                }
            }
        }

		private void Timer_Elapsed(object sender,System.Timers.ElapsedEventArgs e)
		{
			if (this.m_threadCalculateStatistic != null)
			{
				if (this.m_threadCalculateStatistic.ThreadState == System.Threading.ThreadState.Stopped)
				{
					this.m_timer.Stop();
                    if (this.CompleteCalculating != null)
                    {
                        this.CompleteCalculating(this, new System.EventArgs());
                    }
				}
			}
		}

		#endregion

	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Switzerland Switzerland
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions