Click here to Skip to main content
15,888,984 members
Home / Discussions / C#
   

C#

 
GeneralRe: Deleting Read Bytes Pin
computerpublic11-May-14 5:58
computerpublic11-May-14 5:58 
GeneralRe: Deleting Read Bytes Pin
OriginalGriff11-May-14 6:22
mveOriginalGriff11-May-14 6:22 
GeneralRe: Deleting Read Bytes Pin
Richard MacCutchan11-May-14 7:17
mveRichard MacCutchan11-May-14 7:17 
GeneralRe: Deleting Read Bytes Pin
Richard MacCutchan11-May-14 7:17
mveRichard MacCutchan11-May-14 7:17 
QuestionHow Do I Select Particular Data From Sql Database In C#? Pin
hahaahahaahahhahahahhahaha10-May-14 18:29
hahaahahaahahhahahahhahaha10-May-14 18:29 
AnswerRe: How Do I Select Particular Data From Sql Database In C#? Pin
OriginalGriff10-May-14 21:31
mveOriginalGriff10-May-14 21:31 
QuestionUpdating UI from Task Pin
GrooverFromHolland10-May-14 5:40
GrooverFromHolland10-May-14 5:40 
AnswerRe: Updating UI from Task Pin
Eddy Vluggen12-May-14 8:31
professionalEddy Vluggen12-May-14 8:31 
GrooverFromHolland wrote:
They both do the job, but what is better
They do the same; which one is 'better' is nitpicking. If you want to pick the nits (which can be fun and educational), download ILspy and decompile the class.

The Invoke-pattern is older than the Task-class, which was added in 4 (?). To some, the newer way is "more readable" as it does the same with less code (=fewer bugs). Others will be used to the old pattern.

I prefer not to have a wait or an extra task, but to invoke the UI from the thread using the classical invoke-pattern from MSDN;
C#
  1  using System;
  2  using System.Collections.Generic;
  3  using System.Linq;
  4  using System.Text;
  5  using System.Windows.Forms;
  6  using System.Threading.Tasks;
  7  
  8  namespace ConsoleApplication2
  9  {
 10      class Program
 11      {
 12          static void Main()
 13          {
 14              System.Threading.Thread.CurrentThread.Name = "Main thread";
 15              using (var f = new SomeForm())
 16                  f.ShowDialog();
 17          }
 18      }
 19      class SomeDataCollection : List<Guid> { }
 20      class SomeForm: Form
 21      {
 22          ListBox _listBox1;
 23          Button _button1;
 24          public SomeForm()
 25          {
 26              _listBox1 = new ListBox() { Dock = DockStyle.Fill };
 27              _button1 = new Button() { Dock = DockStyle.Top };
 28              Controls.AddRange(new Control[] { _listBox1, _button1 });
 29  
 30              _button1.Click += delegate
 31              {
 32                  Task Tf2 = Task.Factory.StartNew(() =>
 33                  {
 34                      SomeDataCollection _data = getData();
 35                      showData(_data);
 36                  });
 37              };
 38          }
 39          void showData(IList<Guid> whatData)
 40          {
 41              if (InvokeRequired)
 42              {
 43                  Invoke(new Action<SomeDataCollection>(showData), new[] { whatData });
 44                  return;
 45              }
 46  
 47              _listBox1.DataSource = whatData;
 48          }
 49          SomeDataCollection getData()
 50          {
 51              var result = new SomeDataCollection();
 52              for (int i = 0; i < 10; i++)
 53              {
 54                  result.Add(Guid.NewGuid());
 55                  System.Threading.Thread.Sleep(150);
 56              }
 57              return result;
 58          }
 59      }
 60  }
I keep preferring the old style as it makes for very predictable code-constructs. I would however add that it's beneficial during debugging to name each thread during coding! Put a breakpoint on line 35, run, and when the debugger hits it, choose "view, threads" from the menu in the VS-IDE. Each thread/task that you launch can have a unique name - which may even include spaces Smile | :)
Bastard Programmer from Hell Suspicious | :suss:
If you can't read my code, try converting it here[^]

GeneralRe: Updating UI from Task Pin
GrooverFromHolland13-May-14 10:31
GrooverFromHolland13-May-14 10:31 
GeneralRe: Updating UI from Task Pin
Eddy Vluggen14-May-14 7:19
professionalEddy Vluggen14-May-14 7:19 
QuestionEditing values in List? Pin
Member 1044193910-May-14 4:16
professionalMember 1044193910-May-14 4:16 
AnswerRe: Editing values in List? Pin
Wes Aday10-May-14 4:25
professionalWes Aday10-May-14 4:25 
AnswerRe: Editing values in List? Pin
pradeep surya10-May-14 5:19
pradeep surya10-May-14 5:19 
AnswerRe: Editing values in List? Pin
Member 1044193910-May-14 6:33
professionalMember 1044193910-May-14 6:33 
AnswerRe: Editing values in List? Pin
Wes Aday10-May-14 14:08
professionalWes Aday10-May-14 14:08 
AnswerRe: Editing values in List? Pin
BillWoodruff10-May-14 13:31
professionalBillWoodruff10-May-14 13:31 
AnswerRe: Editing values in List? Pin
Emre Ataseven11-May-14 7:05
professionalEmre Ataseven11-May-14 7:05 
QuestionError : Expected class,delegate,enum, interface,or struct Pin
Member 104511519-May-14 16:02
Member 104511519-May-14 16:02 
AnswerRe: Error : Expected class,delegate,enum, interface,or struct Pin
BillWoodruff9-May-14 16:44
professionalBillWoodruff9-May-14 16:44 
QuestionError : Expected class,delegate,enum, interface,or struct Pin
Member 104511519-May-14 16:01
Member 104511519-May-14 16:01 
AnswerRe: Error : Expected class,delegate,enum, interface,or struct Pin
Richard MacCutchan9-May-14 21:38
mveRichard MacCutchan9-May-14 21:38 
QuestionI want to learn how to make in C# only Stock bar chart Pin
Member 107434229-May-14 11:47
Member 107434229-May-14 11:47 
AnswerRe: I want to learn how to make in C# only Stock bar chart Pin
Garth J Lancaster9-May-14 13:20
professionalGarth J Lancaster9-May-14 13:20 
GeneralRe: I want to learn how to make in C# only Stock bar chart Pin
Member 107434229-May-14 14:46
Member 107434229-May-14 14:46 
AnswerRe: I want to learn how to make in C# only Stock bar chart Pin
BillWoodruff9-May-14 16:50
professionalBillWoodruff9-May-14 16:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.