Solved

Delegate, Lambda and invoke.

Posted on 2010-11-29
10
702 Views
Last Modified: 2012-05-10
Hi!

I have a question regarding these examples I have here. Is there another way to do this , a better way? Should I use the MethodInvoker?
// using delegate 
this.workflowRuntime.WorkflowCompleted += delegate(object sender, WorkflowCompletedEventArgs e)
{
     BeginInvoke(new MethodInvoker(delegate()
     {
          this.uxInfo.Text = "Completed";
      }));
};

// Using lambda
this.workflowRuntime.WorkflowCompleted += (sender, e) => BeginInvoke(new MethodInvoker(delegate
{
     this.uxInfo.Text = "Completed";
}));

Open in new window

0
Comment
Question by:AWestEng
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
10 Comments
 
LVL 18

Accepted Solution

by:
Richard Lee earned 500 total points
ID: 34231342
Both examples are the same. The second just uses the more concise lamba syntax. Your aim here is to execute the method asynchronously which is achieve by BeginInvoke, however one drawback is that it is executed asynchronously on the same thread that created the UI control's handle was created on, see:

http://msdn.microsoft.com/en-us/library/system.windows.forms.control.begininvoke.aspx

You should consider using the new .NET 4 multi-threading features which will execute on another thread and synchronise to allow UI updates. The following example demonstrates both doing the above in a WPF application, but then customised to work with any type of application.

http://blogs.msdn.com/b/csharpfaq/archive/2010/06/18/parallel-programming-task-schedulers-and-synchronization-context.aspx

DaTribe
0
 
LVL 18

Expert Comment

by:Richard Lee
ID: 34231368
BTW there are four articles in this series. You should probably start from 1 to appreciate fully the new Task Parallel Library (TPL).

http://blogs.msdn.com/b/csharpfaq/archive/2010/06/01/parallel-programming-in-net-framework-4-getting-started.aspx

DaTribe
0
 
LVL 1

Author Comment

by:AWestEng
ID: 34232412
sounds really nice, I will read the articles, but I use Winforms in this app, is it possible to do it there too?
0
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
LVL 18

Expert Comment

by:Richard Lee
ID: 34232589
The articles focus on WPF and WinForms. The TPL works the same for both the only difference is just how the UI controls are updated.

DaTribe
0
 
LVL 1

Author Comment

by:AWestEng
ID: 34233204
nice reading, thx.

In this case I have a workflow instance, and what I have read the workflow is executed in it's own thread, it is sending back some stuff to the ui tread. But I'm not sure how to use this in my workflow. Is it possible to implement it here to? But when is it good to use, in the case when there is only a lot of data exchange between a backround thread and the UI thread or is good to use  even when there is few data exchange between them?
0
 
LVL 18

Expert Comment

by:Richard Lee
ID: 34233308
When faced with this decision it is prudent to ask yourself if the work to be accomplished will be enough to cause the UI to freeze preventing the user from performing any actions. If the answer is yes, then you have valid candidate.

Besides UI situations if your application required background work to be done (meaning your application can proceed while a background thread does something) then this is a valid candidate.

I think the question to ask here is are you having a problem to begin with or do you just want to perform multi-threading.

What exactly is the problem?

DaTribe
0
 
LVL 1

Author Comment

by:AWestEng
ID: 34233709
in this case  there are really no problems. this is more of a "good practice" question.

I use a "WF - state machine" in my app, and I send back which state is currently executed and show that informaiton in a listview and light up som "leds" that are placed in image that represents the state machine, so I can see how the states are executed.
0
 
LVL 18

Expert Comment

by:Richard Lee
ID: 34237933
If the application is working already and the workflow executes on a separate thread then unless there are more specifics peculiarities then there is no recommended that I can provide besides what has been previously mentioned.

DaTribe
0
 
LVL 1

Author Closing Comment

by:AWestEng
ID: 34238018
thx man :)
0
 
LVL 18

Expert Comment

by:Richard Lee
ID: 34238260
No probs.
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question