c# - Multithreading in .NET - implementing a counter in the background -


As a training exercise, I am creating a counter that runs in a background thread, one. 3.5 3.5 in WPF app. The idea is that the counter increases and outputs on the screen every few seconds.

Because it is a training practice, the foreground thread does not really do anything, but imagine that it does!

There is a start and stop button to start and stop the counter.

The problem is that I want to update the foreground thread in every second and update it in the text box of the form. When I try this, the whole app goes 'no answer'. Currently I have got it to update the count for a certain time after it started.

I will dump all the codes here so that you can get the full picture:

  public partial squares window 1: window {public representative zero endingInteractivity (int presentcount) ; Number of private int reports; Private thread work thread; Public Window 1 () {InitializeComponent (); } Events in # field private zeros-window-loaded (object sender, RoutedEventArgs e) {txtCurrentCount.Text = "0"; Report = 0; InitialiseThread (); } Private Zero btnStart_Click (Object Sender, RoutedEventArgs e) {workThread.Start (); // Thread some foreground thread stuff Sleep (200); // I want to keep it in a loop to report from time to time txtCurrentCount.Text = the number reported. To string (); } Private Zero btnStop_Click (Object Sender, RoutedEventArgs e) {workThread.Abort (); TxtCurrentCount.Text = The number reported. To string (); InitialiseThread (); } #third private zero initial thread () {WorkObject obj = new WorkObject (report order, number reported); WorkThread = new thread (obj.StartProcessing); } Private Zero Reportincrement (int presentcount) (Reported number = presentcount;}} Public category WorkObject {Private window 1. Finnish Inquisition _callback; Private Ent _currentCount; Public WorkObsject (window 1. Finnish collecting callback, ant count) { _callback = callback; _currentCount = count;} Public Zero Start Processing (Object ThreadState) {for (int i = 0; i & lt; 100000; i ++) {_currentCount ++; if (_callback! = Null) {_callback (_currentCount ); Thread.Sleep (100);}}}}  

May The question is how do I get it to report periodically, and why does it fail when I try to put reporting in the loop?

Edit Do: have mentioned that my training practice was to become familiar with the old school system of multithreading, so I BackgroundWorker !

A message in every form in the windows application lu This is what looks like this:

  (NotTimeToCloseForm) {message message = GetMessageFromWindows (); WndProc (MSG); }  

This applies to all Windows applications not just net. WPF provides a default implementation of WinProc which goes something like this:

  zero WndProc (message message) {switch (msg.Type) {case WM_LBUTTONDOWN: RaiseButtonClickEvent (New ButtonClickEventArgs ( Msg)); break; Case WM_PAINT: UpdateFormorDesplay (new paint event event (msg)); break; Case WM_xxx // ... break; Default: MakeWindowsDealWithMessage (msg); }}  

As you can see, a window can only process one message / event at a time as you are sleeping in your button click event, message loop Is being captured and the application closes (receiving messages and processing).

If you use this, it periodically permits the message in the WM_TIMER (tick) message message queue to process your application on other messages, while using some other thread Time is going to be able to do anything.

See MSDN ""

for more details.

Comments

Popular posts from this blog

asp.net - Javascript/DOM Why is does my form not support submit()? -

sockets - Delphi: TTcpServer, connection reset when reading -

javascript - Classic ASP "ExecuteGlobal" statement acting differently on two servers -