0

I have a WPF application for plotting a chart dynamically a shown in the following code.

        public ObservableCollection<KeyValuePair<int, double>> Items { get; set; }
        public void startgraph
        {
        for (i = 0; i < 60; i++)
            plot_values[i] = random.NextDouble() * 100;

        bw = new BackgroundWorker();

        bw.DoWork += (o, ea) =>
        {
            index = 1;

                while (index < 60)
                {
                    this.Dispatcher.BeginInvoke(new Action(() =>
                    {
                        Items.Add(new KeyValuePair<int, double>(index++, plot_values[index]));
                    }));
                    System.Threading.Thread.Sleep(1000);

                }

        };
        bw.RunWorkerAsync();
    }

I want to have a pause button which when clicked pauses the plot and when clicked again resumes the plot. Please help.

Zeena
  • 1
  • 1
  • when worker resumes, it starts from current index in your while loop, isn't it? – TriV Apr 08 '17 at 05:07
  • 2
    see this topic http://stackoverflow.com/questions/3065700/how-to-start-and-stop-a-continuously-running-background-worker-using-a-button – TriV Apr 08 '17 at 05:09
  • Make sure you follow the example in [this answer](http://stackoverflow.com/a/3065784) in the marked duplicate, and **not** the accepted answer. The accepted answer is about the worst way to go about "pausing" a worker. – Peter Duniho Apr 08 '17 at 06:41
  • Also, I'll note that `BackgroundWorker`, while still a viable solution for worker tasks, is a bit clunky as compared to the new `async`/`await` paradigm. And if you implement your background task using `Task.Run(async () => ...);`, you can use the information in [A pattern to pause/resume an async task?](http://stackoverflow.com/q/19613444) to implement pause/resume in a very elegant way (one which does not tie up _any_ thread while the task is paused). – Peter Duniho Apr 08 '17 at 06:55
  • Thank u . It works!! – Zeena Apr 08 '17 at 07:38

0 Answers0