"Cannot evaluate expression because code is optimized" when calling a method from some thread

In my application, I encounter uneven behavior. I have a function: Load try-catch blocks into the Task class. When I call it from the main thread: Task.Load () works fine.

I created a thread that runs some method. when I call Task.Load () from the thread method, I get this message: "The expression cannot be evaluated because the code is optimized, or the native frame is on top of the call stack."

Can someone point out what the problem is?

Thanks in advance.

Mayan


Thank you for your responses.

Steve, I'm using a debug build. I think the problem is with your first concept, but I don’t know how to deal with it. Here is a sample code, maybe this will help. The sample contains the Task class with the Load method and the Dispatcher class, which starts a new thread from which the Load method is called:

public class Task{
  public long Id{get; set;}
  public string Name{get; set;}

  public void Load(DataRow row){
    try{
      Id = (long)row["id"];
      Name = row["name"].ToString();
    }
    catch(..){}
  }
}

public class Dispatcher{
  Thread dispatchingThread = new Thread(getTasks);

  private getTasks(){
    DataTable dt = DAL.GetPendingTasks();
    foreach(DataRow row in dt.Rows){
      Task task = new Task
      task.Load(row);
      //process task...
    }
  }
}

"Unable to evaluate ..." an exeception occurs in the Load () method each time in a different place. This method works great when calling it from the main thread. (I lost the error handling code ...)

Sorry for the long post.

Thanks again,

Mayan

+3
source share
1 answer

, , VS. main(), clearcut - main/only thread. , , , , /. Load(), , , , .

( ), Release , . Debug.

+2

Source: https://habr.com/ru/post/1762249/


All Articles