There is a listBox1 and listbox2 when I select values ​​from the first one from the database the information is loaded and entered into the second one. I want to do that in the second thread, the information in the second is updated because data can change quickly. I wrote it all and made a button to update, everything works.

now I want without a button, so that the stream starts first and performs updates every х seconds / minutes ...- and it works. But if I make updates more frequent (for clarity, 1500 ms, to catch an exception), when I click on the listbox, I get an exception (as I noticed before the listbox is updated), but not always, on a slow update, it can be “not met”, except Click right before the update (it seems to me, maybe not right).

I tried to make the listbox inactive at the moment of the start of the execution of a thread, but to turn it back on again at the end of the cycle does not help. In the exception, the Link to the object is written does not indicate an instance of the object. I do not understand how it does not indicate if the listbox is with values. Tell me why it occurs, and what to do with it, what would the user work as usual and at the same time the data was updated?


The code is long

listbox layout (as usual, only telerikovsky)

 <telerik:RadListBox Name="LB_Ocenki_Semester"> <telerik:RadListBox.ItemTemplate > <DataTemplate> <Grid > <TextBlock TextWrapping="Wrap" Text="{Binding Path=nazvanieDisciplini, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Height="23" HorizontalAlignment="Left" Name="label24" VerticalAlignment="Top" Width="160" FontWeight="Bold" FontSize="14" /> <Label Content="{Binding Path=ocenka, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Margin="160,-3,0,0" Height="23" Width="40" HorizontalAlignment="Left" Name="label136" VerticalAlignment="Top" FontWeight="Bold" FontSize="14" /> </Grid> </DataTemplate> </telerik:RadListBox.ItemTemplate> </telerik:RadListBox> есть методы, например для получения таблицы дисциплин,оценок у студента на выбранном семестре public DataTable Get_Semester_Students_Ocenki(string Zachetka,string Semester) { MyAdapter.SelectCommand = new SqlCommand(string.Format("SELECT YearName,Zachetka,Disciplini.Disciplina,Ocenka as ocenka,Primechanie,NazvanieDisciplini as nazvanieDisciplini FROM UspevaemostOcenki INNER JOIN Disciplini on Disciplini.Disciplina=UspevaemostOcenki.Disciplina where zachetka={0} and YEARNAME={1}", Zachetka, Semester), MyConnection); Table_Semester_Students_Ocenki.Clear(); try { MyAdapter.Fill(Table_Semester_Students_Ocenki); } catch { } return Table_Semester_Students_Ocenki; } .... ..... и стоит привязка листбокса и DataView,который получается из запроса LB_Ocenki_Semester.ItemsSource = new DataView(Con.Get_Semester_Students_Ocenki(Students_Semester_Zachetka_Selected.Text.ToString().Trim(), Selected_Semester_Nomer.Text.ToString().Trim())); пока всё без проблем. а позже добавляю кнопку "автоматически обновлять" и пишу обработчик *thread = new Thread(this.Proc); thread.Priority = ThreadPriority.Highest; thread.IsBackground = true; thread.Start();* public void Proc() { while (true) { this.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, (ThreadStart)delegate() { Refresh_Selected_Student_Semester_Info(); }); Thread.Sleep(1500);//для наглядности-ставлю часто } } (Con- это sqlconnection,) public void Refresh_Selected_Student_Semester_Info() { Con.MyAdapter.SelectCommand = new SqlCommand(string.Format("select UspevaemostOcenki.YearName,Disciplini.Disciplina,NazvanieDisciplini, Ocenka,Primechanie,Years.YearName as YN from UspevaemostOcenki inner join Disciplini on UspevaemostOcenki.Disciplina = Disciplini.Disciplina inner join Years on Years.Nomer = UspevaemostOcenki.YearName where zachetka={0} and UspevaemostOcenki.YearName={1}", Students_Semester_Zachetka_Selected.Text.ToString().Trim(), Selected_Semester_Nomer.Text.ToString().Trim()), Con.MyConnection); Con.Table_Semester_Students_Ocenki.Clear(); try { Con.MyAdapter.Fill(Con.Table_Semester_Students_Ocenki); } catch { } } Это всё работает, если на него "не дышать", как только начинаю много клацать-вылетает исключение 
  • close the Con.MyAdapter.Close () connection; - johniek_comp
  • I have a Con-property of type sqlconnection, I open it 1 time and when I create a new query, I appeal to it, not creating a new connection each time and not closing it (I watched it in the video tutorials) and until now everything worked ... although it is necessary to try to close and then open, it just seems to be somehow not rational and a large load ... although it may not be right ... - Rakzin Roman

1 answer 1

Well, first, you need to check about the object reference

 var list = ListBox.Item; if(list != null) { // погнали } 

well, or whatever you have, but about the connection, then it needs to be closed, otherwise with frequent reference there will be an exception

 void a(object sender, EventArgs e) { conn.Open(); // code conn.Close(); } 

and I don’t understand what requests you send for updates, isn’t it easier to add the SelectedChanged event (I don’t remember exactly, like that) then update the second combo box by the identifier ListBox.Text

  • I have selectedchanged and it works and everything works, but if the user selects one item and doesn’t change anything, say 10 minutes, he can continue to work with data that no longer exists, and if the stream is doing the same, an exception may occur. .. maybe! .. if I start often at this time to clap (for fun, what to catch) on the listbox2 then at some point it will pop up ... I don’t understand where it comes from. - Rakzin Roman
  • throw the code, correct it - johniek_comp
  • He does not understand Item - var list = ListBox.Item; and what should be done to add an item? - Rakzin Roman