c#-4.0

Task.ContinueWith() parent task doesn't wait for child task to finish [duplicate]

隐身守侯 提交于 2020-01-13 10:12:11
问题 This question already has answers here : Task.WaitAll doesn't wait for child task? (2 answers) Closed 5 years ago . Since I was understanding the Task in context of nested task, I really don't understand that- Why the 3rd print before 2nd print? Even though, I have used Task.WaitAll(t) , it print 3rd line before 2nd line. Code: public static void Main() { Task t = new Task( () => { Thread.Sleep(2000); Console.WriteLine("1st print..."); }); t.ContinueWith( x => { Thread.Sleep(2000); Console

How to select data into List<T> instead of DataTable?

心不动则不痛 提交于 2020-01-13 09:57:06
问题 This is how currently I'm selecting data from database: public DataTable GetData() { DataTable table = new DataTable("Table"); using (SqlConnection connection = new SqlConnection("Connection string")) { SqlCommand command = new SqlCommand(); command.Connection = connection; command.CommandType = System.Data.CommandType.Text; command.CommandText = "query string"; connection.Open(); SqlDataAdapter adapter = new SqlDataAdapter(command); adapter.Fill(table); } return table; } But it return

Contract class should be an abstract class

本秂侑毒 提交于 2020-01-13 08:57:09
问题 The following code gives me the warning Contract class 'FooContracts' should be an abstract class . From all the examples I've read online (e.g. http://www.infoq.com/articles/code-contracts-csharp), this should work (presumably without compiler warnings). [ContractClass(typeof(FooContracts))] public interface IFoo { void Bar(string foo); } [ContractClassFor(typeof(IFoo))] internal sealed class FooContracts : IFoo { void IFoo.Bar(string foo) { Contract.Requires(foo != null); } } I'm in Visual

C# for loop increment by 2 trouble

一笑奈何 提交于 2020-01-13 08:01:36
问题 This algorithm is about to store strings from Array A to Array B by storing "A", "B" to Index 8 and Index 9 I really initiate to make the array size of B to be 10 because later I will put some other things there. My partial code: string[] A = new string[]{"A","B"} string[] B = new string[10]; int count; for(count = 0; count < A.length; count++) { B[count] = A[count] } 回答1: So you want to increment every index with 2: string[] A = new string[] { "A", "B", "C", "D" }; string[] B = new string[A

C# for loop increment by 2 trouble

拥有回忆 提交于 2020-01-13 08:01:29
问题 This algorithm is about to store strings from Array A to Array B by storing "A", "B" to Index 8 and Index 9 I really initiate to make the array size of B to be 10 because later I will put some other things there. My partial code: string[] A = new string[]{"A","B"} string[] B = new string[10]; int count; for(count = 0; count < A.length; count++) { B[count] = A[count] } 回答1: So you want to increment every index with 2: string[] A = new string[] { "A", "B", "C", "D" }; string[] B = new string[A

The difference between GotFocus and GotKeyboardFocus

一世执手 提交于 2020-01-13 07:57:13
问题 What is the difference(s) between GotFocus and GotKeyboardFocus -and similarly LostFocus and LostKeyboardFocus ? Sorry for the simple question, but, I googled it and read a lot of blog posts, but I'm still confused. It seems nobody knows exactly what is the difference ): UPDATE: My usage: I am creating a custom control by extending Control class. Something like ComboBox but with some other effects. I'm trying to open and close a Popup by setting a property: IsDropDownOpen just like a ComboBox

How RaisePropertyChanged<T> finds out the property name?

。_饼干妹妹 提交于 2020-01-12 13:56:31
问题 There is one overload of this method in NotificationObject :- protected void RaisePropertyChanged<T>(Expression<Func<T>> propertyExpression); We write in the following way in the setter of property: RaisePropertyChanged(() => PropertyVariable); How does it works ? How it finds the property name out of this Lambda expression ? 回答1: An Expression<TDelegate> represents the abstract syntax tree of the lambda expression. So you just have to analyze this syntax tree to find out the property name:

How to empty a BlockingCollection

≡放荡痞女 提交于 2020-01-12 13:34:10
问题 I have a thread adding items to a BlockingCollection . On another thread I am using foreach (var item in myCollection.GetConsumingEnumerable()) If there is a problem I want to break out of my foreach and my method and clear whatever is left in the BlockingCollection however I can't find a way to do it. Any ideas? 回答1: Possibly use the overload of GetConsumingEnumerable that takes a CancellationToken ; then, if anything goes wrong from the producing side, it can cancel the consumer. 回答2: I'm

What's the best way to structure this Linq-to-Events Drag & Drop code?

南笙酒味 提交于 2020-01-12 08:39:33
问题 I am trying to handle a drag & drop interaction, which involves mouse down, mouse move, and mouse up. Here is a simplified repro of my solution that: on mouse down, creates an ellipse and adds it to a canvas on mouse move, repositions the ellipse to follow the mouse on mouse up, changes the colour of the canvas so that it's obvious which one you're dragging. var mouseDown = Observable.FromEvent<MouseButtonEventArgs>(canvas, "MouseLeftButtonDown"); var mouseUp = Observable.FromEvent

Reading Form Data in ActionFilterAttribute

两盒软妹~` 提交于 2020-01-12 08:25:35
问题 I created an ActionFilterAttribute for my web api in order to authorize people. Getting accessToken by RequestUri is okay, however i want to send it in form data. While reading Request.Content in onActionExecuting method of ActionFilterAttribute, server always has an empty result. How can i solve this problem? The code is as like as below: public class RequireAuthorization : ActionFilterAttribute { public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext