18

I'm completely stuck. I'm testing MetaTrader API and getting next error when tries to run a method in the Immediate Window of VS 2010:

A first chance exception of type 'System.Threading.ThreadAbortException' occurred in mscorlib.dll

A first chance exception of type 'System.Threading.ThreadAbortException' occurred in System.Runtime.Remoting.dll

Evaluation requires a thread to run temporarily. Use the Watch window to perform the evaluation.

What does it mean? Can it happens because of runtime versions difference (api 2.0, app 4.0)?

Community
  • 1
  • 1
abatishchev
  • 98,240
  • 88
  • 296
  • 433

4 Answers4

7

I believe the method you are calling through the Immediate Window ends up calling Debugger.NotifyOfCrossThreadDependency. This method was only introduced in .NET 4.0, so it makes sense that the problem won't reproduce itself when using an older version of the runtime. This blog post explains NotifyOfCrossThreadDependency in detail, but the gist of it is that it causes the Watch window to show a Refresh button which must be pressed before the evaluation occurs. If it is evaluated through the Immediate Window, though, you get the "Evaluation requires a thread to run temporarily. Use the Watch window to perform the evaluation" error.

Here's an example property that reproduces this error:

    public int CauseError
    {
        get 
        {                
            Debugger.NotifyOfCrossThreadDependency();
            return 5;
        }
    }
Arturo Torres Sánchez
  • 2,751
  • 4
  • 20
  • 33
Omer Raviv
  • 11,409
  • 5
  • 43
  • 82
  • Following the advice in the error, I copy the method I'm calling in the Immediate Window to the Watch window, But then I get a dialog saying: "To prevent an unsafe abort when evaluating the function 'OpenQA.Selenium.Remote.RemoteWebDriver.FindElementByClassName' all threads were allowed to run. This may have changed the state of the process and any breakpoints encountered have been skipped.". Then I see this in the Watch window: "The function evaluation requires all threads to run.". Am I doing something wrong or the error message in question is misleading? How do I evaluate this method call? – Cagin Uludamar Aug 22 '21 at 08:35
6

I believe that error means that the method you are trying to execute is spawning a thread. However, since the program is in Break mode, it can't run. To avoid a deadlock (where the method will wait forever for a thread that won't run), Visual Studio kills any spawned threads.

My suggestion is to move the call into the program, and use some other means to execute it.

Mike Caron
  • 14,351
  • 4
  • 49
  • 77
  • Sounds promising, but I did a test and found: this occurs only if app is targeted 4.0, nothing like that in case of 2.0/3.5! How can it be? Different thread/thread debug model? – abatishchev Nov 26 '10 at 06:58
  • Could be. Honestly, I have very little knowledge of how VS debugs the .NET runtime. Maybe this threading situation was permitted in 3.5, but for whatever reason was deemed dangerous in 4.0? *shrug* Sorry I can't be more helpful... – Mike Caron Nov 26 '10 at 11:25
  • Thanks anyway for interesting assumption and suggestion! :) – abatishchev Nov 26 '10 at 20:12
  • 1
    @MikeCaron I don't think that's the case: If I evaluate `public int StartNewThread { get { new Thread(new ThreadStart(() => Console.WriteLine())).Start(); return 5; } }` through the Immediate Window, it works fine without giving this error. – Omer Raviv Dec 08 '13 at 13:38
0

That's because the server is running under .NET 2.0 and a client (thru .NET Remoting) - under .NET 4.0.

Switching client to .NET 2.0/3.5 fixed the problem.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
-2

do not remove the app.config which will contain information like follows:

<configuration>
  <configSections>
    <sectionGroup name="userSettings" 
                  type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
      <section 
               name="MySolution.Properties.Settings"
               type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" 
               allowExeDefinition="MachineToLocalUser" 
               requirePermission="false" />
    </sectionGroup>
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
  </startup>
</configuration>
Kevin
  • 1
  • 1