-3

Hi I am working on a TABU Search Algorithm and I need to access many functions to make it readable and easy I have used buttons and button.performClick();

Can someone let me know what I am doing wrong and how I can solve this issue? Thank you

    struct DataParameter
    {
        public int Process;
        public int Delay;
    }
    private DataParameter _inputparameter;

    private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        progressBar1.Value = e.ProgressPercentage;
        lblBackGroundWorker.Text = string.Format("Processing...{0}%", e.ProgressPercentage);
        progressBar1.Update();
    }

    private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
    {
        int process = ((DataParameter)e.Argument).Process;
        int delay = ((DataParameter)e.Argument).Delay;
        int index = 1;
        try
        {
            //progressBar1.Value = 0;
            //progressBar1.Update();
            progressBar = 1;
            //this.tmrTimeAndDate.Start();
            Reset_Clear_For_New_Timetable();
            sw_WhileLoop = Stopwatch.StartNew();
            while (whileLoop > 0)
            {
                if (!backgroundWorker.CancellationPending)
                {
                    backgroundWorker.ReportProgress(index++ * 100 / process, string.Format("Processing...{0}%", progressBar));
                    Thread.Sleep(delay); // used to simulate length of operation

                    if (whileLoop == 99)
                    {

                        Console.WriteLine("initial"); // initial
                        cmd_Start_Scheduling.PerformClick(); // create timetable
                        Get_Timetable_Send_To_Initial_DataTable(); // get timetable to the initial table
                        Get_Timetable_Send_To_Optimal_DataTable(); // get timetable to the optimal table
                        Calculate_Energy_High_Score_Initial();//calculate high score of initial solution
                        Calculate_Energy_High_Score_Optimal();//calculate high score of optimal solution
                        Reset_Clear_For_New_Timetable();

                    }
                    else
                    {
                        Console.WriteLine("not initial");// not initial
                        cmd_Start_Scheduling.PerformClick(); // create timetable
                        Fill_New_Solution();
                        Compare_Solution_Keep_The_Best();
                        Reset_Clear_For_New_Timetable();
                    }

                    whileLoop = whileLoop - 1;
                    progressBar = progressBar + 1;


                }
            }
            sw_WhileLoop.Stop();
            MessageBox.Show("Time taken: " + sw_WhileLoop.Elapsed.TotalSeconds.ToString() + " seconds \n Scheduling ended on step 7 because there was no Sup Class Left.");
            MessageBox.Show("Done");
            TimetableOutputQuestion TimetableOutputQuestionOpen = new TimetableOutputQuestion();
            this.Hide();
            TimetableOutputQuestionOpen.Show();
            progressBar = 1;
            whileLoop = 99;

        }
        catch (Exception ex)
        {
            backgroundWorker.CancelAsync();
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

    private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        MessageBox.Show("Timetable Scheduling Process Has Been Completed", "Done", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }

    private void CmdStartScheduling_Click(object sender, EventArgs e)
    {
        if (!backgroundWorker.IsBusy)
        {
            _inputparameter.Delay = 100;
            _inputparameter.Process = 1200;
            backgroundWorker.RunWorkerAsync(_inputparameter);
        }
    }

    private void cmdStopScheduling_Click(object sender, EventArgs e)
    {
        if (backgroundWorker.IsBusy)
        {
            backgroundWorker.CancelAsync();
        }
    }
user2038084
  • 27
  • 1
  • 7
  • 2
    Please review the guidelines of [how to ask a good question](http://stackoverflow.com/help/how-to-ask). In particular - what is happening vs what are you expecting to happen. Do you get an error message? If so, what is it? What is `button.performClick` - it doesn't appear in your code? – RB. Feb 28 '17 at 12:29
  • Also, this might well be a duplicate of http://stackoverflow.com/questions/142003/cross-thread-operation-not-valid-control-accessed-from-a-thread-other-than-the - this is a very common problem... – RB. Feb 28 '17 at 12:30
  • Don't try to show a messagebox in `DoWork`. This method is executing on a different thread than your UI thread. Pass the information on and handle the error in `RunWorkerCompleted` event, which is executed back on the UI thread. – René Vogt Feb 28 '17 at 12:32
  • Why are you calling cmd_Start_Scheduling.PerformClick() from DoWork when you test in the click handler that it should only do something when that backgroundworker is not running? Looks nonsensical. – Ralf Feb 28 '17 at 12:32
  • Because that Button is connected to a function – user2038084 Feb 28 '17 at 12:33
  • You can call that method without the button. Its just a method. Even if calling that method still wouldn't make to much sense because it wouldn't do anything. – Ralf Feb 28 '17 at 12:35
  • @Ralf do you know what TABU Algorithm is? – user2038084 Feb 28 '17 at 12:37
  • No. But should be irrelevant to your problem. – Ralf Feb 28 '17 at 12:40
  • @Ralf well it needs to look into a lot of constraints soft and hard and it will be a lot of loops, to make these steps smaller I have used methods and they will be visited. – user2038084 Feb 28 '17 at 12:44
  • You're manipulating the progress bar from another thread. The background workers "DoWork" method is fired on a background thread. You cannot manipulate controls from threads that did not create it – WBuck Feb 28 '17 at 13:16

1 Answers1

-2

Add This code to the end in your while loop (inside the loop)

DateTime timeout = DateTime.Now.AddMilliseconds(50);
while (DateTime.Now < timeout) Application.doEvents;

it should do the trick.

Good luck

Tim Jones
  • 63
  • 1
  • 10
  • why did I get a -1? I am just helping the guy, it might not be the best answer but it works – Tim Jones Feb 28 '17 at 14:58
  • 1
    @TimJones If you know that it's a bad answer, then why did you post it in the first place? And why are you surprised if you get downvotes for an answer that you *know* is a bad answer? – Servy Feb 28 '17 at 15:17
  • 2
    You've just led the OP down a path that will likely cause him more problems and possibly difficult to debug ones at that! – Chris Dunaway Feb 28 '17 at 15:20