How do I make a thread act according to the arrangement data?

0

I have the following code in c # with windows forms inside a button:

new Task(() => {
  Task[] tareas = {

    Task.Factory.StartNew(() => metodo1(), CancellationToken.None, TaskCreationOptions.AttachedToParent, tarea),
    Task.Factory.StartNew(() => metodo2(), CancellationToken.None, TaskCreationOptions.AttachedToParent, tarea)

  };
}).Start();


public void metodo1() {
  for (int v = 0; v < arreglointernet.Length; v++) {
    if (progressBar1.Visible == false) {
      progressBar1.Visible = true;
    }
    if (progressBar1.Value < 100) {
      progressBar1.Value++;
    }

    if (arreglointernet[v].Contains("D")) {

      richTextBox3.Text += arreglointernet[v].ToString() + "\nindex of " + arreglointernet[v].LastIndexOf(";") + "\n" + "Debito de internet aplicado\n";
    } else {
      richTextBox3.Text += arreglointernet[v].ToString() + "\nindex of " + arreglointernet[v].LastIndexOf(";") + "\n" + "credito de internet aplicado\n";

    }



  }

}

public void metodo2() {
  for (int vv = 0; vv < arregloproceso.Length; vv++) {
    if (progressBar1.Visible == false) {
      progressBar1.Visible = true;
    }
    if (progressBar1.Value < 100) {

      progressBar1.Value++;
    }
    if (arregloproceso[vv].Contains("D")) {

      richTextBox3.Text += arregloproceso[vv].ToString() + "\nindex of " + arregloproceso[vv].LastIndexOf(";") + "\n" + "Debito de proces aplicado\n";
    } else {
      richTextBox3.Text += arregloproceso[vv].ToString() + "\nindex of " + arregloproceso[vv].LastIndexOf(";") + "\n" + "credito de proces aplicado\n";

    }
  }

}

The above generates the following result after method 1 or method 2:

Arrangements with transactions are ready to begin with the processI; C; 2000 index of 3 Internet credit applied I; D; 1000 index of 3 Debit of internet applied

index of -1 Internet credit applied P; C; 4000.54 index of 3 applied process credit P; D; 2000 index of 3 Debit of applied process

index of -1 applied process credit

index of -1 applied process credit

How do I not take into account those indexOf -1 is to say that the thread stops when there is no data in an array ?????

    
asked by arsoft cr 16.07.2018 в 00:14
source

1 answer

0

Why do not you simply add a condition before the assignment?

        richTextBox3.text += ....

You are getting indexof -1 because your variable does not contain the character

  

";"

So what you have to do is put a comparison to know if the variable contains the character.

 if(arreglointernet[v].contains(";"))
 {   
      richTextBox3.text += ....
 }

Handling timer:

One advantage of Timers is that once they are started, they will execute their process on secondary threads at an interval that we choose:

you can either use the control from the toolbar or declare and create the instance from code

Winforms timers:

    public partial class Form1 : Form
    {
        Timer timer; //declarando la instancia global

    form_load(....)
    {
        timer = new Timer(); //Creando la instancia
        //estos son los parametros que tienes que asignar ya sea en codigo o desde el diseñador

        timer.Enabled = false; // asi el timer no correra desde el inicio 
        timer.Interval = 7000; //7000 milisegundos = 7 segundos
        timer.Tick += new EventHandler(Timertick); //el evento que sera invocado cada 7 segundos 
    }

        buttonStartClick()
        {
              timer.Start();//inicia el timer
        } 
         buttonStopClick()
        {
              timer.Stop();//Detiene el timer
        } 

    private  void Timertick(object sender, EventArgs e)
    {
        metodo1();
        metodo2();
    }

If you have problems with Ambiguity of the timer, you have to use the System.Windows.Forms.Timer . the class I think the threading class also has timers, but those are not what you need.

    
answered by 16.07.2018 / 17:39
source