Jump to content
  • Advertisement
Sign in to follow this  
Peter5897

Refreshing a WPF Window

This topic is 3647 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I have created a C++ program that, given a track, will attempt to find a path using a genetic programming algorithm. I have also created a C# application using WPF which will draw both the track and path. In the C# program, I allow the user to select a track file and then generate paths by clicking two buttons, Track and Path. When Path is clicked, I start my C++ program by creating a Process. My C++ app then spits out the path using the StandardOutput and I read this Asynchronously from my C# app. I am trying to create a situation where the C++ app can send out periodic updates to the C# app and the C# app will then display the current path to the user while the C++ app is running in the background. I am able to receive these path updates just fine but my window does not update with the new information. I suspect that the Process's ASynchronous reads are taking a higher priority than the redraw which is why I can not update.
    // this function is called when the "Path" button is clicked
    private void StartEvolutionStrategies( )
    {
      m_progressBar.IsIndeterminate = true;

      es.Close();

      es = new Process(); // create my process
      es.StartInfo.WorkingDirectory = workingDirectory;
      es.StartInfo.FileName = "Evolution Strategies.exe";
      es.StartInfo.CreateNoWindow = true;
      es.EnableRaisingEvents = true;
      es.OutputDataReceived += new DataReceivedEventHandler(DataReceivedEventHandler);
      es.Exited += new EventHandler(ExitEventHandler);

      es.StartInfo.UseShellExecute = false;
      es.StartInfo.RedirectStandardOutput = true;
      es.StartInfo.Arguments = trackFilename + " ExtraArguments true";
      es.Start();
      es.BeginOutputReadLine(); // begin reading the output asynchronously
    }

    // function called whenever the Process outputs data
    private void DataReceivedEventHandler(Object sender, DataReceivedEventArgs e)
    {
      string line = e.Data as string;
      if ((line == null) || (line == ""))
        return;

      if (line.Contains("New Path"))
        path.ClearSegments(); // clears out the old path
      else if (line.Contains("Redraw"))
        Dispatcher.BeginInvoke(DispatcherPriority.Send, new DrawDelegate(Draw));  // draw updates the Window's Canvas and Children
      else
        path.AddSegment(e.Data);  // adds another part of the path
    }

    private void Draw()
    {
      m_canvas.Children.Clear();

      foreach (Triangle tri in m_triangles) // draw the track
      {
        tri.Draw(m_canvas);
      }

      if (m_triangles.Count != 0 && path != null) // draw the path
      {
        Triangle triangle = m_triangles[0];

        Point pos = new Point();
        pos.X = (triangle.Point1.X + triangle.Point2.X) * .5;
        pos.Y = (triangle.Point1.Y + triangle.Point2.Y) * .5;

        path.Draw(m_canvas, pos);
      }

      UpdateLayout();
    }


I haven't done much work with multithreading before so any help would be greatly appreciated. ***EDIT*** Maybe this would be more useful in the .Net forum?

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!