David_Wright

All I want to do is re-get the value of a string every so often (say 5 mins).

I don't want anything writing to a Form or a Console line, I just want the string value to be updated every so often.

Is this really so hard to do Am I looking to do something impossible

I had thought that this might be relatively simple, but obviously this is really hard to do in C#



Re: Visual C# General Is this really that hard??

SerenityPCCS

You can always add an instance of a Timer and have it update every 5 minutes or whatever frequency... then as part of the Tick event, do what you want with the string. Just make sure you remember to Start the Timer Smile





Re: Visual C# General Is this really that hard??

David_Wright

Okay, but how do I do this And please don't just say add a timer. Code is always more helpful.

OMG, everything seems to be like pulling teeth. There is no good documentation about how to do any of this!! Googling for an answer is going nowhere, as all the answers seem to include either a Console or a Form.

All I want to do is get a string to update it's value every (say) 5 mins.







Re: Visual C# General Is this really that hard??

David_Wright

btw, before anyone says, you might want to study up on OOP or C#. If someone comes up to me and asks what the load limit is on a 20' length of 2x12 Walnut, I don't say go and study up on Methods and Materials of Construction. I just give them the answer, after all, that was what was asked in the first place. I don' think the person asking is really interested in learning how to calculate the values.

I need the value of a string updated every (say) 5 mins. No Console No Forms, but I guess this is impossible.





Re: Visual C# General Is this really that hard??

Vaughn Friesen

OK, some code to create a timer:

Code Snippet

System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();

timer.Interval = 1000 * 60 * 5;

timer.Tick += new EventHandler(timer_Tick);

timer.Start();

And then create an event handler:

Code Snippet

static void timer_Tick(object sender, EventArgs e)

{

// Do whatever

}





Re: Visual C# General Is this really that hard??

Vodstok

I think the reason you were getting the responses you were (regarding adding a timer) stems from most people would not write code for a string update and a timer if there was not and obvious interface (such as a console or a form). I imagine you are either writing a class or a service, but I figured I would clarify, at least in my opinion, why people would jump to those two conclusions.



Re: Visual C# General Is this really that hard??

David_Wright

First off, thank you for the code, much appreciated. However, it appears that the first code snippet includes Forms, I'm sure I mentioned "No Forms".

The second snippet:

"// Do whatever"

This doesn't seem correct, I can't put a string in there, as it complains when I try to run it. If I can put a string in there (getting a value every so often), could someone show me the proper way to do it. Every variation that I could think of is resulting in errors.

I had no idea that it should be this hard. Sad






Re: Visual C# General Is this really that hard??

David_Wright

Fair assumption, except I'm sure that in the OP, I mentioned "No Forms, No Console". I don't want anything written by C#. I just want a value of a string to be updated every (say) 5 mins. The value of the string will be used by MCML (Not a form or console). However, in these forums, I am reluctant to state the MCML part, because people start directing me towards places like the Sandbox thinking it is an issue with MCML. I will again clarify, the issue is with the C# side, not the MCML (that works as it should).


I just want to update the value of a string every (say) 5 mins. (No Forms - No Console), if C# can't do this, I would prefer someone tell me, so that I don't waste my time.





Re: Visual C# General Is this really that hard??

Vaughn Friesen

The timer class is in the System.Windows.Forms namespace. To use the timer class you need to reference that namespace, whether you use forms or not. If you prefer, you can use System.Threading.Timer, and there's a code sample here.

As for the do whatever snippet, you should take out the whole line (including the "//") and insert your own code there.





Re: Visual C# General Is this really that hard??

Vaughn Friesen

Here's some code for System.Threading.Timer:

Code Snippet
Timer
timer = new Timer(new TimerCallback(TimerProc), null, 0, 1000 * 60 * 5);

And here's the timer callback. Insert your update code between the braces:

Code Snippet

void TimerProc(object State)

{

}

Also you need to add

using System.Threading;

at the top of your code.





Re: Visual C# General Is this really that hard??

David_Wright

Okay, I put using System.Threading; at the top, but I can't seem to put anything between the two brackets under void.

If I put anything there, I start getting errors that a closing bracket is required right after the opening bracket.

Should I not be putting the code to get the strings value between those brackets





Re: Visual C# General Is this really that hard??

Vaughn Friesen

Sorry, I mean the braces. (Those { } things, whatever they are.)



Re: Visual C# General Is this really that hard??

David_Wright

Sorry, me too, I shouldn't have said brackets.

I think I have a viable solution, I am going to try it out and see how it goes.

Thank you for all of your help, I am hoping my idea works Smile