Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Unable to work with Multi-Threading

Unable to work with Multi-Threading

Scheduled Pinned Locked Moved C#
questionhelp
6 Posts 4 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Bastar Media
    wrote on last edited by
    #1

    Hello there, Please have a look in this code: ERROR LINE IS JUST BELOW #REGION YEARLYTAX... (COMMENTED)

    private void ImportData_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
    #region Delete Previous Data...
    if (MessageBox.Show("Delete all previous records?", "DELETE ALL", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No) == MessageBoxResult.Yes)
    {
    if (MessageBox.Show("ALL RECORDS WILL BE DELETED PERMANENTLY... ARE YOU SURE?", "DELETE ALL", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No) == MessageBoxResult.Yes)
    {
    lblMessage.Content = "Please wait...";
    Cursor = Cursors.Wait;
    Engine E = new Engine();
    SQLiteCommand CMD = new SQLiteCommand("DELETE FROM YEARS", E.Conn);
    CMD.ExecuteNonQuery();
    CMD = new SQLiteCommand("DELETE FROM WARDS", E.Conn);
    CMD.ExecuteNonQuery();
    CMD = new SQLiteCommand("DELETE FROM USERS", E.Conn);
    CMD.ExecuteNonQuery();
    CMD = new SQLiteCommand("DELETE FROM SETTINGS", E.Conn);
    CMD.ExecuteNonQuery();

                    E.Dispose();
                    CMD.Dispose();
                    Cursor = null;
                }
                else goto Import;
            }
            #endregion
    
        Import:
            ButtonsPanel.IsEnabled = false;
            FrmClose.IsEnabled = false;
    
            BW.WorkerSupportsCancellation = false;
            BW.WorkerReportsProgress = true;
            BW.WorkerSupportsCancellation = false;
            BW.DoWork += BW\_DoWork;
            BW.ProgressChanged += BW\_ProgressChanged;
            BW.RunWorkerCompleted += BW\_RunWorkerCompleted;
            BW.RunWorkerAsync();
    
            lblMessage.Content = "Done...";
            ButtonsPanel.IsEnabled = true;
            FrmClose.IsEnabled = true;
        }
        void BW\_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            lblMessage.Content = "Done...";
            Progress.Value = 0;
        }
        void BW\_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            Progress.Value = e.ProgressPercentage;
        }
        void BW\_DoWork(object sender, DoWorkEventArgs e)
        {
            Progress.Value = 0;
    
    OriginalGriffO L P 3 Replies Last reply
    0
    • B Bastar Media

      Hello there, Please have a look in this code: ERROR LINE IS JUST BELOW #REGION YEARLYTAX... (COMMENTED)

      private void ImportData_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
      {
      #region Delete Previous Data...
      if (MessageBox.Show("Delete all previous records?", "DELETE ALL", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No) == MessageBoxResult.Yes)
      {
      if (MessageBox.Show("ALL RECORDS WILL BE DELETED PERMANENTLY... ARE YOU SURE?", "DELETE ALL", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No) == MessageBoxResult.Yes)
      {
      lblMessage.Content = "Please wait...";
      Cursor = Cursors.Wait;
      Engine E = new Engine();
      SQLiteCommand CMD = new SQLiteCommand("DELETE FROM YEARS", E.Conn);
      CMD.ExecuteNonQuery();
      CMD = new SQLiteCommand("DELETE FROM WARDS", E.Conn);
      CMD.ExecuteNonQuery();
      CMD = new SQLiteCommand("DELETE FROM USERS", E.Conn);
      CMD.ExecuteNonQuery();
      CMD = new SQLiteCommand("DELETE FROM SETTINGS", E.Conn);
      CMD.ExecuteNonQuery();

                      E.Dispose();
                      CMD.Dispose();
                      Cursor = null;
                  }
                  else goto Import;
              }
              #endregion
      
          Import:
              ButtonsPanel.IsEnabled = false;
              FrmClose.IsEnabled = false;
      
              BW.WorkerSupportsCancellation = false;
              BW.WorkerReportsProgress = true;
              BW.WorkerSupportsCancellation = false;
              BW.DoWork += BW\_DoWork;
              BW.ProgressChanged += BW\_ProgressChanged;
              BW.RunWorkerCompleted += BW\_RunWorkerCompleted;
              BW.RunWorkerAsync();
      
              lblMessage.Content = "Done...";
              ButtonsPanel.IsEnabled = true;
              FrmClose.IsEnabled = true;
          }
          void BW\_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
          {
              lblMessage.Content = "Done...";
              Progress.Value = 0;
          }
          void BW\_ProgressChanged(object sender, ProgressChangedEventArgs e)
          {
              Progress.Value = e.ProgressPercentage;
          }
          void BW\_DoWork(object sender, DoWorkEventArgs e)
          {
              Progress.Value = 0;
      
      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      That doesn't make a lot of sense:

              var INF = new IniFile(Path.Combine(Directories.OLD\_DATAFOLDER, @"yt.txn")); // Error in this line (Cross-Thread) IniFile is another static class in another file.
      

      If it's a static class, you can't create a variable or an instance of it! So either you will get a couple of error messages telling you that, and your app won;t compile; or it isn't a static class at all, and the problem is in the class constructor.

      Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      B 1 Reply Last reply
      0
      • B Bastar Media

        Hello there, Please have a look in this code: ERROR LINE IS JUST BELOW #REGION YEARLYTAX... (COMMENTED)

        private void ImportData_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
        #region Delete Previous Data...
        if (MessageBox.Show("Delete all previous records?", "DELETE ALL", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No) == MessageBoxResult.Yes)
        {
        if (MessageBox.Show("ALL RECORDS WILL BE DELETED PERMANENTLY... ARE YOU SURE?", "DELETE ALL", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No) == MessageBoxResult.Yes)
        {
        lblMessage.Content = "Please wait...";
        Cursor = Cursors.Wait;
        Engine E = new Engine();
        SQLiteCommand CMD = new SQLiteCommand("DELETE FROM YEARS", E.Conn);
        CMD.ExecuteNonQuery();
        CMD = new SQLiteCommand("DELETE FROM WARDS", E.Conn);
        CMD.ExecuteNonQuery();
        CMD = new SQLiteCommand("DELETE FROM USERS", E.Conn);
        CMD.ExecuteNonQuery();
        CMD = new SQLiteCommand("DELETE FROM SETTINGS", E.Conn);
        CMD.ExecuteNonQuery();

                        E.Dispose();
                        CMD.Dispose();
                        Cursor = null;
                    }
                    else goto Import;
                }
                #endregion
        
            Import:
                ButtonsPanel.IsEnabled = false;
                FrmClose.IsEnabled = false;
        
                BW.WorkerSupportsCancellation = false;
                BW.WorkerReportsProgress = true;
                BW.WorkerSupportsCancellation = false;
                BW.DoWork += BW\_DoWork;
                BW.ProgressChanged += BW\_ProgressChanged;
                BW.RunWorkerCompleted += BW\_RunWorkerCompleted;
                BW.RunWorkerAsync();
        
                lblMessage.Content = "Done...";
                ButtonsPanel.IsEnabled = true;
                FrmClose.IsEnabled = true;
            }
            void BW\_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
            {
                lblMessage.Content = "Done...";
                Progress.Value = 0;
            }
            void BW\_ProgressChanged(object sender, ProgressChangedEventArgs e)
            {
                Progress.Value = e.ProgressPercentage;
            }
            void BW\_DoWork(object sender, DoWorkEventArgs e)
            {
                Progress.Value = 0;
        
        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        It would be much more helpful if you put only the relevant parts of the code, and showed the exact text of the error message.

        1 Reply Last reply
        0
        • B Bastar Media

          Hello there, Please have a look in this code: ERROR LINE IS JUST BELOW #REGION YEARLYTAX... (COMMENTED)

          private void ImportData_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
          {
          #region Delete Previous Data...
          if (MessageBox.Show("Delete all previous records?", "DELETE ALL", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No) == MessageBoxResult.Yes)
          {
          if (MessageBox.Show("ALL RECORDS WILL BE DELETED PERMANENTLY... ARE YOU SURE?", "DELETE ALL", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No) == MessageBoxResult.Yes)
          {
          lblMessage.Content = "Please wait...";
          Cursor = Cursors.Wait;
          Engine E = new Engine();
          SQLiteCommand CMD = new SQLiteCommand("DELETE FROM YEARS", E.Conn);
          CMD.ExecuteNonQuery();
          CMD = new SQLiteCommand("DELETE FROM WARDS", E.Conn);
          CMD.ExecuteNonQuery();
          CMD = new SQLiteCommand("DELETE FROM USERS", E.Conn);
          CMD.ExecuteNonQuery();
          CMD = new SQLiteCommand("DELETE FROM SETTINGS", E.Conn);
          CMD.ExecuteNonQuery();

                          E.Dispose();
                          CMD.Dispose();
                          Cursor = null;
                      }
                      else goto Import;
                  }
                  #endregion
          
              Import:
                  ButtonsPanel.IsEnabled = false;
                  FrmClose.IsEnabled = false;
          
                  BW.WorkerSupportsCancellation = false;
                  BW.WorkerReportsProgress = true;
                  BW.WorkerSupportsCancellation = false;
                  BW.DoWork += BW\_DoWork;
                  BW.ProgressChanged += BW\_ProgressChanged;
                  BW.RunWorkerCompleted += BW\_RunWorkerCompleted;
                  BW.RunWorkerAsync();
          
                  lblMessage.Content = "Done...";
                  ButtonsPanel.IsEnabled = true;
                  FrmClose.IsEnabled = true;
              }
              void BW\_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
              {
                  lblMessage.Content = "Done...";
                  Progress.Value = 0;
              }
              void BW\_ProgressChanged(object sender, ProgressChangedEventArgs e)
              {
                  Progress.Value = e.ProgressPercentage;
              }
              void BW\_DoWork(object sender, DoWorkEventArgs e)
              {
                  Progress.Value = 0;
          
          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #4

          And what error do you get? I have the suspicion that you're trying to access UI controls from this thread, but as you haven't told us what the exception is, we cannot say for certain.

          1 Reply Last reply
          0
          • OriginalGriffO OriginalGriff

            That doesn't make a lot of sense:

                    var INF = new IniFile(Path.Combine(Directories.OLD\_DATAFOLDER, @"yt.txn")); // Error in this line (Cross-Thread) IniFile is another static class in another file.
            

            If it's a static class, you can't create a variable or an instance of it! So either you will get a couple of error messages telling you that, and your app won;t compile; or it isn't a static class at all, and the problem is in the class constructor.

            Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

            B Offline
            B Offline
            Bastar Media
            wrote on last edited by
            #5

            Hello Friend, sorry for my long code and IniFile is not static. moreover, IniFile is not static. Here is the exception report: System.InvalidOperationException was unhandled by user code. Additional information: The calling thread cannot access this object because a different thread owns it. And the line where I am getting the code:

            var INF = new IniFile(Path.Combine(Directories.OLD_DATAFOLDER, @"yt.txn"));

            OriginalGriffO 1 Reply Last reply
            0
            • B Bastar Media

              Hello Friend, sorry for my long code and IniFile is not static. moreover, IniFile is not static. Here is the exception report: System.InvalidOperationException was unhandled by user code. Additional information: The calling thread cannot access this object because a different thread owns it. And the line where I am getting the code:

              var INF = new IniFile(Path.Combine(Directories.OLD_DATAFOLDER, @"yt.txn"));

              OriginalGriffO Offline
              OriginalGriffO Offline
              OriginalGriff
              wrote on last edited by
              #6

              So look at the line, and there are two possible parts:

              Directories.OLD_DATAFOLDER

              Which looks like an enum, but we can't see it. If it's an enum then its won't be that.

              new IniFile(...);

              Which means it's in the constructor, which we can't see. You've got a cross threading error in code we can't see which probably shouldn't be using threads and the fragment you show use doesn't use threading anyway. That doesn't exactly make it easy for us to work out what your problem might be...:laugh:

              Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
              "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups