none
Using Thread problem RRS feed

  • Question

  • hello everyone.

    i have a problem and my problem is when i want to use this method to add data in database my ui is going to freeze 

    my method :

    namespace DataBaseLibraryTest1
    {
        public class SetAndGetDataClass
        {

            private string UserNameIK;
            private string PasswordIK;
            private string FirstNameIK;
            private string LastNameIK;
            private string LogicalIK;
            private string PersonalCodeIK;

            testDataBase1Entities ENTest = new testDataBase1Entities();
            UsersTestTable usertabledata = new UsersTestTable();

            public void SetDataInDB(string UserNameGT,string PasswordGT,string FirstNameGT,string LastNameGT,string LogicalProcessGT,string personalCodeGT)
            {
                UserNameIK = UserNameGT;
                PasswordIK = PasswordGT;
                FirstNameIK = FirstNameGT;
                LastNameIK = LastNameGT;
                LogicalIK = LogicalProcessGT;
                PersonalCodeIK = personalCodeGT;

                usertabledata.FirstName = UserNameIK;
                usertabledata.LastName = LastNameIK;
                usertabledata.UserName = UserNameIK;
                usertabledata.Password = PasswordIK;
                usertabledata.LogicalProcess1 = LogicalIK;
                usertabledata.PicturesDb = null;
                usertabledata.PersonalCode = PersonalCodeIK;
                ENTest.UsersTestTables.Add(usertabledata);
                ENTest.SaveChanges();
            }

    }

    }

    and my button event handler :

            private void AddBtn_Click(object sender, RoutedEventArgs e)
            {
                MyClass.SetDataInDB(UserNameTXTbox.Text, Passwordtxtbox.Text, FirstNameTxtBox.Text, LastNmaeTxtbox.Text, LogicalTxtBox.Text, PersonalCode.Text);
            }

    my question is how can i use threads for this problem.

    i search a lot about this problem but i dont know how do this.

    could you help me with this problem ?

    i use this codes for this problem and i dont know how they work :

            private void AddBtn_Click(object sender, RoutedEventArgs e)
            {
                Thread mythread = new Thread(new ThreadStart(setdataindbdb));
                mythread.Start();
            }

            private void setdataindbdb()
            {
                this.Dispatcher.Invoke(() =>
                {
                    MyClass.SetDataInDB(UserNameTXTbox.Text, Passwordtxtbox.Text, FirstNameTxtBox.Text, LastNmaeTxtbox.Text, LogicalTxtBox.Text, PersonalCode.Text);
                });
            }

    • Edited by IKING251 Sunday, August 11, 2019 4:04 PM
    Sunday, August 11, 2019 3:48 PM

Answers

All replies

  • Use the BackgroundWorker Class. It was created for this. There are abundant articles and samples about it.



    Sam Hobbs
    SimpleSamples.Info

    • Marked as answer by IKING251 Monday, August 12, 2019 7:52 AM
    Sunday, August 11, 2019 5:52 PM
  • my project is a wpf project not a windowsform application.

    Sunday, August 11, 2019 6:53 PM
  • Then it should be in the WPF forum. Note that you did not say WPF. You need to state those things explicitly except if this question were in the WPF forum then the implication is good.

    BackgroundWorker is not in the Forms library. It works for WPF too; did you look? I did not ask if you are using Windows Forms or WPF because it is not relevant.



    Sam Hobbs
    SimpleSamples.Info

    Sunday, August 11, 2019 8:26 PM