I have a method that has a lot of loops:private void update(double depth)        {            Console.WriteLine(update with level  + depth);            this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(delegate()                {            List<Grid> grids = new List<Grid>();            Dependencies.Children.Clear();            Grid g = new Grid();            //Canvas.SetZIndex(g, 100);            g.Width = 50;            g.Height = 50;            g.Tag = focus;            Ellipse e = new Ellipse();            e.Width = 50;            e.Height = 50;            e.Fill = Brushes.Red;            if (depth == 1)            {                Canvas.SetTop(g, 163);            }            else if (depth == 2)            {                Canvas.SetTop(g, 108);            }            else if (depth == 3)            {                Canvas.SetTop(g, 81);            }            else if (depth == 4)            {                Canvas.SetTop(g, 65);            }            else if (depth == 5)            {                Canvas.SetTop(g, 54);            }            else if (depth == 6)            {                Canvas.SetTop(g, 46);            }            Canvas.SetLeft(g, 500);            g.Children.Add(e);            Viewbox box = new Viewbox();            box.Width = e.Width;            box.Height = e.Height;            TextBox txt = new TextBox();            txt.Text = focus.getName();            box.Child = txt;            txt.Background = Brushes.Transparent;            txt.BorderBrush = Brushes.Transparent;            g.Children.Add(box);            grids.Add(g);            List<SourceFile> list = new List<SourceFile>();            list = focus.getInvocations();            int counter = 1;            foreach (SourceFile sf in list)            {                Grid g1 = new Grid();                //Canvas.SetZIndex(g, 101);                g1.Width = 50;                g1.Height = 50;                g1.Tag = sf;                Ellipse e1 = new Ellipse();                //Dependencies.Children.Add(e1);                sf.setGrid(g1);                e1.Width = 50;                e1.Height = 50;                e1.Fill = Brushes.Red;                g1.Children.Add(e1);                if (depth == 1)                {                    Canvas.SetTop(g1, 488);                }                else if (depth == 2)                {                    Canvas.SetTop(g1, 324);                }                else if (depth == 3)                {                    Canvas.SetTop(g1, 244);                }                else if (depth == 4)                {                    Canvas.SetTop(g1, 195);                }                else if (depth == 5)                {                    Canvas.SetTop(g1, 163);                }                else if (depth == 6)                {                    Canvas.SetTop(g1, 139);                }                Canvas.SetLeft(g1, counter * (1000 / (list.Count + 1) ));                Viewbox box1 = new Viewbox();                box1.Width = g1.Width;                box1.Height = g1.Height;                TextBox txt1 = new TextBox();                txt1.Text = sf.getName();                txt1.Background = Brushes.Transparent;                txt1.BorderBrush = Brushes.Transparent;                box1.Child = txt1;                g1.Children.Add(box1);                Line l = new Line();                //Canvas.SetZIndex(l, 1);                l.Stroke = Brushes.Green;                l.StrokeThickness = 10;                Binding x1 = new Binding(); x1.Path = new PropertyPath(Canvas.LeftProperty);                x1.Converter = new MyConverter();                x1.ConverterParameter = g;                Binding y1 = new Binding(); y1.Path = new PropertyPath(Canvas.TopProperty);                y1.Converter = new MyConverter();                y1.ConverterParameter = g;                Binding x2 = new Binding(); x2.Path = new PropertyPath(Canvas.LeftProperty);                x2.Converter = new MyConverter();                x2.ConverterParameter = g1;                Binding y2 = new Binding(); y2.Path = new PropertyPath(Canvas.TopProperty);                y2.Converter = new MyConverter();                y2.ConverterParameter = g1;                x1.Source = y1.Source = g;                x2.Source = y2.Source = g1;                l.SetBinding(Line.X1Property, x1);                l.SetBinding(Line.Y1Property, y1);                l.SetBinding(Line.X2Property, x2);                l.SetBinding(Line.Y2Property, y2);                Dependencies.Children.Add(l);                l.Tag = new Call(focus, sf);                Contacts.AddPreviewContactDownHandler(l, OnLineDown);                counter++;                grids.Add(g1);                SizeChangedEventHandler act = (Object s, SizeChangedEventArgs args) =>                {                    BindingOperations.GetBindingExpressionBase(l, Line.X1Property).UpdateTarget();                    BindingOperations.GetBindingExpressionBase(l, Line.Y1Property).UpdateTarget();                    BindingOperations.GetBindingExpressionBase(l, Line.X2Property).UpdateTarget();                    BindingOperations.GetBindingExpressionBase(l, Line.Y2Property).UpdateTarget();                };                g.SizeChanged += act;                g1.SizeChanged += act;            }            int counter2 = 1;            if (depth >= 2)            {                int invocCount = 0;                foreach (SourceFile s in list)                {                    foreach (SourceFile source in s.getInvocations())                    {                        invocCount = invocCount + s.getInvocations().Count;                    }                }                Console.WriteLine(invocCount);                foreach (SourceFile s in list)                {                    foreach (SourceFile source in s.getInvocations())                    {                        Console.WriteLine(`Found invocation of  + s.getName() + :  + source.getName());                        Grid g1 = new Grid();                        g1.Width = 50;                        g1.Height = 50;                        Ellipse e1 = new Ellipse();                       // Canvas.SetZIndex(g1, 102);                        grids.Add(g1);                        e1.Width = 50;                        e1.Height = 50;                        e1.Fill = Brushes.Red;                        source.setGrid(g1);                        g1.Tag = source;                        g1.Children.Add(e1);                        if (depth == 2)                        {                            Canvas.SetTop(g1, 540);                        }                        else if (depth == 3)                        {                            Canvas.SetTop(g1, 406);                        }                        else if (depth == 4)                        {                            Canvas.SetTop(g1, 325);                        }                        else if (depth == 5)                        {                            Canvas.SetTop(g1, 271);                        }                        else if (depth == 6)                        {                            Canvas.SetTop(g1, 232);                        }                        Canvas.SetLeft(g1, counter2 * (1000 / (invocCount + 1)));                        Viewbox box1 = new Viewbox();                        box1.Width = g1.Width;                        box1.Height = g1.Height;                        TextBox txt1 = new TextBox();                        txt1.Text = source.getName();                        txt1.Background = Brushes.Transparent;                        txt1.BorderBrush = Brushes.Transparent;                        box1.Child = txt1;                        g1.Children.Add(box1);                        Line l = new Line();                        //Canvas.SetZIndex(l, 2);                        l.Stroke = Brushes.Green;                        l.StrokeThickness = 10;                        Binding x1 = new Binding(); x1.Path = new PropertyPath(Canvas.LeftProperty);                        x1.Converter = new MyConverter();                        x1.ConverterParameter = s.getGrid();                        Binding y1 = new Binding(); y1.Path = new PropertyPath(Canvas.TopProperty);                        y1.Converter = new MyConverter();                        y1.ConverterParameter = s.getGrid();                        Binding x2 = new Binding(); x2.Path = new PropertyPath(Canvas.LeftProperty);                        x2.Converter = new MyConverter();                        x2.ConverterParameter = g1;                        Binding y2 = new Binding(); y2.Path = new PropertyPath(Canvas.TopProperty);                        y2.Converter = new MyConverter();                        y2.ConverterParameter = g1;                        x1.Source = y1.Source = findGrid(grids, s, source);                        x2.Source = y2.Source = g1;                        l.SetBinding(Line.X1Property, x1);                        l.SetBinding(Line.Y1Property, y1);                        l.SetBinding(Line.X2Property, x2);                        l.SetBinding(Line.Y2Property, y2);                        Dependencies.Children.Add(l);                        l.Tag = new Call(s, source);                        Contacts.AddPreviewContactDownHandler(l, OnLineDown);                        counter2++;                        SizeChangedEventHandler act = (Object o, SizeChangedEventArgs args) =>                        {                            BindingOperations.GetBindingExpressionBase(l, Line.X1Property).UpdateTarget();                            BindingOperations.GetBindingExpressionBase(l, Line.Y1Property).UpdateTarget();                            BindingOperations.GetBindingExpressionBase(l, Line.X2Property).UpdateTarget();                            BindingOperations.GetBindingExpressionBase(l, Line.Y2Property).UpdateTarget();                        };                        source.getGrid().SizeChanged += act;                        g1.SizeChanged += act;                    }                }            }            int counter3 = 1;            if (depth >= 3)            {                int invocCount = 0;                foreach (SourceFile s in list)                {                    foreach (SourceFile source in s.getInvocations())                    {                        foreach (SourceFile s1 in source.getInvocations())                        {                            invocCount = invocCount + source.getInvocations().Count;                        }                    }                }                foreach (SourceFile s in list)                {                    foreach (SourceFile source in s.getInvocations())                    {                        foreach (SourceFile s1 in source.getInvocations())                        {                            Grid g1 = new Grid();                            grids.Add(g1);                            g1.Width = 50;                            g1.Height = 50;                            g1.Tag = s1;                            Ellipse e1 = new Ellipse();                            e1.Width = 50;                            e1.Height = 50;                            e1.Fill = Brushes.Red;                            s1.setGrid(g1);                            g1.Children.Add(e1);                            if (depth == 3)                            {                                Canvas.SetTop(g1, 569);                            }                            else if (depth == 4)                            {                                Canvas.SetTop(g1, 455);                            }                            else if (depth == 5)                            {                                Canvas.SetTop(g1, 379);                            }                            else if (depth == 6)                            {                                Canvas.SetTop(g1, 325);                            }                            Canvas.SetLeft(g1, counter3 * (1000 / (invocCount + 1)));                            Viewbox box1 = new Viewbox();                            box1.Width = g1.Width;                            box1.Height = g1.Height;                            TextBox txt1 = new TextBox();                            txt1.Background = Brushes.Transparent;                            txt1.BorderBrush = Brushes.Transparent;                            txt1.Text = s1.getName();                            box1.Child = txt1;                            g1.Children.Add(box1);                            Line l = new Line();                            //Canvas.SetZIndex(l, 2);                            l.Stroke = Brushes.Green;                            l.StrokeThickness = 10;                            Binding x1 = new Binding(); x1.Path = new PropertyPath(Canvas.LeftProperty);                            x1.Converter = new MyConverter();                            x1.ConverterParameter = source.getGrid();                            Binding y1 = new Binding(); y1.Path = new PropertyPath(Canvas.TopProperty);                            y1.Converter = new MyConverter();                            y1.ConverterParameter = source.getGrid();                            Binding x2 = new Binding(); x2.Path = new PropertyPath(Canvas.LeftProperty);                            x2.Converter = new MyConverter();                            x2.ConverterParameter = g1;                            Binding y2 = new Binding(); y2.Path = new PropertyPath(Canvas.TopProperty);                            y2.Converter = new MyConverter();                            y2.ConverterParameter = g1;                            x1.Source = y1.Source = findGrid(grids, source, s1);                            x2.Source = y2.Source = g1;                            l.SetBinding(Line.X1Property, x1);                            l.SetBinding(Line.Y1Property, y1);                            l.SetBinding(Line.X2Property, x2);                            l.SetBinding(Line.Y2Property, y2);                            Dependencies.Children.Add(l);                            l.Tag = new Call(source, s1);                            Contacts.AddPreviewContactDownHandler(l, OnLineDown);                            counter3++;                            SizeChangedEventHandler act = (Object o, SizeChangedEventArgs args) =>                            {                                BindingOperations.GetBindingExpressionBase(l, Line.X1Property).UpdateTarget();                                BindingOperations.GetBindingExpressionBase(l, Line.Y1Property).UpdateTarget();                                BindingOperations.GetBindingExpressionBase(l, Line.X2Property).UpdateTarget();                                BindingOperations.GetBindingExpressionBase(l, Line.Y2Property).UpdateTarget();                            };                            s1.getGrid().SizeChanged += act;                            g1.SizeChanged += act;                        }                    }                }            }         int counter4 = 1;         if (depth >= 4)         {             int invoCount = 0;             foreach (SourceFile s in list)             {                 foreach (SourceFile source in s.getInvocations())                 {                     foreach (SourceFile s1 in source.getInvocations())                     {                         foreach (SourceFile s2 in s1.getInvocations())                         {                             invoCount = invoCount + s1.getInvocations().Count;                         }                     }                 }             }             foreach (SourceFile s in list)             {                 foreach (SourceFile source in s.getInvocations())                 {                     foreach (SourceFile s1 in source.getInvocations())                     {                         foreach (SourceFile s2 in s1.getInvocations())                         {                             Grid g1 = new Grid();                             grids.Add(g1);                             g1.Width = 50;                             g1.Height = 50;                             g1.Tag = s2;                             Ellipse e1 = new Ellipse();                             e1.Width = 50;                             e1.Height = 50;                             e1.Fill = Brushes.Red;                             s2.setGrid(g1);                             g1.Children.Add(e1);                             if (depth == 4)                             {                                 Canvas.SetTop(g1, 585);                             }                             else if (depth == 5)                             {                                 Canvas.SetTop(g1, 488);                             }                             else if (depth == 6)                             {                                 Canvas.SetTop(g1, 418);                             }                             Canvas.SetLeft(g1, counter4 * (1000 / (invoCount + 1)));                             Viewbox box1 = new Viewbox();                             box1.Width = g1.Width;                             box1.Height = g1.Height;                             TextBox txt1 = new TextBox();                             txt1.Background = Brushes.Transparent;                             txt1.BorderBrush = Brushes.Transparent;                             txt1.Text = s2.getName();                             box1.Child = txt1;                             g1.Children.Add(box1);                             Line l = new Line();                             //Canvas.SetZIndex(l, 2);                             l.Stroke = Brushes.Green;                             l.StrokeThickness = 10;                             Binding x1 = new Binding(); x1.Path = new PropertyPath(Canvas.LeftProperty);                             x1.Converter = new MyConverter();                             x1.ConverterParameter = s1.getGrid();                             Binding y1 = new Binding(); y1.Path = new PropertyPath(Canvas.TopProperty);                             y1.Converter = new MyConverter();                             y1.ConverterParameter = s1.getGrid();                             Binding x2 = new Binding(); x2.Path = new PropertyPath(Canvas.LeftProperty);                             x2.Converter = new MyConverter();                             x2.ConverterParameter = g1;                             Binding y2 = new Binding(); y2.Path = new PropertyPath(Canvas.TopProperty);                             y2.Converter = new MyConverter();                             y2.ConverterParameter = g1;                             x1.Source = y1.Source = findGrid(grids, s1, s2);                             x2.Source = y2.Source = g1;                             l.SetBinding(Line.X1Property, x1);                             l.SetBinding(Line.Y1Property, y1);                             l.SetBinding(Line.X2Property, x2);                             l.SetBinding(Line.Y2Property, y2);                             Dependencies.Children.Add(l);                             l.Tag = new Call(s1, s2);                             Contacts.AddPreviewContactDownHandler(l, OnLineDown);                             counter4++;                             SizeChangedEventHandler act = (Object o, SizeChangedEventArgs args) =>                             {                                 BindingOperations.GetBindingExpressionBase(l, Line.X1Property).UpdateTarget();                                 BindingOperations.GetBindingExpressionBase(l, Line.Y1Property).UpdateTarget();                                 BindingOperations.GetBindingExpressionBase(l, Line.X2Property).UpdateTarget();                                 BindingOperations.GetBindingExpressionBase(l, Line.Y2Property).UpdateTarget();                             };                             s2.getGrid().SizeChanged += act;                             g1.SizeChanged += act;                         }                     }                 }             }         }      int counter5 = 1;      if (depth >= 5)      {          int invoCount = 0;          foreach (SourceFile s in list)          {              foreach (SourceFile source in s.getInvocations())              {                  foreach (SourceFile s1 in source.getInvocations())                  {                      foreach (SourceFile s2 in s1.getInvocations())                      {                          foreach (SourceFile s3 in s2.getInvocations())                          {                              invoCount = invoCount + s2.getInvocations().Count;                          }                      }                  }              }          }          foreach (SourceFile s in list)          {              foreach (SourceFile source in s.getInvocations())              {                  foreach (SourceFile s1 in source.getInvocations())                  {                      foreach (SourceFile s2 in s1.getInvocations())                      {                          foreach (SourceFile s3 in s2.getInvocations())                          {                              Grid g1 = new Grid();                              g1.Width = 50;                              g1.Height = 50;                              grids.Add(g1);                              g1.Tag = s3;                              Ellipse e1 = new Ellipse();                              //Dependencies.Children.Add(e1);                              e1.Width = 50;                              e1.Height = 50;                              e1.Fill = Brushes.Red;                              s3.setGrid(g1);                              g1.Children.Add(e1);                              if (depth == 5)                              {                                  Canvas.SetTop(g1, 596);                              }                              else if (depth == 6)                              {                                  Canvas.SetTop(g1, 511);                              }                              Canvas.SetLeft(g1, counter5 * (1000 / (invoCount + 1)));                              Viewbox box1 = new Viewbox();                              box1.Width = g1.Width;                              box1.Height = g1.Height;                              TextBox txt1 = new TextBox();                              txt1.Background = Brushes.Transparent;                              txt1.BorderBrush = Brushes.Transparent;                              txt1.Text = s3.getName();                              box1.Child = txt1;                              g1.Children.Add(box1);                              Line l = new Line();                              //Canvas.SetZIndex(l, 2);                              l.Stroke = Brushes.Green;                              l.StrokeThickness = 10;                              Binding x1 = new Binding(); x1.Path = new PropertyPath(Canvas.LeftProperty);                              x1.Converter = new MyConverter();                              x1.ConverterParameter = s2.getGrid();                              Binding y1 = new Binding(); y1.Path = new PropertyPath(Canvas.TopProperty);                              y1.Converter = new MyConverter();                              y1.ConverterParameter = s2.getGrid();                              Binding x2 = new Binding(); x2.Path = new PropertyPath(Canvas.LeftProperty);                              x2.Converter = new MyConverter();                              x2.ConverterParameter = g1;                              Binding y2 = new Binding(); y2.Path = new PropertyPath(Canvas.TopProperty);                              y2.Converter = new MyConverter();                              y2.ConverterParameter = g1;                              x1.Source = y1.Source = findGrid(grids, s2, s3);                              x2.Source = y2.Source = g1;                              l.SetBinding(Line.X1Property, x1);                              l.SetBinding(Line.Y1Property, y1);                              l.SetBinding(Line.X2Property, x2);                              l.SetBinding(Line.Y2Property, y2);                              l.Tag = new Call(s2, s3);                              Contacts.AddPreviewContactDownHandler(l, OnLineDown);                              Dependencies.Children.Add(l);                              counter5++;                              SizeChangedEventHandler act = (Object o, SizeChangedEventArgs args) =>                              {                                  BindingOperations.GetBindingExpressionBase(l, Line.X1Property).UpdateTarget();                                  BindingOperations.GetBindingExpressionBase(l, Line.Y1Property).UpdateTarget();                                  BindingOperations.GetBindingExpressionBase(l, Line.X2Property).UpdateTarget();                                  BindingOperations.GetBindingExpressionBase(l, Line.Y2Property).UpdateTarget();                              };                              s3.getGrid().SizeChanged += act;                              g1.SizeChanged += act;                          }                      }                  }              }          }      }            foreach (Grid grid in grids)            {                Dependencies.Children.Add(grid);                Contacts.AddPreviewContactDownHandler(grid, DownOnSourceFile);            }                }  ));        }Is there any easy way to improve that? And to make it working not only for 6 steps but also for n steps?
Too many loops in Drawing App
c#;performance;algorithm
Break this down into several methods - it's very long, meaning it's not easy to read.