0

I'm currently making improvements to my application and I'm moving the heavy piece of the application that does a lot of manual work to a different thread via Task.Run

var myTask = Task.Run(() => test()).ContinueWith((Finish) =>
{...}

test() is the testing method that will be renamed of course, once this is working.

inside the test() function I access Properties.Settings.Default.A and that works just fine and few lines below I have an if statement where Properties.Settings.Default.B is compared and I end up with:

"The calling thread cannot access this object because a different thread owns it."

I even tried making that an input parameter and then the input variable also gives the same error.

void test()
    {
        timer.Start();

        SRCH_Status.Dispatcher.BeginInvoke((Action)(() => SRCH_Status.Content = "Retrieving information . . ."));
        SRCH_Progress.Dispatcher.BeginInvoke((Action)(() => SRCH_Progress.Content = "Scanning in progress"));

        if (Properties.Settings.Default.A == 0)      
        ....


       if (Properties.Settings.Default.B> 1) //ERROR HERE
        {...}
...}

What am I doing wrong here?!

Here is the stackTrace property of the exception:

at System.Windows.Threading.Dispatcher.VerifyAccess()
at System.Windows.DependencyObject.ClearValue(DependencyPropertyKey key)
at System.Windows.Controls.ItemCollection.Clear()
at APP.Main.test() in c:\....\Main.xaml.cs:line 718
at APP.Main.<MENU_Start_Click>b__3() in c:\...\Main.xaml.cs:line 187
at System.Threading.Tasks.Task.InnerInvoke()
at System.Threading.Tasks.Task.Execute()
DethoRhyne
  • 930
  • 1
  • 9
  • 29
  • Is your Properties, Settings or Default associated with anything UI, or created in the UI/dispatch thread? – Joe May 11 '16 at 12:18
  • could you show implementation of `Properties.Settings.Default.B`? – StepUp May 11 '16 at 12:26
  • You probably misidentified the statement that threw the exception. You need to show us the exception's StackTrace property. – Hans Passant May 11 '16 at 12:37
  • @StepUp B is integer representation of an enum that's related to a custom dropdown menu.. In dropdown you select 1,2,3, that's stored as Level.One,Two,Three and B contains 1 2 or 3 depending on the use. On load it loads from settings to the usercontrol. But this same thing is happening with the A setting as well. A and B are used in identical places, just on 2 different instances of a user control. – DethoRhyne May 11 '16 at 13:17
  • @HansPassant I have added the stacktrace. – DethoRhyne May 11 '16 at 13:27
  • Right, nothing to do with Properties.Settings. You omitted the bad code from your snippet. – Hans Passant May 11 '16 at 13:32
  • @HansPassant No sir, between A and B are just 3 .Clear() calls on lists. and last one is a UI element... wait a second, could that be the cause?. – DethoRhyne May 11 '16 at 15:08
  • 2
    I heard that slap to the forehead from three thousand miles away. – Hans Passant May 11 '16 at 15:14
  • @HansPassant Hahaha, but yep. this issue is fixed :) You can post an answer if you want. I just replaced element.items.clear() with element.Dispatcher.BeginInvoke((action) => elements.Items.Clear()); and it worked. – DethoRhyne May 11 '16 at 15:20

0 Answers0