I
I
IartanisI2019-06-20 14:37:05
WPF
IartanisI, 2019-06-20 14:37:05

How to make event in wpf asynchronous?

I have this kind of code. The function itself

static public List<string> GetData(string NameBase,string Row,string type = "0")
            List<string> data =  new List<string>();
            
            SQLiteConnection conn = new SQLiteConnection(@"Data Source=Zhur-Test.db"); ;
            conn.Open();


            
            string IdBases = "FullBases";
            string sql1 = "select Id,NameTable from ListTable where FullName = '" + NameBase + "'";
            SQLiteCommand cmd1 = new SQLiteCommand(sql1, conn);
            SQLiteDataReader reader1 = cmd1.ExecuteReader();
            while (reader1.Read())
            {
               
                IdBases = Convert.ToString(reader1["NameTable"]);
            }

            string sql = "select distinct " + Row + " from " + IdBases;





            SQLiteCommand cmd = new SQLiteCommand(sql, conn);

            SQLiteDataReader reader = cmd.ExecuteReader();

            while (reader.Read())
            {
                if (type == "0")
                {
                    data.Add(Convert.ToString(reader[Row]));
                }
                else
                {

                    var b1 = Regex.Split(Convert.ToString(reader[Row]), ";");
                    for (int i = 0; i < b1.Length; i++)
                    {
                        data.Add(Convert.ToString(b1[i]));
                    }
                }
            }
            conn.Close();
            return data;
        }

    }

And the reaction when changing the value of the listbox
private void OuterListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            object item = ListJournal.SelectedItem;

            if (item == null)
            {
            }
            else
            {
                ListJournal.Text = item.ToString();
                NameVicon1.ItemsSource = Func.GetData(ListJournal.Text, "NameVicon1", "1").Distinct();
                Koresp.ItemsSource = Func.GetData(ListJournal.Text, "Koresp").Distinct();
                Resolution.ItemsSource = Func.GetData(ListJournal.Text, "Resolution").Distinct();
                KrtZmist.ItemsSource =  Func.GetData(ListJournal.Text, "KrtZmist").Distinct();
                NomerStr.ItemsSource = Func.GetData(ListJournal.Text, "NomerStr","1").Distinct();
            }
        }

The bottom line is that the database needs quite a lot of time to load the data for autocompletion. And I want to add a process to the background. How can it be implemented?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
C
cyber_roach, 2019-06-20
@cyber_roach

Did the standard methods work?
https://docs.microsoft.com/en-us/dotnet/csharp/pro...

F
freeExec, 2019-06-20
@freeExec

If SQLiteCommandit does not know how to use asynchronous reading methods (which is very strange), then wrap this logic in Task.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question