Click here to Skip to main content
15,889,699 members
Home / Discussions / C#
   

C#

 
GeneralRe: Cross-Threading problem, InvokeRequired and Events Pin
User 680178015-Feb-10 10:38
User 680178015-Feb-10 10:38 
GeneralRe: Cross-Threading problem, InvokeRequired and Events Pin
Luc Pattyn15-Feb-10 11:30
sitebuilderLuc Pattyn15-Feb-10 11:30 
GeneralRe: Cross-Threading problem, InvokeRequired and Events Pin
User 680178015-Feb-10 10:45
User 680178015-Feb-10 10:45 
GeneralRe: Cross-Threading problem, InvokeRequired and Events Pin
Luc Pattyn15-Feb-10 11:23
sitebuilderLuc Pattyn15-Feb-10 11:23 
GeneralRe: Cross-Threading problem, InvokeRequired and Events Pin
Daniel Grunwald16-Feb-10 8:41
Daniel Grunwald16-Feb-10 8:41 
GeneralRe: Cross-Threading problem, InvokeRequired and Events Pin
Luc Pattyn16-Feb-10 9:06
sitebuilderLuc Pattyn16-Feb-10 9:06 
GeneralRe: Cross-Threading problem, InvokeRequired and Events Pin
Daniel Grunwald16-Feb-10 9:26
Daniel Grunwald16-Feb-10 9:26 
GeneralRe: Cross-Threading problem, InvokeRequired and Events Pin
User 680178016-Feb-10 9:42
User 680178016-Feb-10 9:42 
Very interesting approach.
Let me make clear what I am doing. I am trying to build an ANN (neural net).
1. TextBox is used for displaying information in real time, i.e. the number of training pairs, the starting time, end time, etc. This is just for me to keep track, plus it looks cool Smile | :)
2. A class with all the GUI controls and a class with the main training method. A delegate and event in the training class serve as the synch update of the information (also for number 1). e.g.
trainingANN_instance_LogReportSynch

3. I am not very familiar with threads, I've tired to use the approach with InvokeRequired, but I cannot understand why my events do not work. As an example:
private void butTraining_MouseClick(object sender, MouseEventArgs e)
       {

           this.trainingANN_thread = new Thread(new ThreadStart(this.Initiate_ANN_Training));

           LockOptions();

           this.tbVeryBig.Clear();

           //trainingANN_thread.IsBackground = true;

           this.trainingANN_thread.Start();

           this.tbVeryBig.Text += "[" + DateTime.Now.TimeOfDay.ToString() + "]: INITIALIZATION\r\n";
       }


private void SettingsInfoDisplay()
{
    if (this.InvokeRequired)
    {
        DelegateToCrossThread_None crossThreader = new DelegateToCrossThread_None(SettingsInfoDisplay);
        this.Invoke(crossThreader);
    }
    else
    {
        this.tboxOutputNeuron.Text = "" + outputNeurons;
        this.tboxInputNeuron.Text = "" + inputNeurons;
        this.tboxHiddenNeuron.Text = "" + tbarHiddenN.Value;

        this.tboxErrorTreshold.Text = "" + (this.tbarErrorTreshold.Value / 100.0);
        this.tboxTrainingRate.Text = "" + (this.tbarTrainingRate.Value / 100.0);

        this.tbBadF.Text = "<not defined>";
        this.tbAccuracy.Text = "<not defined>";
        this.tbConvergeRate.Text = "<not defined>";
        this.tbErrorCounter.Text = "<not defined>";
        this.tbRatio.Text = "<not defined>";
        this.tbGoodF.Text = "<not defined>";
        this.tbEpoch.Text = "<not defined>";
    }
}

/// </summary>
      private void Options()
      {
          if (this.InvokeRequired)
          {
              DelegateToCrossThread_None crossThreader = new DelegateToCrossThread_None(Options);
              this.Invoke(crossThreader);
          }
          else
          {
              errorTreshold = (double)((tbarErrorTreshold.Value) / 100.0); //error filter
              learningRate = (double)((tbarTrainingRate.Value) / 100.0); //learning rate
              hiddenNeurons = tbarHiddenN.Value;

              //progress filter (converge rate)
              //skipped

              //log options
              if (this.cboxLog.Checked != true)
              {
                  logOption = false; //brief, performance OK
              }
              else
                  logOption = true; //detailed, performance BAD
          }
      }


//Gatter all the settings for the ANN and initiate training
       private void Initiate_ANN_Training()
       {
           if (this.InvokeRequired)
           {
               DelegateToCrossThread_None crossThreader = new DelegateToCrossThread_None(Initiate_ANN_Training);
               this.Invoke(crossThreader);
           }
           else
           {
               SettingsInfoDisplay(); //displaying information for visual guiding
               Options(); //options for training

               this.tbVeryBig.AppendText("text");

               //instanciate training class
               trainingANN_instance = new ANN_Training();

               this.tbVeryBig.Text += "[" + DateTime.Now.TimeOfDay.ToString() + "]: ANN TRAINING STARTED!\r\n";

               trainingANN_instance.GoodFactsSynch += new StatsSynch(trainingANN_instance_GoodFactsSynch); //updates textbox of good facts
               trainingANN_instance.BadFactsSynch += new StatsSynch(trainingANN_instance_BadFactsSynch); //updates tbox of bad
               trainingANN_instance.RatioSynch += new StatsSynch(trainingANN_instance_RatioSynch); //updates tbox ratio
               trainingANN_instance.ConvergeRateSynch += new StatsSynch(trainingANN_instance_ConvergeRateSynch); //converge
               trainingANN_instance.EpochSynch += new StatsSynch(trainingANN_instance_EpochSynch);//epoch
               trainingANN_instance.LogReportSynch += new StringSynch(trainingANN_instance_LogReportSynch); //log reporter
               trainingANN_instance.BadFactsForGraphSynch += new StatsSynch(trainingANN_instance_BadFactsForGraphSynch); //graph
               //trainingANN_instance.WHMatrixSynch += new Weights_MatrixSynch(trainingANN_instance_WHMatrixSynch);
               //trainingANN_instance.WOMatrixSynch += new Weights_MatrixSynch(trainingANN_instance_WOMatrixSynch);

               //training ANN
               trainingANN_instance.Training(inputNeurons, hiddenNeurons, outputNeurons, errorTreshold, learningRate, progressFilter, logOption);

               this.tbVeryBig.Text += "[" + DateTime.Now.TimeOfDay.ToString() + "]: ANN TRAINING FINISHED!\r\n";

               //result stats of the training & recognition
               double ratio = (trainingANN_instance.good / trainingANN_instance.bad * 100);
               this.tbRatio.Text = (Math.Round(ratio, 5)) + "%";
               this.tbEpoch.Text = trainingANN_instance.epoch.ToString();
               this.tbBadF.Text = trainingANN_instance.bad.ToString();
               this.tbGoodF.Text = trainingANN_instance.good.ToString();
               this.tbConvergeRate.Text = Math.Round((trainingANN_instance.progressRatio_temp), 5).ToString() + "%";

               this.tbVeryBig.Text += "[" + DateTime.Now.TimeOfDay.ToString() + "]: RESULTS:\r\n EPOCH: " + trainingANN_instance.epoch.ToString();
               this.tbVeryBig.Text += "\r\n BAD FACTS: " + trainingANN_instance.bad.ToString();
               this.tbVeryBig.Text += "\r\n GOOD FACTS: " + trainingANN_instance.good.ToString();
               this.tbVeryBig.Text += "\r\n RATIO: " + ratio + "\r\n CONVERGE RATE: " + Math.Round((trainingANN_instance.progressRatio_temp), 5).ToString() + "%";
               //unlock options
               UnlockOptions();
               //terminate thread
               trainingANN_thread.Abort();
           }
       }


private void trainingANN_instance_RatioSynch(double variable)
{
    if (this.InvokeRequired)
    {
        DelegateToCrossThread_Double del = new DelegateToCrossThread_Double(trainingANN_instance_RatioSynch);
        this.Invoke(del);
    }
    else
    {
        //Application.DoEvents();
        this.tbRatio.Text = "" + variable;
    }
}

So as you can see i commented App.DoEvents(). Without that line, events do not work, using my approach.

Another observation. When logOption was false or no DoEvents() in the code present, and textbox used to be updated with the += approach, the application used to freeze for certain seconds and only show the output after the training method of class 2 was done, however, if i try to display that info using Console.Write(), it worked fine. However, now, with new ideas with AppendText(), it works without problems.

modified 1-Aug-19 21:02pm.

GeneralRe: Cross-Threading problem, InvokeRequired and Events Pin
Daniel Grunwald16-Feb-10 9:49
Daniel Grunwald16-Feb-10 9:49 
GeneralRe: Cross-Threading problem, InvokeRequired and Events Pin
User 680178016-Feb-10 9:52
User 680178016-Feb-10 9:52 
GeneralRe: Cross-Threading problem, InvokeRequired and Events Pin
Daniel Grunwald16-Feb-10 9:59
Daniel Grunwald16-Feb-10 9:59 
GeneralRe: Cross-Threading problem, InvokeRequired and Events Pin
User 680178016-Feb-10 10:31
User 680178016-Feb-10 10:31 
GeneralRe: Cross-Threading problem, InvokeRequired and Events Pin
Daniel Grunwald16-Feb-10 8:45
Daniel Grunwald16-Feb-10 8:45 
GeneralRe: Cross-Threading problem, InvokeRequired and Events Pin
User 680178016-Feb-10 9:08
User 680178016-Feb-10 9:08 
GeneralRe: Cross-Threading problem, InvokeRequired and Events Pin
Daniel Grunwald16-Feb-10 9:39
Daniel Grunwald16-Feb-10 9:39 
GeneralRe: Cross-Threading problem, InvokeRequired and Events Pin
Luc Pattyn16-Feb-10 9:45
sitebuilderLuc Pattyn16-Feb-10 9:45 
GeneralRe: Cross-Threading problem, InvokeRequired and Events Pin
User 680178016-Feb-10 10:02
User 680178016-Feb-10 10:02 
GeneralRe: Cross-Threading problem, InvokeRequired and Events Pin
User 680178016-Feb-10 10:09
User 680178016-Feb-10 10:09 
GeneralRe: Cross-Threading problem, InvokeRequired and Events Pin
User 680178017-Feb-10 7:05
User 680178017-Feb-10 7:05 
AnswerRe: Cross-Threading problem, InvokeRequired and Events Pin
#realJSOP15-Feb-10 23:21
mve#realJSOP15-Feb-10 23:21 
GeneralRe: Cross-Threading problem, InvokeRequired and Events Pin
User 680178019-Feb-10 4:10
User 680178019-Feb-10 4:10 
GeneralRe: Cross-Threading problem, InvokeRequired and Events Pin
User 680178019-Feb-10 8:06
User 680178019-Feb-10 8:06 
QuestionC# TableLayoutPanel MouseLeave Pin
ikurtz15-Feb-10 5:27
ikurtz15-Feb-10 5:27 
AnswerRe: C# TableLayoutPanel MouseLeave Pin
AhsanS15-Feb-10 5:48
AhsanS15-Feb-10 5:48 
GeneralRe: C# TableLayoutPanel MouseLeave Pin
ikurtz15-Feb-10 6:21
ikurtz15-Feb-10 6:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.