Click here to Skip to main content
15,886,963 members
Home / Discussions / C#
   

C#

 
GeneralRe: I am getting the following error : Method not found: 'Void **************************************************************' Pin
Member 1508430525-Feb-21 15:40
Member 1508430525-Feb-21 15:40 
QuestionExtract RSAParameters from custom Private Key Pin
Member 1508150323-Feb-21 21:09
Member 1508150323-Feb-21 21:09 
AnswerRe: Extract RSAParameters from custom Private Key Pin
Gerry Schmitz24-Feb-21 8:50
mveGerry Schmitz24-Feb-21 8:50 
GeneralRe: Extract RSAParameters from custom Private Key Pin
Member 1508150324-Feb-21 18:33
Member 1508150324-Feb-21 18:33 
AnswerRe: Extract RSAParameters from custom Private Key Pin
Member 1508430525-Feb-21 15:42
Member 1508430525-Feb-21 15:42 
QuestionUsing azure storage account to share file with multiple users Pin
Lmathew22-Feb-21 19:06
Lmathew22-Feb-21 19:06 
AnswerRe: Using azure storage account to share file with multiple users Pin
Gerry Schmitz22-Feb-21 19:11
mveGerry Schmitz22-Feb-21 19:11 
QuestionAsync task Pin
Kenneth Haugland20-Feb-21 6:01
mvaKenneth Haugland20-Feb-21 6:01 
In the past, I did most of my work with parallel objects using Rx, but that was made a bit cumbersome in the recent project. So I was following the code from Brian Lagunas, I made some tweaks to it to see how it all worked.
C#
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace WpfAsynTaskAwait
{
    public class MainViewModel:BindableBase
    {

        #region "ctor"
        public MainViewModel()
        {
            Text = "1: Main UI thread on: " + Thread.CurrentThread.ManagedThreadId.ToString();
            Task.Run(() => DoSomething(MethodCallback)).Await(CompletedCallback, ErrorCallback);
        }
        #endregion

        async Task DoSomething(Action<string> methodCallback)
        {
            await Task.Delay(1500);
            methodCallback?.Invoke("2: Task starts on thread: " + Thread.CurrentThread.ManagedThreadId.ToString());
            await Task.Delay(1500);
            
            //throw new Exception("Aborted exception on thread " + Thread.CurrentThread.ManagedThreadId.ToString());
            
            methodCallback?.Invoke("3: Task runs on thread: " + Thread.CurrentThread.ManagedThreadId.ToString());
            await Task.Delay(1500);            
        }

        #region "Variables"
        private string _text = "Default value";

        public string Text
        {
            get { return _text; }
            set
            {
                SetProperty(ref _text, value);
            }
        }
        #endregion

        #region "Callback methods"

        private void CompletedCallback()
        {
            Text = "4: Method completed called from thread: " + Thread.CurrentThread.ManagedThreadId.ToString();
        }

        private void MethodCallback(string message)
        {
            Text = message + Environment.NewLine +  "Task callback from thread: " + Thread.CurrentThread.ManagedThreadId.ToString(); 
        }

        private void ErrorCallback(Exception ex)
        {
            Text = ex.Message;
        }
        #endregion
    }
    public static class TaskExtensions
    {
        public async static void Await(this Task task, Action completedCallback, Action<Exception> errorCallback)
        {
            try
            {
                await task;
                completedCallback?.Invoke();
            }
            catch (Exception ex)
            {
                errorCallback?.Invoke(ex);
            }
        }
    }
}


I just found it rather confusing that the method MethodCallback was allowed to interact directly (update) via binding with a UI-bound element. And my DoSomething does not return a Task but still does not complain about the implementation. This just confuses me. Why am I allowed to interact via callbacks from a worker thread to a UI thread?
QuestionRe: Async task Pin
Eddy Vluggen20-Feb-21 12:51
professionalEddy Vluggen20-Feb-21 12:51 
AnswerRe: Async task Pin
Kenneth Haugland20-Feb-21 23:57
mvaKenneth Haugland20-Feb-21 23:57 
GeneralRe: Async task Pin
Dave Kreskowiak21-Feb-21 5:10
mveDave Kreskowiak21-Feb-21 5:10 
GeneralRe: Async task Pin
Kenneth Haugland21-Feb-21 8:10
mvaKenneth Haugland21-Feb-21 8:10 
GeneralRe: Async task Pin
Richard Deeming21-Feb-21 22:19
mveRichard Deeming21-Feb-21 22:19 
GeneralRe: Async task Pin
Kenneth Haugland22-Feb-21 6:58
mvaKenneth Haugland22-Feb-21 6:58 
GeneralRe: Async task Pin
Richard Deeming21-Feb-21 22:19
mveRichard Deeming21-Feb-21 22:19 
GeneralRe: Async task Pin
Dave Kreskowiak22-Feb-21 3:42
mveDave Kreskowiak22-Feb-21 3:42 
Questionc# OPCgroup.SyncWrite vs. Float type Pin
Roberto64_Ge18-Feb-21 22:00
Roberto64_Ge18-Feb-21 22:00 
AnswerRe: c# OPCgroup.SyncWrite vs. Float type Pin
OriginalGriff18-Feb-21 22:38
mveOriginalGriff18-Feb-21 22:38 
GeneralRe: c# OPCgroup.SyncWrite vs. Float type Pin
Roberto64_Ge19-Feb-21 0:24
Roberto64_Ge19-Feb-21 0:24 
GeneralRe: c# OPCgroup.SyncWrite vs. Float type Pin
OriginalGriff19-Feb-21 0:55
mveOriginalGriff19-Feb-21 0:55 
GeneralRe: c# OPCgroup.SyncWrite vs. Float type Pin
Roberto64_Ge19-Feb-21 1:41
Roberto64_Ge19-Feb-21 1:41 
GeneralRe: c# OPCgroup.SyncWrite vs. Float type Pin
OriginalGriff19-Feb-21 1:58
mveOriginalGriff19-Feb-21 1:58 
GeneralRe: c# OPCgroup.SyncWrite vs. Float type Pin
Roberto64_Ge19-Feb-21 2:28
Roberto64_Ge19-Feb-21 2:28 
GeneralRe: c# OPCgroup.SyncWrite vs. Float type Pin
Roberto64_Ge19-Feb-21 8:27
Roberto64_Ge19-Feb-21 8:27 
GeneralRe: c# OPCgroup.SyncWrite vs. Float type Pin
Roberto64_Ge23-Feb-21 0:53
Roberto64_Ge23-Feb-21 0:53 

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.