David_Wright

Okay, at the moment I have a public string 'MyData' that gets a value just fine. I would like to know if it is possible to re-get a value for that same string every (say) 5 mins

For example:
----------------------------------------------------------------------------------
public string MyData
{
get
{
return ... ;
}
set
{
... = value;
}
}
-----------------------------------------------------------------------------------
How could I rerun that every (say) 5 mins


T.I.A.


Re: Visual C# General Timed event

TilakGopi

Hi,

Can System.Windows.Forms.Timer be used for this requirement so that in the timer_tick event handler ,you can access the MyData property value

Thanks,

Ch.T.Gopi Kumar.






Re: Visual C# General Timed event

David_Wright

Hello,

Unfortunately, I cannot use Forms because of what I am trying to do. Perhaps an explanation would put things into context.

I need to get this value every (say) 5 min, so that this value (if changed) would be sent to MCML.

Please note that the sending of the value to MCML is not the problem (that works just fine). I just need an automated "background" re-run of that particular bit of code, to re-get the value.

T.I.A.





Re: Visual C# General Timed event

TilakGopi

Hi,

Have a while loop with a flag to break the loop , when ever you want.

In the loop access the MyData and use Thread.Sleep(30000) to sleep for 5 minutes.

This way you can access the MyData property for every 5 minutes and break the loop by using the flag , when you want to stop accessing.

This way may work out.

Thanks,

Ch.T.Gopi Kumar.






Re: Visual C# General Timed event

David_Wright

Would you give an example of how this would be done Where in the code would 'Thread.Sleep(30000)' go exactly





Re: Visual C# General Timed event

TilakGopi

while(ContinueLoop)

{

//Access your MyData

//Do some thing ...

Thread.Sleep(30000);

}

Thanks,

Ch.T.Gopi Kumar






Re: Visual C# General Timed event

Matthew Watson

Something to be aware of when doing this kind of thing is to be able to terminate the background thread gracefully, without having to kill the thread.

To do that, you use an EventWaitHandle that you signal in the code that wants the background loop to terminate.

Here's a simple example. The background thread will do some work every 5 seconds, but it will terminate immediately if you set the "stopper" event while it is in the "WaitOne()" delay.



using System;
using System.Threading;

namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
EventWaitHandle stopper = new EventWaitHandle(false, EventResetMode.AutoReset);
Thread backgroundThread = new Thread(new ParameterizedThreadStart(MyBackgroundThread));

ShowMessage("Starting background thread");
backgroundThread.Start(stopper);

Thread.Sleep(31000); // Pretend to do some other stuff.
stopper.Set(); // Signal to background thread that it should stop.

ShowMessage("Waiting for background thread to terminate.");
backgroundThread.Join(); // Wait for background thread to terminate.
ShowMessage("Background thread terminated.");
}

static void MyBackgroundThread(object stopperObj)
{
ShowMessage("Background thread: Started");

EventWaitHandle stopper = (EventWaitHandle)stopperObj;

for ( ; ; )
{
// Wait 5 seconds each time, or return immediately if the event
// is signalled.

if (stopper.WaitOne(5000, false)) // Replace 5000 with your
break; // desired periodic access time.

ShowMessage("Background thread: Next iteration.");

// Do your periodic work here.
}

ShowMessage("Background thread: Terminating.");
}

static void ShowMessage(string message)
{
Console.WriteLine(DateTime.Now.ToLocalTime() + ": " + message);
}
}
}







Re: Visual C# General Timed event

David_Wright

Okay, maybe you haven't helped too many noobs. But everywhere I try putting the while(ContinueLoop) is throwing up errors. Where do I put the loop exactly

namespace ...
{
public class ...
{
public string
{
...
}

public string
{
...
}
}
}






Re: Visual C# General Timed event

David_Wright

Thank you Matthew Big Smile Your program works wonderfully. Now, how could I apply that into my scenario of wanting to collect a value every (say) 5 mins

T.I.A.

I apologise in advance for my noobie questions. Smile





Re: Visual C# General Timed event

Matthew Watson

See where it says "5000" in the program That's the update period in milliseconds. Replace that with your desired period. For 5 mins it would be simply 5 * 60 * 1000




Re: Visual C# General Timed event

David_Wright

Yes, I get the timing aspect of it, but I don't understand how to re-get a public string value every 5 min. (or whatever interval of time desired)

I don't want anything writing to a console line, I just want it to re-get that string silently every 5 min. How might I achieve this

Sorry for my noobishness, I just don't see any good examples online to follow.

Pretty much, what it boils down to:

1 - Start program in MC, gets initial value of string from C# (<---- This part works fine)
2 - C# re-gets the value of the string every (say) 5 min. silently in the background.
3 - C# fires of the value of the string (if changed) to MC (<--- This part works fine)

It's #2 that is eluding me, I am not sure on how to do this. (#1 and #3 work just fine.) Any help would be appreciated.

T.I.A.





Re: Visual C# General Timed event

Matthew Watson

I guess I don't really understand your question...

I assumed that you could do your periodic work where it says "// Do your periodic work here."

Obviously, you will have seen that - so I don't really know what's up.

(To remove the console writes, you would just remove the calls to Console.WriteLine(), something which I assume you already know too.)




Re: Visual C# General Timed event

VictorC

Could I suggest another method of doing this [which you might not find useful depending on your program's specs but from what I understand you're asking for it might do the same job...]

Instead of having a timed event check for a changed value every 5 minutes why not simply have an event fired every time the value gets changed

public string MyData
{
get
{
return ... ;
}
set
{
... = value;
myDataChanged(MyData);
}
}



private void myDataChanged (string MyData)
{
// check if the changes in the string warrant a change in the MCML
// send the new value
}




Re: Visual C# General Timed event

David_Wright

I am new to C#, and I was wondering how could I refresh the value of 'myString' every 10 minutes (code that I have so far)

public class myClass
{
public string myString()
{
... code to get myString ....
}


}


I don't want any buttons, forms, etc. I just want to refresh 'myString' silently every 10 mins. Can this be done

T.I.A.





Re: Visual C# General Timed event

David_Wright

Thank you for your response Victor. You say "check if the changes in the string warrant a change in the MCML". The problem I have is that the value should be changed every time I refresh the string. The new updated string will be sent to MCML via base.FirePropertyChanged. I just need to get the string updated every 5 min. Another problem I am having is that all the suggestions that I have recieved seem to involve either buttons, forms, or the console line. I don't want that, I just need the string updated every 5 mins, that's it.

I am sure that the suggestions that I have received so far are good, however I am very new to C#, and don't know how to change the suggestions to what I actually need.