Does the topic disappear?

When I click the button, I start the stream:

Thread myThread = new Thread(TestThread); myThread.IsBackground = true; myThread.Start(); 

Inner thread method:

 // Start stopwatch var watch = Stopwatch.StartNew(); // Our method HelperMethods.Mymethod( "19-04-2015", "20-04-2015", Properties.Settings.Default.username, Properties.Settings.Default.password ); // stop it watch.Stop(); // Get number of ellapsed milliseconds var elapsedMs = watch.ElapsedMilliseconds; TimeSpan t = TimeSpan.FromMilliseconds(elapsedMs); string answer = string.Format( "{0:D2}h:{1:D2}m:{2:D2}s:{3:D3}ms", t.Hours, t.Minutes, t.Seconds, t.Milliseconds ); MessageBox.Show("Done " + answer); 

I started this thread yesterday and left my computer unattended (Windows + L). The next day I waited to see above the Message Box with the message "Done." But this was not, although Mymethod had to be stopped at that time. All the code inside Mymethod is inside try/catch , and I am logging an exception that occurs inside this way. It is strange that the exception was not registered, that my program was unexpectedly terminated or something like that. The last message logged (NB: I only log errors) came from the previous day at around 9pm. After that, no signs ... The strange situation I'm in ... what could happen here? Any ideas? Unfortunately, I stopped using it when I returned without being able to check whether Mymethod was still running or not, but it should not have worked, since it had more than enough time to complete during the time when it remained in working condition.

+5
source share
3 answers

MessageBox.Show() should be called on the user interface thread, and not from the background thread. May I suggest using Task with the continuation:

 var task = Task.Factory.StartNew(TestThread); task.ContinueWith(parent => { if(parent.Exception != null) MessageBox.Show("Done."); else MessageBox.Show("Exception occurred."); }, TaskScheduler.FromCurrentSynchronizationContext()); 

The above method will run your method on another thread, and when the method completes, it will start lambda from ContinueWith() to execute on the user interface thread. Thus, calling MessageBox.Show() will not break your application.

Of course, be sure to remove the MessageBox.Show() call from the TestThread method.

0
source

As a rule, Windows is set to hibernate or even hibernate when there is no long time from the user (by default - about 15-30 minutes).

Given that he stayed all night, he could fall asleep and stop all the processes. So, basically the thread worked for a while, but later it was suspended due to the computer entering sleep mode. And this did not work in the remaining time. When you returned, the flow resumed, but the application soon shut down. In other words, the machine did not work most of the time, and the thread did not have time to run to the end. And by disabling the application, you just stopped it completely.

The following is a replacement for your HelperMethods.Mymethod , it runs in a separate background thread, as in your case:

 while(i < 1000) { Console.WriteLine(DateTime.Now); i++; Thread.Sleep(1000); } 

When I started it and turned on the car to sleep - this is the result that I get.

 4/21/2015 6:00:45 PM 4/21/2015 6:00:46 PM 4/21/2015 6:00:47 PM 4/21/2015 6:00:48 PM *** gap here. machine is in sleep, thread not working *** 4/21/2015 6:01:02 PM 4/21/2015 6:01:03 PM 4/21/2015 6:01:04 PM 

Also, calling your Mymethod methods is a bad idea. The method name must be descriptive. It should be easy to understand what he is doing without looking into him.

0
source

MessageBox.Show ("Done" + response); It falls, and this leads to the disappearance of the stream. You must move the call to MessageBox.Show outside the stream. Call it when the thread is completed.

-1
source

All Articles