开发者

"Unable to evaluate expression because the code is optimized" when calling method from some thread

开发者 https://www.devze.com 2023-01-13 10:00 出处:网络
I encounter a strage behavior in my application. I have a function: Load with try-catch blocks in class Task. When I\'m calling it from the main thread: Task.Load() it runs OK.

I encounter a strage behavior in my application. I have a function: Load with try-catch blocks in class Task. When I'm calling it from the main thread: Task.Load() it runs OK.

I created a thread which runs some method. when I call Task.Load() from the thread method I receive that message: "Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack".

Can someone point out what is the problem?

Thanks in advance.

Maya


Thank you for your responses.

Steve, I'm using debug build. I think that the problem relates to your first notion, but I don't know how to handle it. Here is sample code, maybe it will help. The sample contains Task class with Load method, and 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...
    }
  }
}

The "Unable to evaluate..." exeception occures in the Load() method each time in other place. This method works fine when calling it from 开发者_如何学Cthe main thread. (I omitted from the code error handling...)

Sorry for the long post.

Thanks again,

Maya


This is possibly a matter of VS debugger context. When you run inside main(), the context is clearcut - you are in the process's main/only thread. When you spawn a new thread, you have to ensure you are actually in that thread before observing data that is local to the thread/code of interest. Put a breakpoint in your Load() function to ensure you are actually in the correct thread before observing the data of interest.

Another possibility is (as the message says) you are debugging a Release build with optimization on, and the vars cannot be viewed. Try this using your Debug build first.

0

精彩评论

暂无评论...
验证码 换一张
取 消