c# statements control

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 1736 Views - Last Post: 05 January 2012 - 06:38 AM Rate Topic: -----

#1 chaicomo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 07-August 11

c# statements control

Posted 04 January 2012 - 07:13 AM

Hi everybody, how can control a procedure based on time running, the ideia is to write to file with 30 seconds interval in c# codes. Its something like 1 minute from start program write to file a data received from serialport during a several minutes.
Waiting for regard.
Is This A Good Question/Topic? 0
  • +

Replies To: c# statements control

#2 Gavisann  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 103
  • View blog
  • Posts: 382
  • Joined: 01-July 11

Re: c# statements control

Posted 04 January 2012 - 07:20 AM

I believe that you are looking for a timer.

MSDN has a great article on timers with an explanation of each method along with examples.

This post has been edited by Gavisann: 04 January 2012 - 07:21 AM

Was This Post Helpful? 1
  • +
  • -

#3 chaicomo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 07-August 11

Re: c# statements control

Posted 04 January 2012 - 07:30 AM

i saw these article, nothing about statements, i need somethings like for (timer/datetime, timer/datetime<,++) or do, while, if statements based on times.
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5535
  • View blog
  • Posts: 11,857
  • Joined: 02-June 10

Re: c# statements control

Posted 04 January 2012 - 07:44 AM

It sounds like you are asking for the entire code. Are you familiar with C# at all?

Gavisann already lead you in the right direction. You use a timer. When the timer ticks you do whatever it is you need to do. If that is run your procedure (method) then that is what you do.

Do you have the rest of your program coded and are now just looking for a way to do it based on time? Or are you looking for help on the entire program?
Was This Post Helpful? 1
  • +
  • -

#5 chaicomo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 07-August 11

Re: c# statements control

Posted 04 January 2012 - 07:49 AM

i just have all coded, just looking a way to put on time based... i tried put a for statements using datatime keyword to counter, but no way to do thus, i put a timer component in form, but no highlite for... still looking a way...
Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5535
  • View blog
  • Posts: 11,857
  • Joined: 02-June 10

Re: c# statements control

Posted 04 January 2012 - 07:55 AM

If you have a method you need to run each time the timer ticks then it is easy.

  • Add the timer. Set the interval. Remember it is in milliseconds. So 30 seconds would be 30000
  • In the properties pallet look at the events.
  • Next to the .Tick event for the timer, double-click so Visual Studio adds a new handler for you.
  • In that handler, call your method.

Was This Post Helpful? 1
  • +
  • -

#7 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4496
  • View blog
  • Posts: 7,845
  • Joined: 08-June 10

Re: c# statements control

Posted 04 January 2012 - 08:08 AM

You're confused. There is no statement that you can just drop into place. You shouldn't use a loop for this, you use a timer with events. We've already linked you to the site you need, and tlhIn`toq has given you a high level overview. Please attempt to use a timer the way it's supposed to be used. If you can't figure that out, let us know.
Was This Post Helpful? 1
  • +
  • -

#8 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5535
  • View blog
  • Posts: 11,857
  • Joined: 02-June 10

Re: c# statements control

Posted 04 January 2012 - 08:12 AM

Perhaps you could show us the code you have so far. Its possible that it is architected so badly there isn't a single method to be called, which might account for why you can't find a way to add a timer.
Was This Post Helpful? 0
  • +
  • -

#9 chaicomo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 07-August 11

Re: c# statements control

Posted 04 January 2012 - 08:18 AM

i just find a possible way, as just you said me i check for the timer class, i saw it, now i know how is it work, but it need to a form come early with time, like "FormWithTimer", and i just walk for start to zero, i put a timer component into the form, starting write its giving error inheriting..., so how are you possible ideia for....
Was This Post Helpful? 0
  • +
  • -

#10 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4496
  • View blog
  • Posts: 7,845
  • Joined: 08-June 10

Re: c# statements control

Posted 04 January 2012 - 08:20 AM

If you're getting errors, copy and paste the text here. Don't try to explain it yourself, because we can barely understand you as it is.
Was This Post Helpful? 0
  • +
  • -

#11 chaicomo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 07-August 11

Re: c# statements control

Posted 04 January 2012 - 08:26 AM

A part of code which im editing: introducing a timer,


 public void ComPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {

            msg = ComPort.ReadExisting().ToString();
            StreamWriter escreve = new StreamWriter(@"c:\ dados.text");

            timer1 tempo = new timer1();   

            escreve.WriteLine(msg + "    " + DateTime.Now);
            escreve.Close();
            escreve.Dispose();

        }



The error:
1-Error 1 'Porta.Communications' does not contain a definition for 'timer1_Tick' and no extension method 'timer1_Tick' accepting a first argument of type 'Porta.Communications' could be found (are you missing a using directive or an assembly reference?) C:\Users\Chaicomo\Desktop\WindowsFormsApplication2\WindowsFormsApplication2\Form1.Designer.cs 517 62 ControladorSerialPort

2-Error 2 'Porta.Communications.timer1' is a 'field' but is used like a 'type' C:\Users\Chaicomo\Desktop\WindowsFormsApplication2\WindowsFormsApplication2\Form1.cs 140 13 ControladorSerialPort
Was This Post Helpful? 0
  • +
  • -

#12 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5535
  • View blog
  • Posts: 11,857
  • Joined: 02-June 10

Re: c# statements control

Posted 04 January 2012 - 08:33 AM

The errors aren't for the block of code you gave us.
Are you going out of your way on purpose, to make it hard for us to help you?
Was This Post Helpful? 0
  • +
  • -

#13 chaicomo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 07-August 11

Re: c# statements control

Posted 04 January 2012 - 08:42 AM

guys, as i said im starting that timer, so i need to find a way to put it in, at msdn i saw a Form come with a timer: "FormWithTimer : Form" mine didnt come with it "Communications: Form", so im searching a way, and trying a best of myself...
Was This Post Helpful? 0
  • +
  • -

#14 chaicomo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 07-August 11

Re: c# statements control

Posted 04 January 2012 - 08:59 AM

Guys thank you, i find a way:

 public void ComPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {

            msg = ComPort.ReadExisting().ToString();

            Timer timer1 = new Timer();

            timer1.Interval = 1000 * (30);
            timer1.Tick += new EventHandler(timer1_Tick);

        }

 private void timer1_Tick(object sender, EventArgs e)
        {
            StreamWriter escreve = new StreamWriter(@"c:\ dados.text");
            escreve.WriteLine(msg + "    " + DateTime.Now);
            escreve.Close();
            escreve.Dispose();
        }





happy coding for you
Was This Post Helpful? 0
  • +
  • -

#15 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5535
  • View blog
  • Posts: 11,857
  • Joined: 02-June 10

Re: c# statements control

Posted 04 January 2012 - 09:02 AM

Your form didn't come with a button on it either, did it?
Just drag a timer from the toolbox onto your form.

Let me say this though... It sounds like this is all VERY new to you. This type of thing is covered very early on in any self-teaching book. Which makes me think you really aren't ready to be designing an application yet.

Newbie/Rookie said:

I have a little programming experience but I need to write ...


You need to start there. I can't say "I have little experience in speaking Russian, but I have been assigned to write a mystery novel in Russian. Can you help me?"

We can help you by saying "First learn basic programming and the language of C#. Then take on assignments." Could someone here write this program for you? Sure. Could someone here map out all the processes you need to follow and do the Software Design part of this in the slim hope you could code it from there? Sure. But we don't volunteer to do the job that you're either getting paid for, or getting a grade for. You may want to read this.

For now, just work on the lessons. Do a self-teaching book from cover to cover. Then consider writing a program.

Don't try to create a useful working program to fit a need of yours (or a for-pay contract) as your introduction to coding project. When you are learning to code you don't know enough to code a program, let alone know how to engineer the architecture of a program. It would be like saying "I don't know how to read sheet music, or play an instrument. I think I'll write a 3 act opera as my first learning experience."

I don't say this to be mean. We've seen lots of new coders take this approach and we know it doesn't work. Trying to design your own programs before you understand the basics of the code language you've chosen just leads to problems, frustrations, and 'swiss-cheese' education (lots of holes).


Resources, references and suggestions for new programmers. - Updated Dec 2011
Spoiler


View Postchaicomo, on 04 January 2012 - 09:59 AM, said:

Guys thank you, i find a way:

 public void ComPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {

            msg = ComPort.ReadExisting().ToString();

            Timer timer1 = new Timer();

            timer1.Interval = 1000 * (30);
            timer1.Tick += new EventHandler(timer1_Tick);

        }

 private void timer1_Tick(object sender, EventArgs e)
        {
            StreamWriter escreve = new StreamWriter(@"c:\ dados.text");
            escreve.WriteLine(msg + "    " + DateTime.Now);
            escreve.Close();
            escreve.Dispose();
        }





happy coding for you


NO. NOoooooooooooooo!

Every time you get new data you are going to make an ADDITIONAL timer and subscribe to it.
If you get 5 pieces of data you will have 5 timers all running at slightly offset times.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2