Click here to Skip to main content
15,887,585 members
Home / Discussions / C#
   

C#

 
GeneralRe: how to capture a screen shot usng c# and asp.net Pin
Pete O'Hanlon2-Nov-16 22:51
mvePete O'Hanlon2-Nov-16 22:51 
GeneralRe: how to capture a screen shot usng c# and asp.net Pin
syedkhaleel20102-Nov-16 23:02
syedkhaleel20102-Nov-16 23:02 
GeneralRe: how to capture a screen shot usng c# and asp.net Pin
Pete O'Hanlon2-Nov-16 23:30
mvePete O'Hanlon2-Nov-16 23:30 
QuestionSQL injection prevention Pin
Member 128253812-Nov-16 8:28
Member 128253812-Nov-16 8:28 
AnswerRe: SQL injection prevention Pin
OriginalGriff2-Nov-16 9:46
mveOriginalGriff2-Nov-16 9:46 
AnswerRe: SQL injection prevention PinPopular
Richard Deeming2-Nov-16 10:10
mveRichard Deeming2-Nov-16 10:10 
AnswerRe: SQL injection prevention Pin
V.2-Nov-16 22:29
professionalV.2-Nov-16 22:29 
QuestionAsync Dispatcher Calls - Need an explaination Pin
Foothill1-Nov-16 6:44
professionalFoothill1-Nov-16 6:44 
So I decided to update an older application with calls using await...async instead of embeding Thread.Start(() => { myFunction(); }); lambda expressions for long running operations. I have had a few successes but I am a little confused about making asynchronous method call with the dispatcher.

Lets start with the original multithreaded database call. It updates a ListView when it is done.
C#
public bool PushToDatabase(DataCollection data)
{
 try
 {
  // insert data in database
  PerformDatabaseInsert(data);
  // then update the visual
  App.Current.Dispatcher.Invoke(new Action(() => lst_MyData.ItemsSource = data.ToArray()));
  return true;
 }
 catch (Exception ex)
 {
  // Display error to user
  return false;
 }
}

This is all wrapped up in a click event in a pop-out control
C#
private void btn_Submit_Click(object sender, RoutedEventArgs e)
{
 if (InputsAreGood())
 {
  DataCollection data = GetFormFields();

  Thread thread = new Thread(() =>
  {
   MainWindow.SetCursor_Wait(); // visual candy via the Dispatcher

   if (PushToDatabase(data))
   {
    ClosePopOutControl();
   }

   MainWindow.SetCursor_Arrow();
  });

  thread.Start();
 }
 else
 {
  // inform user what they missed
 }
}

Nothing really fancy. The UI remains responsive while the database operation is performed on another thread.

Now, I converted these to asynchronous methods. The code works but I am still not understanding why it is supposed to be better when it seems to make the code look a lot more complicated. I can only surmise that I am going about this all wrong.

First, I moved the ListView update to a separate function.
C#
public async Task UpdateListViewAsync(DataCollection data)
{
 // Dispatcher is awaitable, so no problems here
 await Dispatcher.Invoke(() =>
 {
  // but you cannot await a void so I have to rewrite it to this
  return Task.Run(() => { lst_MyData.ItemsSource = data.ToArray(); });
 }
}

And added an async function to perform the database call.
C#
public async Task<bool> PushToDatabaseAsync(DataCollection data)
{
 Task<bool> dataPushTask = Task.Run(() => PushToDatabase(data));
 bool result = await dataPushTask;
 await UpdateListViewAsync();
 return result;
}

There are fewer lines of code in the function but am I really gaining anything here?

What I had to rewrite the submit code to really bakes my noodle.
C#
private btn_Submit_Click(object sender, RoutedEventArgs e)
{
 bool taskResult;

 try
 {
  MainWindow.SetCursor_Wait();

  if (InputsAreGood())
  {
   DataCollection data = GetFormFields():

   Func<Task<bool>> dataPushLambda = async () => await PushToDatabaseAsync(data); 
   taskResult = await App.Current.Dispatcher.InvokeAsync(dataPushLambda).Result;

   if (taskResult)
   {
    ClosePopOutForm();
   }
   else
   {
    // tell user that something went wrong
   } 
  }
  else
  {
   // tell user what fields they missed
  }
 }
 catch (Exception ex)
 {
  // display the error to the user
 }
 finally
 {
  MainWindow.SetCusor_Arrow();
 }
}

What am looking for is a deeper explanation of what is going on with these two lines
C#
Func<Task<bool>> dataPushLambda = async () => await PushToDatabaseAsync(data); 
taskResult = await App.Current.Dispatcher.InvokeAsync(dataPushLambda).Result;

Also, what is the difference between these two lines?
taskResult = await App.Current.Dispatcher.InvokeAsync(dataPushLambda).Result;
// and
taskResult = await await App.Current.Dispatcher.InvokeAsync(dataPushLambda);

Lastly, I ran into a lot of trouble with the InvokeAsync call not accepting Task<bool> but I couldn't find anything else that would work.
Is there a better way to call async methods with the Dispatcher then what I have here?
Is (or why is) async...await better then spinning up a separate thread for long running operations?
if (Object.DividedByZero == true) { Universe.Implode(); }
Meus ratio ex fortis machina. Simplicitatis de formae ac munus. -Foothill, 2016

AnswerRe: Async Dispatcher Calls - Need an explaination Pin
Richard Deeming1-Nov-16 7:48
mveRichard Deeming1-Nov-16 7:48 
GeneralRe: Async Dispatcher Calls - Need an explaination Pin
Foothill1-Nov-16 8:12
professionalFoothill1-Nov-16 8:12 
GeneralRe: Async Dispatcher Calls - Need an explaination Pin
Richard Deeming1-Nov-16 8:23
mveRichard Deeming1-Nov-16 8:23 
QuestionLabel printing with c# Pin
candogu1-Nov-16 0:57
candogu1-Nov-16 0:57 
AnswerRe: Label printing with c# Pin
OriginalGriff1-Nov-16 1:44
mveOriginalGriff1-Nov-16 1:44 
AnswerRe: Label printing with c# Pin
Richard Deeming1-Nov-16 3:37
mveRichard Deeming1-Nov-16 3:37 
AnswerRe: Label printing with c# Pin
cinias8-Nov-16 3:49
cinias8-Nov-16 3:49 
QuestionHow people generate bench mark graph for c# routine execution speed Pin
Tridip Bhattacharjee31-Oct-16 19:08
professionalTridip Bhattacharjee31-Oct-16 19:08 
AnswerRe: How people generate bench mark graph for c# routine execution speed Pin
Pete O'Hanlon31-Oct-16 20:52
mvePete O'Hanlon31-Oct-16 20:52 
QuestionHow to implement the huffman using c# for image Pin
Rıza Berkay Ayçelebi31-Oct-16 0:29
Rıza Berkay Ayçelebi31-Oct-16 0:29 
AnswerRe: How to implement the huffman using c# for image Pin
Pete O'Hanlon31-Oct-16 0:50
mvePete O'Hanlon31-Oct-16 0:50 
GeneralRe: How to implement the huffman using c# for image Pin
Rıza Berkay Ayçelebi31-Oct-16 23:14
Rıza Berkay Ayçelebi31-Oct-16 23:14 
GeneralRe: How to implement the huffman using c# for image Pin
Richard MacCutchan1-Nov-16 0:54
mveRichard MacCutchan1-Nov-16 0:54 
GeneralRe: How to implement the huffman using c# for image Pin
Rıza Berkay Ayçelebi1-Nov-16 1:36
Rıza Berkay Ayçelebi1-Nov-16 1:36 
GeneralRe: How to implement the huffman using c# for image Pin
Richard MacCutchan1-Nov-16 1:53
mveRichard MacCutchan1-Nov-16 1:53 
GeneralRe: How to implement the huffman using c# for image Pin
Pete O'Hanlon1-Nov-16 2:30
mvePete O'Hanlon1-Nov-16 2:30 
AnswerRe: How to implement the huffman using c# for image Pin
Jochen Arndt31-Oct-16 1:48
professionalJochen Arndt31-Oct-16 1:48 

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.