Home
Forums
New posts
Search forums
Online Courses
2021 Rankings
2021 MFE Programs Rankings Methodology
Reviews
Latest reviews
Search resources
Tracker
What's new
New posts
New media
New media comments
New resources
New profile posts
Latest activity
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
C++ Programming for Financial Engineering
Highly recommended by thousands of MFE students. Covers essential C++ topics with applications to financial engineering.
Learn more
Join!
Python for Finance with Intro to Data Science
Gain practical understanding of Python to read, understand, and write professional Python code for your first day on the job.
Learn more
Join!
An Intuition-Based Options Primer for FE
Ideal for entry level positions interviews and graduate studies, specializing in options trading arbitrage and options valuation models.
Learn more
Join!
Home
Forums
Quant discussion
Computing
SHOW ME THE CODE !
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="Andy Nguyen" data-source="post: 12188" data-attributes="member: 1"><p>I wrote my first window service in C# today. It's so cool.</p><p>There are plenty of guide online but most are confusing, outdated so it feels great when it's finally working.</p><p></p><p>The window service will run in the background. I need to write this so it will copy all the tools code I wrote in my C:\ and put them into a network drive. It will do it once a day when I leave work. You never know what will happen with your code, machine, etc so having latest code will come back and save my life sometimes.</p><p></p><p>Some of the problem is .NET don't have a copy directory method. It only has copy file so I have to add a copy directory recursively class. I also have to add a timer and set it to start the service every 24 hours. It will copy and overwrite all my code to a shared drive. It will also write to a log file with timestamp when it start/stop.</p><p></p><p>Apparently, I can write some code so it can call Winrar program, zip it to a zip file and dump that file instead of copy the whole dir. I'll do it tomorrow.</p><p></p><p>Having a window service is so great. I can add many tasks to it so it can update the database, save the code, email me if there is any error. Once I set this up, I don't have to remember anymore. This will come in handy in the future when we need to run something overnight.</p><p></p><p>I love C# !</p><p></p><p>[code]using System;</p><p>using System.Collections.Generic;</p><p>using System.ComponentModel;</p><p>using System.Data;</p><p>using System.IO;</p><p>using System.IO.Compression;</p><p>using System.Diagnostics;</p><p>using System.ServiceProcess;</p><p>using System.Text;</p><p>using System.Threading;</p><p></p><p>//window service</p><p>namespace wsAndy</p><p>{</p><p>public partial class wsSaveProject : ServiceBase</p><p>{</p><p>private Timer stateTimer;</p><p>private TimerCallback timerDelegate;</p><p></p><p>public wsSaveProject()</p><p>{</p><p>InitializeComponent();</p><p>}</p><p></p><p>protected override void OnStart(string[] args)</p><p>{</p><p>//TODO: Add code here to start your service.</p><p></p><p>timerDelegate = new TimerCallback(OnTick);</p><p>stateTimer = new Timer(timerDelegate, null, 0, 86400000); // 1000ms = 1 secs</p><p>}</p><p></p><p>protected override void OnStop()</p><p>{</p><p>// TODO: Add code here to perform any tear-down necessary to stop your service.</p><p>stateTimer.Dispose();</p><p>FileStream fs = new FileStream(@"\\Backup\wsBackupCodeLog.txt",</p><p>FileMode.OpenOrCreate, FileAccess.Write);</p><p>StreamWriter m_streamWriter = new StreamWriter(fs);</p><p>m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);</p><p>m_streamWriter.WriteLine("Windows service wsSP stopped at \n" + DateTime.Now.ToString());</p><p>m_streamWriter.Flush();</p><p>m_streamWriter.Close();</p><p>}</p><p></p><p>private void OnTick(object stateObject)</p><p>{</p><p></p><p>// Don't let service crash</p><p>try</p><p>{</p><p>// Do your stuff here</p><p></p><p>DirectoryInfo src2005 = new DirectoryInfo(@"c:\Documents and Settings\My Documents\Visual Studio 2005");</p><p>DirectoryInfo dsn2005 = new DirectoryInfo(@"\\backups\VS2005");</p><p></p><p>FileStream fs = new FileStream(@"\\backups\wsBackupCodeLog.txt",</p><p>FileMode.OpenOrCreate, FileAccess.Write);</p><p>StreamWriter m_streamWriter = new StreamWriter(fs);</p><p>m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);</p><p>m_streamWriter.WriteLine("VS2005/8 last copied at \n" + DateTime.Now.ToString());</p><p>m_streamWriter.Flush();</p><p>m_streamWriter.Close();</p><p>copyDirectory(src2005, dsn2005, true);</p><p>}</p><p>catch (Exception ex)</p><p>{</p><p>try</p><p>{</p><p>this.EventLog.WriteEntry(ex.Message, EventLogEntryType.Error);</p><p>}</p><p>catch (Exception exx)</p><p>{</p><p>//Bad luck, handle it differently or rethrow original exception</p><p>}</p><p>}</p><p>}</p><p></p><p>public static void copyDirectory(DirectoryInfo srcPath, DirectoryInfo destPath, bool recursive)</p><p>{</p><p>if (!destPath.Exists)</p><p>destPath.Create();</p><p></p><p>// copy files</p><p>foreach (FileInfo fi in srcPath.GetFiles())</p><p>{</p><p>fi.CopyTo(Path.Combine(destPath.FullName, fi.Name), true);</p><p>}</p><p></p><p>// copy directories</p><p>if (recursive)</p><p>{</p><p>foreach (DirectoryInfo di in srcPath.GetDirectories())</p><p>{</p><p>copyDirectory(di, new DirectoryInfo(Path.Combine(destPath.FullName, di.Name)), recursive);</p><p>}</p><p>}</p><p>}</p><p>}</p><p>}[/code]</p></blockquote><p></p>
[QUOTE="Andy Nguyen, post: 12188, member: 1"] I wrote my first window service in C# today. It's so cool. There are plenty of guide online but most are confusing, outdated so it feels great when it's finally working. The window service will run in the background. I need to write this so it will copy all the tools code I wrote in my C:\ and put them into a network drive. It will do it once a day when I leave work. You never know what will happen with your code, machine, etc so having latest code will come back and save my life sometimes. Some of the problem is .NET don't have a copy directory method. It only has copy file so I have to add a copy directory recursively class. I also have to add a timer and set it to start the service every 24 hours. It will copy and overwrite all my code to a shared drive. It will also write to a log file with timestamp when it start/stop. Apparently, I can write some code so it can call Winrar program, zip it to a zip file and dump that file instead of copy the whole dir. I'll do it tomorrow. Having a window service is so great. I can add many tasks to it so it can update the database, save the code, email me if there is any error. Once I set this up, I don't have to remember anymore. This will come in handy in the future when we need to run something overnight. I love C# ! [code]using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.IO; using System.IO.Compression; using System.Diagnostics; using System.ServiceProcess; using System.Text; using System.Threading; //window service namespace wsAndy { public partial class wsSaveProject : ServiceBase { private Timer stateTimer; private TimerCallback timerDelegate; public wsSaveProject() { InitializeComponent(); } protected override void OnStart(string[] args) { //TODO: Add code here to start your service. timerDelegate = new TimerCallback(OnTick); stateTimer = new Timer(timerDelegate, null, 0, 86400000); // 1000ms = 1 secs } protected override void OnStop() { // TODO: Add code here to perform any tear-down necessary to stop your service. stateTimer.Dispose(); FileStream fs = new FileStream(@"\\Backup\wsBackupCodeLog.txt", FileMode.OpenOrCreate, FileAccess.Write); StreamWriter m_streamWriter = new StreamWriter(fs); m_streamWriter.BaseStream.Seek(0, SeekOrigin.End); m_streamWriter.WriteLine("Windows service wsSP stopped at \n" + DateTime.Now.ToString()); m_streamWriter.Flush(); m_streamWriter.Close(); } private void OnTick(object stateObject) { // Don't let service crash try { // Do your stuff here DirectoryInfo src2005 = new DirectoryInfo(@"c:\Documents and Settings\My Documents\Visual Studio 2005"); DirectoryInfo dsn2005 = new DirectoryInfo(@"\\backups\VS2005"); FileStream fs = new FileStream(@"\\backups\wsBackupCodeLog.txt", FileMode.OpenOrCreate, FileAccess.Write); StreamWriter m_streamWriter = new StreamWriter(fs); m_streamWriter.BaseStream.Seek(0, SeekOrigin.End); m_streamWriter.WriteLine("VS2005/8 last copied at \n" + DateTime.Now.ToString()); m_streamWriter.Flush(); m_streamWriter.Close(); copyDirectory(src2005, dsn2005, true); } catch (Exception ex) { try { this.EventLog.WriteEntry(ex.Message, EventLogEntryType.Error); } catch (Exception exx) { //Bad luck, handle it differently or rethrow original exception } } } public static void copyDirectory(DirectoryInfo srcPath, DirectoryInfo destPath, bool recursive) { if (!destPath.Exists) destPath.Create(); // copy files foreach (FileInfo fi in srcPath.GetFiles()) { fi.CopyTo(Path.Combine(destPath.FullName, fi.Name), true); } // copy directories if (recursive) { foreach (DirectoryInfo di in srcPath.GetDirectories()) { copyDirectory(di, new DirectoryInfo(Path.Combine(destPath.FullName, di.Name)), recursive); } } } } }[/code] [/QUOTE]
Verification
Post reply
Home
Forums
Quant discussion
Computing
SHOW ME THE CODE !
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.
Accept
Learn more…
Top