Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. About threading

About threading

Scheduled Pinned Locked Moved C#
data-structuresarchitecturehelptutorialquestion
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Rassul Yunussov
    wrote on last edited by
    #1

    Well i got problem, i have in form the panel, where i draw shapes, and a treeview where i insert the data about shapes:) I want to do it in two different threads, but how to do it? I explaine my architecture: I got a class TOC derived from treeview, and i got a mapcontrol derived from panel, and i have maptable - array of objects. So, when i add new object in the maptable i update data in two components - mapcontrol and TOC... All the objects linked to each other (by references). If it's possible without having static variables?

    L 1 Reply Last reply
    0
    • R Rassul Yunussov

      Well i got problem, i have in form the panel, where i draw shapes, and a treeview where i insert the data about shapes:) I want to do it in two different threads, but how to do it? I explaine my architecture: I got a class TOC derived from treeview, and i got a mapcontrol derived from panel, and i have maptable - array of objects. So, when i add new object in the maptable i update data in two components - mapcontrol and TOC... All the objects linked to each other (by references). If it's possible without having static variables?

      L Offline
      L Offline
      LongRange Shooter
      wrote on last edited by
      #2

      Any time you have a process using Forms, you will have issues when dealing with more than one thread. But what you describe sounds like you need to consider implementing the Model, View, Controller pattern to solve your problem. In the MVC pattern, your various elements on your form are Views of data. The Model handles read/update of data and implements an Observer pattern. The Views are subscribers to the Observer. The controller creates the Views, tells them their states, and receives events that represent User Gestures. So when a 'user gesture' occurs that changes data in your TOC (for example) the controller notifies the Model what that change is (maybe by something like model.StateChange(chapterChosen)). The model gets that particular chapter object, and raises the Observer event. The Views receive the event and read the data specific to each view from the Model. Thus you change one control and all of the controls automatically change. There are 10 kinds of people in the world.
      Those that read binary...
      ...and those who don't.

      R 3 Replies Last reply
      0
      • L LongRange Shooter

        Any time you have a process using Forms, you will have issues when dealing with more than one thread. But what you describe sounds like you need to consider implementing the Model, View, Controller pattern to solve your problem. In the MVC pattern, your various elements on your form are Views of data. The Model handles read/update of data and implements an Observer pattern. The Views are subscribers to the Observer. The controller creates the Views, tells them their states, and receives events that represent User Gestures. So when a 'user gesture' occurs that changes data in your TOC (for example) the controller notifies the Model what that change is (maybe by something like model.StateChange(chapterChosen)). The model gets that particular chapter object, and raises the Observer event. The Views receive the event and read the data specific to each view from the Model. Thus you change one control and all of the controls automatically change. There are 10 kinds of people in the world.
        Those that read binary...
        ...and those who don't.

        R Offline
        R Offline
        Rassul Yunussov
        wrote on last edited by
        #3

        Yes , this is the way i saw, but... Your next words: "The Views receive the event and read the data specific to each view from the Model" How can views read the data of another class (controller)? Because when i make the thread i can read only static members... Maybe little example could help me... Maybe i don't understand you...

        L 1 Reply Last reply
        0
        • L LongRange Shooter

          Any time you have a process using Forms, you will have issues when dealing with more than one thread. But what you describe sounds like you need to consider implementing the Model, View, Controller pattern to solve your problem. In the MVC pattern, your various elements on your form are Views of data. The Model handles read/update of data and implements an Observer pattern. The Views are subscribers to the Observer. The controller creates the Views, tells them their states, and receives events that represent User Gestures. So when a 'user gesture' occurs that changes data in your TOC (for example) the controller notifies the Model what that change is (maybe by something like model.StateChange(chapterChosen)). The model gets that particular chapter object, and raises the Observer event. The Views receive the event and read the data specific to each view from the Model. Thus you change one control and all of the controls automatically change. There are 10 kinds of people in the world.
          Those that read binary...
          ...and those who don't.

          R Offline
          R Offline
          Rassul Yunussov
          wrote on last edited by
          #4

          For example here is my code:

          using System;
          using System.Windows.Forms;
          using System.Drawing;
          namespace GeoObjects
          {
          /// ///
          ///

          public class GeoTOC : System.Windows.Forms.TreeView
          {
          	private System.ComponentModel.IContainer components;
          	private System.Windows.Forms.ImageList imageList\_LayersColor;
          	GeoMapControl m\_pGeoMapControl;
          	public GeoTOC()
          	{
          		// 
          		// TODO: Add constructor logic here
          		//
          		InitializeComponent();
          	}
          
          	private void InitializeComponent()
          	{
          		this.components = new System.ComponentModel.Container();
          		this.imageList\_LayersColor = new System.Windows.Forms.ImageList(this.components);
          		// 
          		// imageList\_LayersColor
          		// 
          		this.imageList\_LayersColor.ImageSize = new System.Drawing.Size(16, 16);
          		this.imageList\_LayersColor.TransparentColor = System.Drawing.Color.Transparent;
          		// 
          		// GeoTOC
          		// 
          		this.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
          		this.CheckBoxes = true;
          		this.ImageIndex = 0;
          		this.ImageList = this.imageList\_LayersColor;
          		this.SelectedImageIndex = 0;
          
          	}
          

          #region Accessors
          public GeoMapControl MapControl
          {
          set
          {
          m_pGeoMapControl = value;
          m_pGeoMapControl.TOC = this;
          }
          }
          #endregion
          public void UpdateTable()
          {
          this.BeginUpdate();
          Nodes.Clear();
          imageList_LayersColor.Images.Clear();
          System.Drawing.Bitmap img = new System.Drawing.Bitmap(16,16);
          Graphics g = Graphics.FromImage(img);
          g.Clear(Color.White);
          g.Dispose();
          imageList_LayersColor.Images.Add(img);
          foreach(GeoMapTable gmt in m_pGeoMapControl.MapTables)
          {
          TreeNode Map = Nodes.Add(gmt.Name);
          Map.Checked = gmt.Visible;
          Map.ForeColor = Color.Blue;
          foreach(GeoLayer gl in gmt.Layers)
          {
          img = new System.Drawing.Bitmap(16,16);
          g = Graphics.FromImage(img);
          g.Clear(Color.White);
          g.FillRectangle(new SolidBrush(gl.LayerColor),3,3,11,11);
          g.Dispose();
          imageList_LayersColor.Images.Add(img);
          TreeNode layer = Map.Nodes.Add(gl.Name);
          layer.Checked = gl.Visible;
          layer.ImageIndex = imageList_LayersColor.Images.Count-1;
          layer.SelectedImageIndex = imageList_LayersColor.Images.Count-1;
          }
          }
          this.EndUpdate();
          }
          protected override void OnAfterCheck(TreeViewEventArgs e)
          {
          TreeNode parent = e.Node.Parent;
          if(parent==null)
          ((GeoMapTable)m_pGeoMapControl.MapTables[e.Node.Index]).Visible = e.Node.Checked;
          else
          ((GeoLayer)((Ge

          1 Reply Last reply
          0
          • L LongRange Shooter

            Any time you have a process using Forms, you will have issues when dealing with more than one thread. But what you describe sounds like you need to consider implementing the Model, View, Controller pattern to solve your problem. In the MVC pattern, your various elements on your form are Views of data. The Model handles read/update of data and implements an Observer pattern. The Views are subscribers to the Observer. The controller creates the Views, tells them their states, and receives events that represent User Gestures. So when a 'user gesture' occurs that changes data in your TOC (for example) the controller notifies the Model what that change is (maybe by something like model.StateChange(chapterChosen)). The model gets that particular chapter object, and raises the Observer event. The Views receive the event and read the data specific to each view from the Model. Thus you change one control and all of the controls automatically change. There are 10 kinds of people in the world.
            Those that read binary...
            ...and those who don't.

            R Offline
            R Offline
            Rassul Yunussov
            wrote on last edited by
            #5

            I tried next, and got next error message :An unhandled exception of type 'System.InvalidOperationException' occurred in system.windows.forms.dll Additional information: The action being performed on this control is being called from the wrong thread. You must marshal to the correct thread using Control.Invoke or Control.BeginInvoke to perform this action.:

            using System;
            using System.Windows.Forms;
            using System.Drawing;
            using System.Threading;
            namespace GeoObjects
            {
            /// ///
            ///
            class FillIn
            {
            static GeoObjects.GeoTOC m_pGeoTOC;
            public FillIn(GeoTOC toc)
            {
            m_pGeoTOC = toc;
            }
            public static void Process()
            {
            m_pGeoTOC.UpdateTableThread();
            }
            }
            public class GeoTOC : System.Windows.Forms.TreeView
            {
            private System.ComponentModel.IContainer components;
            private System.Windows.Forms.ImageList imageList_LayersColor;
            GeoMapControl m_pGeoMapControl;
            public GeoTOC()
            {
            //
            // TODO: Add constructor logic here
            //
            InitializeComponent();
            }

            	private void InitializeComponent()
            	{
            		this.components = new System.ComponentModel.Container();
            		this.imageList\_LayersColor = new System.Windows.Forms.ImageList(this.components);
            		// 
            		// imageList\_LayersColor
            		// 
            		this.imageList\_LayersColor.ImageSize = new System.Drawing.Size(16, 16);
            		this.imageList\_LayersColor.TransparentColor = System.Drawing.Color.Transparent;
            		// 
            		// GeoTOC
            		// 
            		this.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            		this.CheckBoxes = true;
            		this.ImageIndex = 0;
            		this.ImageList = this.imageList\_LayersColor;
            		this.SelectedImageIndex = 0;
            
            	}
            

            #region Accessors
            public GeoMapControl MapControl
            {
            set
            {
            m_pGeoMapControl = value;
            m_pGeoMapControl.TOC = this;
            }
            }
            #endregion
            public void UpdateTableThread()
            {
            this.BeginUpdate();
            Nodes.Clear();
            imageList_LayersColor.Images.Clear();
            System.Drawing.Bitmap img = new System.Drawing.Bitmap(16,16);
            Graphics g = Graphics.FromImage(img);
            g.Clear(Color.White);
            g.Dispose();
            imageList_LayersColor.Images.Add(img);
            foreach(GeoMapTable gmt in m_pGeoMapControl.MapTables)
            {
            TreeNode Map = Nodes.Add(gmt.Name);
            Map.Checked = gmt.Visible;
            Map.ForeColor = Color.Blue;
            foreach(GeoLayer gl in gmt.Layers)
            {
            img = new System.Drawing.Bitmap(16,16);
            g = Graphics.FromImage(img);
            g.Clear(Color.White);
            g.FillRectangle(new SolidBrush(gl.LayerColor),3,3,11,11);

            1 Reply Last reply
            0
            • R Rassul Yunussov

              Yes , this is the way i saw, but... Your next words: "The Views receive the event and read the data specific to each view from the Model" How can views read the data of another class (controller)? Because when i make the thread i can read only static members... Maybe little example could help me... Maybe i don't understand you...

              L Offline
              L Offline
              LongRange Shooter
              wrote on last edited by
              #6

              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...

              publi

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups