Unable to work with Multi-Threading
-
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;
-
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;
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...
-
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;
-
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;
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.
-
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...
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"));
-
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"));
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 anenum
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...