Created an application that checks for updates on the server every 5 minutes. The application can only run one on the local machine, I control it via mutex:

instanceMutex = new Mutex(true, @"Global\" + Assembly.GetExecutingAssembly().GetType().GUID.ToString(), out createdNew); //Local if (!createdNew) { MessageBox.Show("1mf-alno Updater запущена у другого пользователя. Нельзя запускать две копии 1mf-alno Updater на одном компьютере. Обновления для вас не доступны."); instanceMutex = null; Current.Shutdown(); return; } 

The class that checks for updates made through the singleton pattern, I will not post all its code, only the constructor with the implementation of the singleton:

 private static FPTClass instance; public static FPTClass Instance { get { if (instance == null) { instance = new FPTClass(host, user, pass); } return instance; } } private FPTClass(string hostIP, string userName, string password) { try { //отдельный поток для progressBar worker = new BackgroundWorker(); worker.WorkerReportsProgress = true; worker.DoWork += new DoWorkEventHandler(worker_DoWork); worker.ProgressChanged += worker_ProgressChanged; string catalogUpd = ""; using (wiupDataContext dcUser = new wiupDataContext()) { catalogUpd = dcUser.RegKeys.FirstOrDefault(x => x.key == SingleApp.superpro.serialNum).catalog.Trim(); } host = hostIP + catalogUpd + "/"; user = userName; pass = password; //запускаем таймер проверки обновления на сервере ftp timer.Elapsed += (sender, args) => { if (processing || !isOk) return; processing = true; try { if (ftpResponse != null) { //на всякий случай ftpResponse.Close(); ftpRequest = null; } checkNewCatalog(); processing = false; } catch (Exception ex) { SingleApp.notifyIcon.ShowBalloonTip("Ошибка обновления каталога.", ex.Message, BalloonIcon.Error); processing = false; } }; timer.Start(); } catch (Exception e) { SingleApp.utilityClass.ДобавитьЗаписьВЛог(e, "tNgStN"); } } 

During the update process, an error occurs that is fixed on SQL Server, the cause of the error is clear, but from the screenshot you can see that the record is duplicated, i.e. at the time of the error, there were 2 threads that caused this code almost simultaneously, why is this happening? Where is the second stream formed? How can I track this? enter image description here

  • Give the code that throws an error. Judging by the text of the exception - you somewhere call Single / SingleOrDefault. In the code you give, this call is not. - PashaPash
  • Yes, that's right, I know that I have more than one entry in the table, although I am trying to execute the following code: PendingUpd curRec = pendUpd.Single (); This is not the problem; the problem is in two streams, which should not be, there should always be only one - MiXaiL
  • To be honest, I don’t understand how pendUpd.Single() is related to the code in question. How I understood - almost no. The question should contain a minimal, self-sufficient and reproducible example . - PashaPash
  • @PashaPash, you are right this line does not relate in any way to my problem, and the error also arises, my problem is that I create an extra thread, although I seem to have envisaged everything and considered it, but it is there and it can be seen on the screen, How can I find this stream? Can I not correctly designed singleton or mutex? I laid out the code above - MiXaiL
  • The answer was given below (most likely correct, not sure why the author deleted it) - you have a non-thread safe implementation of Singleton. Copy it to thread-safe ( www.stackoverflow.com/a/486519/177221 , version with static readonly). If it doesn't help, then the problem is in another place: ( - PashaPash

0