Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

C# - Button with time

Posted on 2011-10-28
16
Medium Priority
?
488 Views
Last Modified: 2012-05-12
Button with time to perform the tasks for a period and stop automatically in time configured.
Any idea is welcome.

ocaccy
0
Comment
Question by:ocaccy
  • 7
  • 6
  • 2
  • +1
16 Comments
 
LVL 5

Accepted Solution

by:
mcs0506 earned 224 total points
ID: 37044048
Hi,
You can add Timer object in you form, On form load you set its time and when button is pressed set enable=true to start timer. your job that you want to perform in that specific time will written in its tick event and also make check in tick event when time reached then again set timer.enable=false.

Regards

Dani
0
 
LVL 37

Assisted Solution

by:TommySzalapski
TommySzalapski earned 448 total points
ID: 37044588
If I understand correctly and you want to keep working until a timer goes off then you can just check the system time.
If you want the process to work for 5 seconds, you could do

DateTime stopTime = DateTime.Now + TimeSpan(0, 0, 0, 5);
while(stopTime > DateTime.Now)
{
  //do code
}
0
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 1328 total points
ID: 37044624
*If that's running in the main UI thread then you'll need DoEvents() in that polling loop.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 37

Assisted Solution

by:TommySzalapski
TommySzalapski earned 448 total points
ID: 37044662
You also might want a Thread.Sleep(100) or something in the loop so it's not chewing up as much processor time. Depends on what it's doing, but my guess is that that's the best option.
0
 

Author Comment

by:ocaccy
ID: 37060059
Hi,

If I need 10 minutes,

This is OK?
DateTime stopTime = DateTime.Now + TimeSpan(0, 0, 10, 0);
while(stopTime > DateTime.Now)
{
  //do code
}

Open in new window

0
 

Author Comment

by:ocaccy
ID: 37097775
timespan timespan2
Why?
0
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 1328 total points
ID: 37097789
Use the AddSeconds(), AddMinutes(), etc methods...

Example:

    DateTime stopTime = DateTime.Now.AddSeconds(2);

Or:

    DateTime stopTime = DateTime.Now.AddMinutes(10);

*You can add a TimeSpan, too, if you really want to:

    DateTime stopTime = DateTime.Now.Add(new TimeSpan(0, 0, 10, 0));
0
 

Author Comment

by:ocaccy
ID: 37098054
Not stopping and not delay the execution of the program.
How to play an mp3 of 1.5 seconds here?

ocaccy
/// <summary>
        /// COUNT 1st time, 2nd time and 3rd time.
        /// </summary>
        private void timer3_Tick(object sender, EventArgs e)
        {
            int i_KyuSoku = Convert.ToInt32(nUD_KyuSoku.Value);
            int i_RyuSoku = Convert.ToInt32(nUD_RyuSoku.Value);
            int i_Seiti = Convert.ToInt32(nUD_Seiti.Value);
            int i_2sEstagios = i_KyuSoku + i_RyuSoku;
            int i_3sEstagios = i_KyuSoku + i_RyuSoku + i_Seiti;

            TimeSpan ts = DateTime.Now.Subtract(inicio);
            lbl_crono.Text = (string.Format("{0:00}:{1:00}:{2:00}", (int)ts.TotalHours, ts.Minutes, ts.Seconds));

            TimeSpan ts1 = TimeSpan.FromSeconds(i_KyuSoku + 1);
            string txtDate1 = string.Format("Full time: {0}", new DateTime(ts.Ticks).ToString("HH:mm:ss"));

            TimeSpan ts2 = TimeSpan.FromSeconds(i_RyuSoku + 1);
            string txtDate2 = string.Format("Full time: {0}", new DateTime(ts.Ticks).ToString("HH:mm:ss"));

            TimeSpan ts3 = TimeSpan.FromSeconds(i_Seiti + 1);
            string txtDate3 = string.Format("Full time: {0}", new DateTime(ts.Ticks).ToString("HH:mm:ss"));

            TimeSpan ts22 = TimeSpan.FromSeconds(i_2sEstagios + 1);
            //string txtDate3 = string.Format("Full time: {0}", new DateTime(ts.Ticks).ToString("HH:mm:ss"));

            TimeSpan ts33 = TimeSpan.FromSeconds(i_3sEstagios);
            //string txtDate3 = string.Format("Full time: {0}", new DateTime(ts.Ticks).ToString("HH:mm:ss"));

            if (ts <= ts1)
            {
                label152.Visible = false;
                label153.Visible = false;
                label154.Visible = false;
                label155.Visible = false;
                label151.Visible = true;
            }

            if (ts > ts1 && ts <= ts22)
            {
                label151.Visible = false;
                label152.Visible = true;
            }

            if (ts > ts1 && ts > ts22 && ts <= ts33)
            {
                // i see one stop in the flow with this:    
                DateTime stopTime = DateTime.Now.AddSeconds(2);
                while (stopTime > DateTime.Now)
                {
                     System.Media.SystemSounds.Beep.Play();
                    // Not stopping and not delay the execution 
                    // of the program.
                    // How to play an mp3 of 1.5 seconds here? 
                }
               
                label151.Visible = false;
                label152.Visible = false;
                label153.Visible = true;
                label154.Visible = true;
 
            }

            if (ts > ts33)
            {
                timer3.Stop();
                timer_10.Stop();
                label151.Visible = false;
                label152.Visible = false;
                label153.Visible = false;
                label154.Visible = false;
                label155.Visible = true;
                //timer_10.Enabled = true;
                
            }
        }

Open in new window

0
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 1328 total points
ID: 37098205
The SoundPlayer() class can play .wav files...but can't do MP3s.

Do you want to play your own sound file?...or loop the system beep for 1.5 seconds?
0
 

Author Comment

by:ocaccy
ID: 37098239
I still do not know how to use, so I put the code as the previous model.

I have a time routine.
Also has an alarm sound with duration of 1.5 seconds.
This format mpe3, but can transform to wav.

When give a x time, I want to play this sound, but I want to continue running normally.
0
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 1328 total points
ID: 37098242
If you convert it to a .wav then you can use the SoundPlayer class and the Play() method:
http://msdn.microsoft.com/en-us/library/system.media.soundplayer.play(VS.90).aspx

    "Plays the .wav file using a new thread, and loads the .wav file first if it has not been loaded."
0
 

Author Comment

by:ocaccy
ID: 37098437
Ok, but, how to this sound play only one time?
As I did, every second restart, and processing delays.
0
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 1328 total points
ID: 37100031
?

If you only want it to play once then take it out of the loop.  Do want it to play, and then delay?...or delay and then play?
0
 

Author Comment

by:ocaccy
ID: 37111477
Hi,

sorry for the confusion.

I want to play a file of 1.5 seconds, just one time.
0
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 1328 total points
ID: 37111483
Place the call to Play() before your loop then.
0
 

Author Closing Comment

by:ocaccy
ID: 37111575
Hi, I put the play alone in another condition.
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

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

Introduction This question got me thinking... (http://www.experts-exchange.com/questions/28707487/GLOBALS.html) Why shouldn't we use Globals? This is a simple question without a simple answer.  How do you explain these concepts to a programmer w…
"Disruption" is the most feared word for C-level executives these days. They agonize over their industry being disturbed by another player - most likely by startups.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

810 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