并发问题不确定如何解决

时间:2018-07-07 03:01:10

标签: c# ado.net

问题是:如果两个用户(或更多)同时使用我的应用程序。用户输入一个值123,然后按保存。然后,用户两个尝试输入值123。为了使bool serialExist语句正常工作,我必须重新加载表适配器的新副本。如果数据库很大,则会减慢应用程序的速度,这将成为一个问题。也许有人知道我该怎么做。还是我只是坚持每次重新加载表适配器?我确实尝试了并发,但是它似乎没有用..我知道我缺少了一些..

private void btnSave_Click(object sender, EventArgs e)
                    {
                        try
                        {
                            if (IsValidData())
                            {

                                int serialStart = int.Parse(maskedSerial.Text.Remove(2, 1));
                                int serialEnd = int.Parse(txtCount.Text);

                                // Sets progressbar to visible..
                                progressBar1.Visible = true; 
                                progressBar1.Maximum = serialEnd;

                                for (int i = 0; i < serialEnd; i++)
                                {
                                    progressBar1.Value++;
                                    progressBar1.Update();

                                    int answer = serialStart++;

                                    bool serialExist = appData.Serial_Log.Any(SerialLogs => SerialLogs.Serial.Contains(answer.ToString("00-0000")));  // Checks to see if serial exist

                                    if (serialExist)
                                    {


                                        // Create list of serials that exist
                                        serialsSkippedList.Add(answer);

                                        // Sets count back by 1 each time a serial number exist
                                        i--; 

                                        progressBar1.Value--;
                                        progressBar1.Update();
                                        continue;
                                    }
                                    else
                                    {
                                        // Create a new Entry object
                                        AppData.Serial_LogRow newEntry = appData.Serial_Log.NewSerial_LogRow();

                                        // Set field values for this new entry
                                        newEntry.Serial = answer.ToString("00-0000");
                                        newEntry.Customer = comboCustomer.Text.ToUpper();
                                        newEntry.Antenna = comboAntenna.Text.ToUpper();
                                        newEntry.JobNumber = maskedJobNumber.Text.ToUpper();
                                        newEntry.Notes = txtNotes.Text;
                                        newEntry.Date = dateTimePicker1.Value;

                                        // Add the row to the database
                                        appData.Serial_Log.AddSerial_LogRow(newEntry);

                                        // Update the serialsTableAdapter object
                                        serial_LogTableAdapter.Update(newEntry);
                                        // Close the form

                                        // Create list of serial that are issued
                                        serialList.Add(answer);
                                        this.Close();
                                    }
                                }

                                // Message box list of serials skipped because they already exist
                                if (serialsSkippedList.Count != 0)
                                {
                                    serialsSkippedList.Sort();
                                    string messageExist = "Sorry these serial numbers existed in the database already, so they were skipped \n\n";
                                    foreach (int serialExist in serialsSkippedList)
                                    {
                                        messageExist += serialExist.ToString("00-0000") + ", ";
                                    }
                                    // Display message box for skipped serials
                                    MessageBox.Show(messageExist, "Serial numbers already exist", MessageBoxButtons.OK, MessageBoxIcon.Error); 
                                }

                                // Message box list of serial numbers that are issued
                                serialList.Sort();
                                string message = "New serial numbers have been successfully entered in the data base, here is a list of issued serial numbers\n\n" +
                                    "Customer: " + comboCustomer.Text.ToUpper() +
                                    "\nAntenna: " + comboAntenna.Text.ToUpper() +
                                    "\nJob Number: " + maskedJobNumber.Text.ToUpper() + "\n" + "Serial/Serials Issued: ";
                                foreach (int serialsIssued in serialList)
                                {
                                    message += serialsIssued.ToString("00-0000") + ", ";
                                }
                                // Display message box for the list
                                MessageBox.Show(message, "New Serial Numbers issued", MessageBoxButtons.OK, MessageBoxIcon.Information);

                            }
                        }
    catch (DBConcurrencyException)
                    {
                        MessageBox.Show("A concurrency error occurred. " + "The row was not upddated.", "Concurrency Exeption", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message + "\n\n" +
                            ex.GetType().ToString() + "\n" +
                            ex.StackTrace, "Exception");
                    }

1 个答案:

答案 0 :(得分:0)

这是桌面应用程序而不是Web应用程序或服务很重要。

根据您的情况,您可以尝试使用Redis服务器及其提供的锁定功能。 StackExchange.Redis库将使这变得非常容易。伪代码...

redis.LockTake(serial);
//check if serial exists
//create if not
redis.LockRelease(serial);
//next serial...
相关问题