c# - Code to be run on program close and pc shutdown -
I have a formless application that runs in the form of a system tray application. When the computer stops or the application stops, then it is struggling to run the code.
This is what I have done so far
class sysTrayIcon {public sysTrayIcon () {AppDomain.CurrentDomain.ProcessExit + = New EventHandler (ExitCode); SystemEvents.SessionEnding + = New Session End Event Handler (ExitCode); } Close the public zero exitCode (object sender, EventArgs e) {contactCman app = new contactCman (); bool didWeSuceed = appClosing.stampUserOut (); If (didWeSuceed == false) {MessageBox.Show ("Stump Out Function Run Returns Failed !!"); } Other {Mailboxes} Show ("Function Run Stump Out Function Success"); }}} Class contact key {public dictionary & lt; String, string & gt; GetUrlResponse (string toDo) {var url = main SiteUrl + "/" + apiFileName + "?" + "User Id =" + User Id + "& amp; pw =" + pw + "and Tudo =" + Two Too "; HttpWebRequest request = WebRequest.Create (url) as HttpWebRequest; HttpWebResponse response = (HttpWebResponse) request.GetResponse (); WebHeaderCollection Header = Response. Header; Var Encoding = ASCIIINcodeing. ASCIII; Wire reaction text; (Var reader = new System.IO.StreamReader (response.GetResponseStream (), encoding) using {responseText = reader.ReadToEnd (); } // key1 = value1; Key2 = value2; Key3 = value3; Var responseData = responseText.Split (new [] {';'}, string split option. RemoveEmptyEntries). Select (Part = & gt; Part Split ('='). Execution (Partition = & gt; Partition [0], Partition = & gt; Division [1]); Return Feedback Data; } Public Dictionary & lt; String, string & gt; Checkstst () {string to to do = "getLastStamp"; Dictionary & lt; String, string & gt; urlResponse = getUrlResponse (toDo); Return urlResponse; } public bool stampUserOut () {dictionary & lt; String, string & gt; UsersLastStamp = checkLastStamp (); String value off; If (usersLastStamp.TryGetValue ("Check InOut", OutOn Value Off) & amp; (valueOfKey == "in")) {string toDo = "stampUserOut"; Dictionary & lt; String, string & gt; urlResponse = getUrlResponse (toDo); If (urlResponse.TryGetValue ("message", outside value) & amp; amp; (valueOfKey == "success!")) {True; } Other {return false; }} And {return false; }}} My problem is that the code above the version (only the program has been tested when the program is closed), as it is intended to run through the debugger inside the visual studio. If I run it out, the message box never happens.
I think it would be better to hear the incident. Until the handler is executed, the application will actually stop the exit from:
Fixed zero main () {Application.ApplicationExit + = Application_ApplicationExit; ...} Fixed Zero Application_ApplicationExit (Object Sender, EventArgs e) {MessageBox.Show ("Exit!"); }
Comments
Post a Comment