Click here to Skip to main content
15,890,845 members
Home / Discussions / C#
   

C#

 
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 
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 
Thank you for the answer Dave.

So I modified the code in the MethodCallback and got exactly what you said Thumbs Up | :thumbsup: :
This type of CollectionView does not support changes to its SourceCollection from a thread different from the Dispatcher thread.


Now I have some more research to go further.

The change in the code is just banal, Binding to a TextBox and Listview's itemssource directly will throw the exception:
C#
public class MainViewModel:BindableBase
   {

     //  ObservableCollection<string> MainCollection = new ObservableCollection<string>();

       #region "ctor"
       public MainViewModel()
       {

           Text = "1: Main UI thread on: " + Thread.CurrentThread.ManagedThreadId.ToString();
           TextCollection.Add("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());
           TextCollection.Add("2,5: 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 ObservableCollection<string> _textCollection = new ObservableCollection<string>();
       public ObservableCollection<string> TextCollection
       {
           get { return _textCollection; }
           set { SetProperty(ref _textCollection, value); }
       }

       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();
           TextCollection.Add("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();
           TextCollection.Add(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);
           }
       }
   }

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 
QuestionWinforms Usercontrol Databinding question Pin
Member 1501196518-Feb-21 5:20
Member 1501196518-Feb-21 5:20 
AnswerRe: Winforms Usercontrol Databinding question Pin
Gerry Schmitz18-Feb-21 7:38
mveGerry Schmitz18-Feb-21 7:38 
GeneralRe: Winforms Usercontrol Databinding question Pin
Member 1501196518-Feb-21 7:47
Member 1501196518-Feb-21 7:47 
GeneralRe: Winforms Usercontrol Databinding question Pin
Gerry Schmitz18-Feb-21 8:07
mveGerry Schmitz18-Feb-21 8:07 

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.