Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use a thread for this?

Tags:

c#

.net

winforms

I am currently working on a sales point program that might be used by many computers using the same local database.

I am asking the following question because right now I have everything set up so only one computer uses the program at a time, which is realistically naive. However, I also want to know if it's really viable for me to implement something like Threads and Locks on this or if I'm just going to waste time and valuable resources.

Can anyone please tell me? Thank you! I will add an excerpt code that might be ran by different processes:

 private void PuntoDeVenta_Load(object sender, EventArgs e)
    {
        //Connect to the local database and check if we successfully connected.
        conn = new ConexionBD();
        if (!conn.conectar())
        {
            MessageBox.Show("Hubo un error al conectarse a la base de datos. Favor de verificar.", "Error", MessageBoxButtons.OK);
            this.Close();
        }
        comboBox1.SelectedIndex = 0;
        groupBox3.Enabled = false;
        this.KeyUp += new System.Windows.Forms.KeyEventHandler(KeyEvent);
        string[] detalle = conn.informacionEmpresa();
        iva = Convert.ToInt16(detalle[10]);
        label15.Text = "Impuesto (" + iva + "%):";
        //Get the highest order number until now. Not sure if I need to use Thread here.
        String maxPedido = conn.detalleNumeroPedidoMasReciente();
        int n;
        if (int.TryParse(maxPedido, out n))
        {
            maxPedido = (n + 1).ToString();
        }
        else
        {
            n = Convert.ToInt32(Regex.Match(maxPedido, @"\d+").Value);
            String nonNumeric = String.Concat(maxPedido.Where(c => !Char.IsDigit(c)));
            maxPedido = nonNumeric + (n + 1).ToString();
        }
        textBox1.Text = maxPedido;
    }
like image 449
Julio Garcia Avatar asked Nov 30 '22 13:11

Julio Garcia


1 Answers

Think about threads the way you would think about hiring workers for a business. When do you hire workers for a business? Only when you have lots of work for them to do, because hiring is expensive.

Ideally you should make a new thread only when these conditions are met:

  • The work to do is CPU intensive
  • The work is long -- definitely more than 30 milliseconds, more like seconds or minutes of work.
  • There is an idle CPU that can be 100% dedicated to servicing that thread.

In your case, it looks like you're considering hiring a worker to run a database query. Well, would you hire a worker for your business whose only job was to do nothing except send a single letter, and then pay them to sleep while they waited for the reply? Of course not. So don't hire a thread to do that either. If you have high-latency work to do that is not CPU-bound, make an asynchronous workflow on a single thread.

An asynchronous workflow does not hire a new worker. Rather, it is like you send a letter, and while you're waiting for the reply to arrive you find other work to do, thereby making efficient use of your time. When the reply arrives, you pick up the "letter workflow" where you left off.

like image 173
Eric Lippert Avatar answered Dec 05 '22 01:12

Eric Lippert