Monday, September 9, 2013

Timer untuk Program Olympiade Astronomi

Berikut ini adalah contoh bagaimana cara membuat timer yang dapat diset pada halaman admin. yang pertama untuk mengatur berapa banyak waktu yang dibutuhkan dalam pertandingan terlebih dahulu atur waktuna pada halaman set timer yang ada pada aplikasi admin IOAA. Dibawah ini merupakan tampilan XMLnya.

sciprt  utamanya adalah sebagai berikut :
          bool hasil = await MyService.InsertTimerAsync(new ServiceReference1.Timer
            {
                startTime = txtdate.Text + " " + txttime.Text,
                endTime = Convert.ToInt16(txtduration.Text)

            });
            if (hasil)
            {
                MessageBox.Show("Timer was Set");               
            }
            else
            {
                MessageBox.Show("Cannot Set");
            }
dimana script diatas memanggil servise InsertTimerAsync yang merupakan service dari WCF yg berisi sript seperti dibawah ini:
public bool InsertTimer(Timer TimerInsert)
        {
            string strMessage = string.Empty;
            con.Open();
            SqlCommand cmd = new SqlCommand("update tbl_timer set start_time=@start_time,end_time=DATEADD(MINUTE,@end_time,@start_time)", con);
            cmd.Parameters.AddWithValue("@start_time", TimerInsert.startTime);
            cmd.Parameters.AddWithValue("@end_time", TimerInsert.endTime);
            try
            {
                int result = cmd.ExecuteNonQuery();

                con.Close();
                if (result == 1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception e)
            {
                return false;
            }           
        }
Setelah settingan untuk timer sudah dibuat maka cara membuat timer dan mencocokkan hasil settingan timer adalah sebagai berikut :
public void timer_Tick(object sender, object e)
        {
            new Action(async () =>
            {
                bool cek = await service.checkTimerAsync();

                if (cek)
                {
                    txtWarning.Content = "";
                    gridSoal.Visibility = Visibility.Visible;
                    var c = await service.GetTimeAsync();
                    foreach (var cou in c)
                    {
                        sec = cou.Timer;
                    }
                    jam = sec / 3600;
                    int b = jam * 3600;
                    menit = (sec - b) / 60;
                    detik = (sec - b) % 60;



                    // Else continue counting.
                    if (detik < 1)
                    {
                        detik = 59;
                        if (menit == 0)
                        {
                            menit = 59;
                            if (jam != 0)
                                jam -= 1;

                        }
                        else
                        {
                            menit -= 1;
                        }
                    }
                    else
                        detik -= 1;
                    // Display the current values of hours, minutes and seconds in
                    // the corresponding fields.
                    t0.Text = "Time :";
                    t1.Text = ":";
                    t2.Text = ":";
                    txtJam.Text = jam.ToString();
                    txtMenit.Text = menit.ToString();
                    txtDetik.Text = detik.ToString();
                    if ((jam == 0) && (menit == 0) && (detik == 0))
                    {
                        // If the time is over, clear all settings and fields.
                        // Also, show the message, notifying that the time is over.

                        // this.Frame.Navigate(typeof(HalamanPeserta), rootPage);
                        t0.Text = "";
                        t1.Text = "";
                        t2.Text = "";
                        txtJam.Text = "";
                        txtMenit.Text = "";
                        txtDetik.Text = "";
                        gridSoal.Visibility = Visibility.Collapsed;
                        txtWarning.Content = "End Competition";
                        timer.Stop();
                    }
                }
                else
                {
                    // MessageBox.Show("Not the competition time");
                    gridSoal.Visibility = Visibility.Collapsed;
                    t0.Text = "";
                    t1.Text = "";
                    t2.Text = "";
                    txtJam.Text = "";
                    txtMenit.Text = "";
                    txtDetik.Text = "";
                    txtWarning.Content = "Not the competition time";

                }
            }).Invoke();


        }

No comments:

Post a Comment