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

C#

 
AnswerRe: Handling Cross thread Pin
Richard Deeming22-Mar-16 2:58
mveRichard Deeming22-Mar-16 2:58 
GeneralRe: Handling Cross thread Pin
pradipkilledar27-Mar-16 22:22
pradipkilledar27-Mar-16 22:22 
GeneralRe: Handling Cross thread Pin
Richard Deeming29-Mar-16 1:45
mveRichard Deeming29-Mar-16 1:45 
QuestionGet Picture Using PictureBox Handle Pin
Django_Untaken21-Mar-16 7:29
Django_Untaken21-Mar-16 7:29 
AnswerRe: Get Picture Using PictureBox Handle Pin
User 740747021-Mar-16 8:43
User 740747021-Mar-16 8:43 
QuestionI want to generator serial numbers for rows in datagrid Pin
Engr. Yange21-Mar-16 3:06
Engr. Yange21-Mar-16 3:06 
AnswerRe: I want to generator serial numbers for rows in datagrid Pin
Eddy Vluggen22-Mar-16 7:17
professionalEddy Vluggen22-Mar-16 7:17 
QuestionUpdating the UI from a Rx background thread Pin
Kenneth Haugland20-Mar-16 23:38
mvaKenneth Haugland20-Mar-16 23:38 
I had the strangest problem today, and I did manage to solve it but I don't understand why it wont work out of the box.

So I created an Observable that is just a range:
C#
Observable.Range(0, 15)
    // Take the Observable off the UI thread
    .SubscribeOn(Scheduler.Default)
    //Create parallel tasks
    .Select(x => Task.Run(()=>LongRunningTask(x,
    CompletedOctavebandCalcualtions,
    CalculatedValues),
    CancellationToken.None).ConfigureAwait(true))
    // Start the thread
    .Subscribe();

I have the LongRunning task as follows:
C#
Random RandomTimeInterval = new Random();

public FreqValues LongRunningTask(double currentInteger, IProgress<double> ProgressBarIncrement, IProgress<FreqValues> CalcualtedValues)
{
    FreqValues Result = new FreqValues();
    Result.Frequency = currentInteger;
    Result.Value = currentInteger - RandomTimeInterval.Next(0, 10000);
    Thread.Sleep(RandomTimeInterval.Next(0, 3000));
    Debug.WriteLine("Current integer:{1} on threadId:{0}", Thread.CurrentThread.ManagedThreadId, Result.Frequency.ToString());

    ProgressBarIncrement.Report(1);
    CalcualtedValues.Report(Result);
    return Result;
}

Ï used the IProgress to update the UI on how many threads had completed. I have deliberately messed up the time each thread will take to be able to spot any mistakes. Updating the UI is straight forward:
C#
Progress<double> CompletedOctavebandCalcualtions = new Progress<double>(s =>
{
    pgbProgress.Value += s;
});

Progress<FreqValues> CalculatedValues = new Progress<FreqValues>(s =>
{
    UnsortedCalcualtedValues.Add(s);
    MyStrings.Append(s.Frequency.ToString() + " Hz with the Value: " + s.Value.ToString() + Environment.NewLine);
    if (UnsortedCalcualtedValues.Count == NumberOfFrequencies)
    {
        txtText.Text = MyStrings.ToString();
    }
});

What I don't understand at all is why I cant get the UI values to update if I put anything in the Subscribe. I will have to ObserveOnDispatcher of course, but that is easy:
C#
Observable.Range(0, 15)
    // Take the Observable off the UI thread
    .SubscribeOn(Scheduler.Default)
    //Create parallel tasks
    .Select(x => Task.Run(()=>LongRunningTask(x,
    CompletedOctavebandCalcualtions,
    CalculatedValues),
    CancellationToken.None).ContinueWith(e=>e.Result))
    //To the UI thread
    .ObserveOnDispatcher()
    // Start the thread
    .Subscribe(evt=>
    {
        txtText.Text += evt.Result.Frequency.ToString();
    }
    );

The problem with that code is simply that the callback to the progress bar doesn't work, and the code inside the Subscribe will only execute once all the threads are completed. That the end result only shows after it is completed is fine for the text box, but I need to show the calculation progress. Can anyone tell me why It won't update?
AnswerRe: Updating the UI from a Rx background thread Pin
Luc Pattyn21-Mar-16 15:10
sitebuilderLuc Pattyn21-Mar-16 15:10 
GeneralRe: Updating the UI from a Rx background thread Pin
Kenneth Haugland21-Mar-16 19:16
mvaKenneth Haugland21-Mar-16 19:16 
GeneralRe: Updating the UI from a Rx background thread Pin
Luc Pattyn21-Mar-16 19:35
sitebuilderLuc Pattyn21-Mar-16 19:35 
GeneralRe: Updating the UI from a Rx background thread Pin
Kenneth Haugland21-Mar-16 20:24
mvaKenneth Haugland21-Mar-16 20:24 
AnswerRe: Updating the UI from a Rx background thread Pin
Kenneth Haugland21-Mar-16 23:07
mvaKenneth Haugland21-Mar-16 23:07 
AnswerRe: Updating the UI from a Rx background thread Pin
Kenneth Haugland22-Mar-16 23:56
mvaKenneth Haugland22-Mar-16 23:56 
QuestionIntermittent problem with design-time display of inherited UserControls in VS 2013 Pin
BillWoodruff19-Mar-16 4:36
professionalBillWoodruff19-Mar-16 4:36 
AnswerRe: Intermittent problem with design-time display of inherited UserControls in VS 2013 Pin
OriginalGriff19-Mar-16 5:23
mveOriginalGriff19-Mar-16 5:23 
GeneralRe: Intermittent problem with design-time display of inherited UserControls in VS 2013 Pin
BillWoodruff19-Mar-16 8:30
professionalBillWoodruff19-Mar-16 8:30 
Questionproblem asked in SIEMENS. Pin
Member 1240313019-Mar-16 0:27
Member 1240313019-Mar-16 0:27 
GeneralRe: problem asked in SIEMENS. Pin
Sascha Lefèvre19-Mar-16 0:32
professionalSascha Lefèvre19-Mar-16 0:32 
AnswerRe: problem asked in SIEMENS. Pin
Richard MacCutchan19-Mar-16 0:37
mveRichard MacCutchan19-Mar-16 0:37 
AnswerRe: problem asked in SIEMENS. Pin
Patrice T19-Mar-16 12:05
mvePatrice T19-Mar-16 12:05 
Questionsorting records in datagrid Pin
Engr. Yange18-Mar-16 20:05
Engr. Yange18-Mar-16 20:05 
AnswerRe: sorting records in datagrid Pin
Mycroft Holmes18-Mar-16 21:43
professionalMycroft Holmes18-Mar-16 21:43 
QuestionIs there a better way in Rx? Pin
Matt T Heffron18-Mar-16 13:06
professionalMatt T Heffron18-Mar-16 13:06 
AnswerRe: Is there a better way in Rx? Pin
Kenneth Haugland18-Mar-16 16:02
mvaKenneth Haugland18-Mar-16 16:02 

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.