137

I'm suddenly getting a strange error while debugging. Up to now the variable in the watch windows has been showing correctly. Now I am always getting this error message in the watch windows:

The function evaluation requires all threads to run

I am not able to check any variable anymore. I am not explicitly working with threads. What can I do to get it working again?

I already disabled, as mentioned in some forums, the function: "Enable property Evaluation and other implicit function Calls" in the option window of the debugger. But without success, and it gives me this error:

Error Implicit Function evaluation disabled by the user

David Klempfner
  • 8,700
  • 20
  • 73
  • 153
Maik
  • 1,582
  • 3
  • 12
  • 13
  • 1
    To get that point of the list: Have you restarted Visual Studio? – MUG4N May 01 '15 at 10:22
  • 1
    Yes, I did. Restarted and same issue. – Maik May 01 '15 at 10:24
  • 1
    check this out: http://stackoverflow.com/questions/4280604/evaluation-requires-a-thread-to-run-temporarily-use-the-watch-window-to-perform – MUG4N May 01 '15 at 10:25
  • 1
    Even if it would work, this can't be the solution, as I want to use the NET 4.x Framework. I do not want to downgrade just because if this issue. I am wondering why it was working some time ago. – Maik May 01 '15 at 10:35
  • 1
    i have the same issue. VS2013 had a button you could click, but VS2015 does not have this button. – Spongman Oct 21 '15 at 18:49

8 Answers8

157

From the msdn forum:

This isn't an error in and of itself, but more of a feature of your debugger.

Some properties require code to be executed in order for the property to be read, but if this requires cross-thread interaction, then other threads may have to run as well. The debugger doesn't do this automatically, but certainly can, with your permission.

Just click the little evaluate icon and it will run your code and evaluate the property.

enter image description here

For further details on this behaviour check this excellent article:
Why do we get “The function evaluation requires all threads to run” | Microsoft Learn

Richard Deeming
  • 29,830
  • 10
  • 79
  • 151
MUG4N
  • 19,377
  • 11
  • 56
  • 83
  • 17
    I did read this article. I have no such a button to click, So not exactly the issue i have. Strange enough is, that it was working since I have upgraded to Visual Studio 2015 RC today. – Maik May 01 '15 at 12:21
  • 1
    Even that small snipped is not working anymore, when trying to get the result value with help of the watch window. public IEnumerable MappingQuery() { var context = new DatabaseEntities(); context.Database.Connection.ConnectionString = GlobalData.ConnectionString; var Results = from a in context.Mapping select new clsDefinitions.Mapping { value1= a.value1, value2 = a.value2 }; return Results; – Maik May 02 '15 at 10:32
  • 2
    Same issue here: http://stackoverflow.com/questions/4460206/lazyt-the-function-evaluation-requires-all-threads-to-run – MUG4N May 03 '15 at 09:56
  • 1
    I just clicked on *"the little evaluate icon"* and it worked. *visual studio 2017* – Ahmad Maleki May 27 '18 at 10:07
  • 11
    If you don't see any icon, try modify the variable/command to execute the query from the watch window, instead of using drop down to explore it's properties. For example, adding `.ToList()` or `.Any()`. – Hp93 Jul 16 '18 at 17:52
  • 12
    I'm not sure why but calling .ToList() on my query fixed the issue – J.Kirk. Jul 24 '18 at 18:52
  • 2
    @J.Kirk. I found the same thing - thanks! I had been using `var` and `IEnumerable` and just assigning `db.AGENCY_TABLE.OrderBy(e => e.Name);` - but once I used `var` with `.ToList()` (or `List` with `.ToList()` also works), it reveals the result! – vapcguy Sep 05 '18 at 20:44
  • 2
    I have speant years just making temperary variables and all sorts of bs because I didn't know about that little button. – Dylan Jan 31 '20 at 02:59
  • 4
    Link on article is dead. – Hrvoje May 27 '22 at 07:52
  • 2
    For some reason, MS decided not to redirect the original blog links to the correct archived links. The corrected link for the article is: [Why do we get “The function evaluation requires all threads to run” | Microsoft Learn](https://learn.microsoft.com/en-us/archive/blogs/eliofek/why-do-we-get-the-function-evaluation-requires-all-threads-to-run) – Richard Deeming Mar 07 '23 at 10:17
39

I ran into this issue when just trying to get items from a table called "AGENCY" using Entity Framework:

var agencies = db.AGENCY.OrderBy(e => e.FULLNAME);

enter image description here

Hovering over agencies in debug mode, clicking to expand the options, and clicking Results would give the dreaded "The function evaluation requires all threads to run" with a "Do Not Enter" icon at the end that, on which, clicking did nothing.

2 possible solutions:

  1. Add .ToList() at the end:

    var agencies = db.AGENCY_TABLE.OrderBy(e => e.FULLNAME).ToList();

    List<AGENCY_TABLE> agencies = db.AGENCY_TABLE.OrderBy(e => e.FULLNAME).ToList();

    Credit goes to Hp93 for helping me come to this solution. In the comments on MUG4N's answer where I found this solution, it also mentions trying .Any() instead of .ToList(), but this gives a Boolean instead of a <T>, like <AGENCY> is, so it probably wouldn't help.

  2. Workaround - try a different path in the debug options. I found that I could click on the "Non-Public Members" > "_internalQuery" > ObjectQuery > Results View and get my values that way.

enter image description here

vapcguy
  • 7,097
  • 1
  • 56
  • 52
12

MUG4N has indeed provided a correct answer however if you hover over the line of code in debug, you may be looking at something like the below. If so, click the little re-evaluate icon highlighted in the image below...

enter image description here

NB: I obtained this image by pinning, normally the re-evaluate icone are in the middle of the window and not down the left hand column.

Ewan
  • 541
  • 8
  • 23
3

You should make thread safe call because accessing Windows form controls are not Thread safe in multithreading. This is my simple code which makes Thread safe call and sets Progress bar.

public partial class Form1 : Form
{// This delegate enables asynchronous calls for setting  
    // the text property on a TextBox control.  
    delegate void StringArgReturningVoidDelegate(string text);
    private Thread demoThread = null;

    public int Progresscount = 0;
    static EventWaitHandle waithandler = new AutoResetEvent(false);
    public Form1()
    {
        InitializeComponent();
    }
    public static bool CheckForInternetConnection()
    {
        try
        {


            using (var client = new WebClient())
            {
                using (var stream = client.OpenRead("http://www.google.com"))
                {
                    return true;
                }
            }
        }
        catch
        {
            return false;
        }
    }

    public  void Progressincrement()
    {

        waithandler.WaitOne();
        while (CheckForInternetConnection()==true)
        {
            if (Progresscount==100)

            {
                break;
            }
            SetLabel("Connected");
            Progresscount += 1;

       SetProgress(Progresscount.ToString());
            Thread.Sleep(TimeSpan.FromSeconds(1));
        }
        if (Progresscount <100)
        {
            Startthread();
        }
        SetLabel("Completed");


    }

  public  void Startthread ()
        {

   this.demoThread=   new Thread(new ThreadStart(Progressincrement));
        this.demoThread.Start();
     SetLabel("Waiting for connection");
        while (CheckForInternetConnection() == false) ;

        waithandler.Set();
    }
    private void SetLabel(string text)
    {
        // InvokeRequired required compares the thread ID of the  
        // calling thread to the thread ID of the creating thread.  
        // If these threads are different, it returns true.  
        if (this.label1.InvokeRequired)
        {
            StringArgReturningVoidDelegate d = new StringArgReturningVoidDelegate(SetLabel);
            this.Invoke(d, new object[] { text });
        }
        else
        {
            this.label1.Text = text;
        }
    }
    private void SetProgress(string Value)
    {
        // InvokeRequired required compares the thread ID of the  
        // calling thread to the thread ID of the creating thread.  
        // If these threads are different, it returns true.  
        if (this.progressBar1.InvokeRequired)
        {
            StringArgReturningVoidDelegate d = new StringArgReturningVoidDelegate(SetProgress);
            this.Invoke(d, new object[] {Value});
        }
        else
        {
            this.progressBar1.Value = Convert.ToInt32(Value);
        }
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        Startthread();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        MessageBox.Show("Responsive");
    }
}

For more information MSDN

TAHA SULTAN TEMURI
  • 4,031
  • 2
  • 40
  • 66
3

This isn't an error, but more of a feature of your debugger.
The debugger doesn't do this automatically, but certainly can, with users permission. Just click the little space icon and it will run the code and evaluate the property.

Check below screenshot for reference

Yehor Androsov
  • 4,885
  • 2
  • 23
  • 40
Rohit
  • 71
  • 2
  • 1
    But this wasn't always the behavior and is a little annoying to have to keep down this on each item I want to evaluate. Having an option to do this automatically would be nice to have and provide the opportunities to re-instate previous behavior. – user3502865 Feb 15 '21 at 02:05
2

I use the next workaround to pass:

var OtherThreadField = "";
Invoke(new MethodInvoker(delegate
                    {
                        OtherThreadField = ExecuteNeededMEthod();
                    }));

Now i have a value for OtherThreadField.

sh2dow
  • 21
  • 4
1

I faced the same issue and solved .The Issue arise due to username and password ,in SQL connection there is user and password but in code there no user and password. so I enable the user and the password and the issue solved

Jitu
  • 11
  • 1
  • 1
    You should perhaps ask a few questions before presuming this is the same? Could very well be that you've encountered similar things relating to mssql usage, but there is nothing relating to rdbms usage in the question and the problem you've apparently solved arose from what @mug4n explains. So it can be that if the why in the question, your solution is even more helpful, but try to make sure it is at least related through comments first. Or it would be like 'i feel like i want something' answered with 'buy some tea' ... now one could get lucky and it's a perfect answer, still ... :) – T. Nielsen Jul 29 '22 at 08:46
1

For me, this happened when trying to break on a line that accesses a complex object instance contained by a Settings Class.

A breakpoint on the following if results in Settings.Default.FindSettings with the value being "The function evaluation requires all threads to run." If I press the force eval button, it is null. Stepping with the force eval button click or not enters the if block and initializes the object. If I remove the breakpoint and add a new breakpoint following the if block, the Settings.Default.FindSettings deserializes properly with the expected values.

if (Settings.Default.FindSettings == null)
{
    Settings.Default.FindSettings = new FindSettings();
}

After trial and error, I added the following code before the above if block to access the settings prior to breaking. This seems to reliably fix the problem. I do not need it in production so I wrap in conditional compiler directive. I have a comment in the code instead of a non-descript discard:

#if DEBUG
    var _ = Settings.Default.FindSettings;
#endif

I am not sure if the above line would be optimized out in production since it has side effects. As I only need it while debugging, I have not checked.

pigeon
  • 151
  • 9