开发者

Program runs differently when I step into and when I setp over a function in debug mode

开发者 https://www.devze.com 2023-01-12 15:46 出处:网络
I have a function GetAlertData() that returns a Datatable. I invoke it as: var dt = GetAlertData() Behavior in debug mode:

I have a function GetAlertData() that returns a Datatable. I invoke it as:

var dt = GetAlertData()

Behavior in debug mode:

Case 1: When I do F11 all the time and go into the GetAlertData function, all works well and I get the correct table

Case 2: When I do F10 on this function and step over it, GetAlertData returns a table with all values filled as zero (incorrect). (Columns of my table are all float datatype)

In release mode, behavior is same as pressing F10 in debug mode, i.e again I get all zeros.

Any ideas on what could be the reason, or what I can try to find the reason? Thanks..

Edit: my GetAlertData function is something like this..

internal static DataSet GetAlertData()
        {
            using (var sqlConnection = new SqlConnection(Constants.ConnectionString))
            {
                const string sproc = @"[spo_GetAlertData]";
                var cmd = new SqlCommand(sproc, sqlConnection) {CommandType = CommandType.StoredProcedure};

                cmd.Parameters.Add("@TimeWindow", SqlDbType.Int);
                cmd.Parameters["@TimeWindow"].Value =2
                cmd.Parameters.Add("@ThresholdTime", SqlDbType.Int);
                cmd.Parameters["@ThresholdTime"].Value = 2
                var dsAnalysis = new DataSet();
                var da = new SqlDataAdapter(cmd);
                da.Fill(dsAnalysis);
                if (dsAnalysis.Tables.Count > 0 && dsAnalysis.Tables[0开发者_如何学JAVA].Rows.Count > 0)
                    return dsAnalysis;
                return null;
            }
        }


One thing to consider is the difference in execution time using F11 and F10 (stepping into and stepping over methods respectively). F11 steps into a function therefore keeping you in that thread of logic longer than F10 which steps over code allowing it to execute at full speed.

The point is that you could very well have a timing/threading issue that is alleviated when the application has more processing time caused by the fact that you're taking more time to step through and into the code with F11. That's why the release more behavior matches the F10 behavior, faster execution.

I'm guessing that sprinkling something like Thread.Sleep(250) around the problem area would help too but I do not recommend this. It's an action of absolutely last resort best used to just test the timing hypothesis. You need to figure out what is running concurrently that might be causing this.


Without seeing the source code for GetAlertData, I can only guess that you have set up some watch variables that access a property or something with side-effects that change the outcome. The watches only come into scope when you step into the GetAlertData method.


The most likely problem here is that you have a property or .ToString which has side effects that is being evaluated in the autos / locals / watch window when you step. In the F11 case this property is put in one of those windows, evaluates and it's side effect causes the scenario to work. In the F10 scenario it doesn't happen and the scenario fails.

You can test this easily by disabling implicit function evaluation.

  • Tools -> Options
  • Debugger
  • Uncheck "Enable implicit property and calls" checkbox
  • Rerun your scenario
0

精彩评论

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

关注公众号