Click here to Skip to main content
15,887,676 members
Home / Discussions / C#
   

C#

 
GeneralThread Abort Pin
Blue_Skye21-Jul-05 1:22
Blue_Skye21-Jul-05 1:22 
GeneralRe: Thread Abort Pin
LongRange.Shooter21-Jul-05 4:11
LongRange.Shooter21-Jul-05 4:11 
GeneralRe: Thread Abort Pin
Blue_Skye21-Jul-05 6:29
Blue_Skye21-Jul-05 6:29 
GeneralRe: Thread Abort Pin
S. Senthil Kumar21-Jul-05 6:37
S. Senthil Kumar21-Jul-05 6:37 
GeneralAbout threading Pin
Rassul Yunussov21-Jul-05 1:20
Rassul Yunussov21-Jul-05 1:20 
GeneralRe: About threading Pin
LongRange.Shooter21-Jul-05 4:17
LongRange.Shooter21-Jul-05 4:17 
GeneralRe: About threading Pin
Rassul Yunussov21-Jul-05 19:27
Rassul Yunussov21-Jul-05 19:27 
GeneralRe: About threading Pin
LongRange.Shooter22-Jul-05 5:02
LongRange.Shooter22-Jul-05 5:02 
Many of the Patterns websites discusses MVC...but an example is a Form with two temperature boxes -- one 'view' displays Fahrenheit and the other 'view' displays Celcius.

The controller creates each view:
TempView fView = new TempView(TempState.Fahrenheit);
TempView cView = new TempView(TempState.Celcius);
myForm.FTempPanel.Controls.Add(fView));
myForm.CTempPanel.Controls.Add(cView);


the controller then gets the model:
Model mymodel = new Model();


The controller then passes the IObservable data to each view
fView.SetObserver(mymodel.GetObserver);
cView.SetObserver(mymodel.GetObserver);


Finally controller wants a starting temperature to display. I'd normally get it from a saved control file but for this example it is hard coded {shudder}:
mymodel.SetTemp(75);


Now...when the views got the observer interface, they used that to register:
public void SetObserver(IObservable myObserver)
{
       myObserver.DataChangeEvent += new DataChangeHandler(newTemp);
}


when the model gets called with SetTemp() the model notifies the observers that there is new data:
public void SetTemp(int temperature)
{
mytemp = temperature;
ChangeEventArgs myargs = new ChangeEventArgs("temp");
if (DataChangeEvent != null)
DataChangeEvent(this, myargs);
}

Now -- the model DataChangeEvent is happening inside the message pump thread. As a result your form can deal with the event and change the form without having the message pump get confused. So in your TempView object you have an event handler that just says:
public void newTemp(object sender, ChangeEventArgs args)
 {
      if (args.ChangeType == "temp")
          this.temperature.Text = myObserver.GetTemperature(myState).ToString();
 }


So the view recognized that the change was one that affected itself, it made a call into the model (readonly!!!!) to get the temperature and the model used the state to return the correct value of Fahrenheit or Celcius.

Now if you add a slider to the form (which is your TOC) and that is a view that changes temperature, it's only job is to talk to the controller. If the slider is moved, the end result is passed to the controller as a user gesture:

IN THE CONTROLLER
...  TempSlider slider = new TempSlider();
     slider.SetUserChangesTempEvent(new EventHandler(ChangeTemp);
     myForm.SliderPanel.Controls.Add(slider);


and in the slider...

public class TempSlider
 {
    ...
    public void Slider_ValueChange()
    {
        .. after validating the input to verify it is correct
        TempArgs args = new TempArgs(slider.Value.ToString);
        if (UserHasChangeTempEvent != null)
            UserHasChangeTempEvent(this, args);
    }

finally the controller gets the changed temp event, calls the model with the new value, and the model notifies the observers who change to the new value.

It is more complex but you can quickly see that your views are completely free from the form, placement on the form, and from the data. Events happen within the Apartment Thread and the forms get updated without impact to the forms, the message pump, etc. To have an independant thread run, the changed data is just one more UserGesture inside of your control. I tend to encapsulate the Event registration in a method call in case I want to do anything fancy or have multiple event registrations respond to the same eventhandler.

Hope this helps. Google Model View Controller Pattern to find out more.
GeneralRe: About threading Pin
Rassul Yunussov21-Jul-05 19:46
Rassul Yunussov21-Jul-05 19:46 
GeneralRe: About threading Pin
Rassul Yunussov21-Jul-05 21:48
Rassul Yunussov21-Jul-05 21:48 
GeneralCreating a Gif file Pin
djing198520-Jul-05 23:34
djing198520-Jul-05 23:34 
GeneralRe: Creating a Gif file Pin
Libor Tinka21-Jul-05 0:05
Libor Tinka21-Jul-05 0:05 
GeneralRe: Creating a Gif file Pin
djing198521-Jul-05 0:33
djing198521-Jul-05 0:33 
GeneralRe: Creating a Gif file Pin
Yoyosh21-Jul-05 1:59
Yoyosh21-Jul-05 1:59 
GeneralRe: Creating a Gif file Pin
poucin10021-Jul-05 2:03
poucin10021-Jul-05 2:03 
GeneralRe: Creating a Gif file Pin
djing198521-Jul-05 4:25
djing198521-Jul-05 4:25 
GeneralIL code security Pin
Libor Tinka20-Jul-05 23:03
Libor Tinka20-Jul-05 23:03 
GeneralRe: IL code security Pin
Corinna John20-Jul-05 23:38
Corinna John20-Jul-05 23:38 
GeneralRe: IL code security Pin
Libor Tinka21-Jul-05 0:00
Libor Tinka21-Jul-05 0:00 
GeneralMDI Forms Pin
rmedo20-Jul-05 22:37
rmedo20-Jul-05 22:37 
GeneralRe: MDI Forms Pin
Dave Kreskowiak21-Jul-05 2:24
mveDave Kreskowiak21-Jul-05 2:24 
GeneralRe: MDI Forms Pin
rmedo21-Jul-05 2:25
rmedo21-Jul-05 2:25 
GeneralRe: MDI Forms Pin
Dave Kreskowiak21-Jul-05 4:36
mveDave Kreskowiak21-Jul-05 4:36 
GeneralRe: MDI Forms Pin
rmedo23-Jul-05 21:49
rmedo23-Jul-05 21:49 
GeneralRe: MDI Forms Pin
Dave Kreskowiak24-Jul-05 6:44
mveDave Kreskowiak24-Jul-05 6:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.