Keep main program waiting
-
Montag, 7. Mai 2012 19:17
Hello there, i have an application, which is multi thread...
second it go into the class, implement a timer class...public class Program { static void Main(string[] args) { //Program p = new Program(); //p.CreateSqlDatabase(); //p.RunLogParser(); EventReader eventReader=new EventReader("a","a","true","","","a"); eventReader.RunEventReader(5,1);
// WAIT IN HERE } }
private SubtleTime _subtleTime; public EventReader(String sqlServer, String instance, String integratedSecurityType, String username, String password, String dataBase) { . . . #warning Set SubtilTime sleep value _subtleTime = new SubtleTime(60000); _subtleTime.OnMinuteChanged += new EventHandler<EventArgs>(SubtleTime_OnMinuteChanged); . . . }
The timer work wholly in separate thread, (i write this timer for a win app at first),
and since it's in another, and the next phase of program start there, and if i wanna say it better, there's no more action in current thread, the application will terminate and both thread will close...
i need to do 2 thing:
1. tell main thread to wait at all
2. if stop(if it's service) or close(if is console) request come from OS, make sure timer event is completed (if already processes), if not , or if reach to end, disallow it to launch inner codes, and then send a signal to main Application, that it can close...
The event code is like this:
private int _lastTime; private int _minutes; private void SubtleTime_OnMinuteChanged(object sender, EventArgs e) { //Make Application to run in currect time _minutes++; SubtleTime subtleTime = (SubtleTime) sender; if (subtleTime.Minutes <= _lastTime + _interval) return; if (_minutes > (int.MaxValue - (_interval + _interval))) _minutes = 0; _lastTime = _minutes; if (Monitor.TryEnter(_parserLock,100)) { //Log Parser Functioning _lastReadTime = DateTime.Now.AddMinutes(-1*_timeGap); string fromDate = _previousReadTime.ToString("yyyy-MM-dd HH:mm:ss"); string toDate = _lastReadTime.ToString("yyyy-MM-dd HH:mm:ss"); RunLogParser(fromDate, toDate); ReadAuditTable(); _previousReadTime = _lastReadTime; Monitor.Exit(_parserLock); } }
Alle Antworten
-
Dienstag, 8. Mai 2012 16:03
Ok, I already changed base of timer component, so its provide with new methods, the Stop() which was incompletely available, TryStop(int timeout) and Terminate()
The Stop, will wait for lock object to become available, then lock, shut down the thread, and unlock object.
The try stop, try to lock and then perform stop operation, otherwise don't stop thread...
the Terminate , wont check for lock object, and abort the thread directly, but in the end in a try block check to unlock the lock object...
no i can have a direcet method with order timer to stop, and wont interrupt the events jobs, so all i need is to perform, onStart and onStop operation for my console, can any one help? -
Freitag, 18. Mai 2012 08:11
When migrating legacy code to the new async framework, TaskCompletionSource is your friend :)
http://msdn.microsoft.com/en-us/library/dd449174.aspx
internal class Program { private TaskCompletionSource<bool> _taskCompletionSource = new TaskCompletionSource<bool>(TaskCreationOptions.LongRunning); private async static void Main(string[] args) { EventReader reader = new EventReader("a", "a", "true", "", "", "a"); bool result = await reader.Task; } } public class EventReader { private TaskCompletionSource<bool> _taskCompletionSource; public Task<bool> Task { get { return _taskCompletionSource.Task; } } public EventReader(String sqlServer, String instance, String integratedSecurityType, String username, String password, String dataBase) { _taskCompletionSource = new TaskCompletionSource<bool>(TaskCreationOptions.LongRunning); /* * TODO Rest of code */ } public void Cancelled() { _taskCompletionSource.TrySetCanceled(); } public void Error(Exception e) { _taskCompletionSource.TrySetException(e); } public void Stop() { _taskCompletionSource.TrySetResult(true); } }A TaskCompletionSource has a Task property that mimics a normal task (and can be awaited). If you have access to the original source (e.g. in your EventReader class) you can set it's result whenever it's available (I've shown you the 3 ways in the simple Cancelled/Error/Stop methods above).
However, your question isn't hugely clear and I'm not sure you're heading down the right path at all. Let me know if this helped, or you need more clarification.
Craig Dean (MCPD)
Chief Executive
www.webappuk.comPlease consider marking this as the answer if you have been genuinely helped!
- Als Antwort markiert deadManN Dienstag, 22. Mai 2012 16:10
-
Dienstag, 22. Mai 2012 16:12
and how you handle windows event? i mean once windows tell the application to close...
my task never done, but user may want to finish it... so it click over CMD close button, and it generate an error, directly to user, not to application, and terminate application, without saving state...
BTW, TNX

