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

C#

 
AnswerRe: Multiple chart display in winforms devexpress Pin
Gerry Schmitz27-Apr-21 11:03
mveGerry Schmitz27-Apr-21 11:03 
QuestionSeparating numbers joined by "," into an array Pin
Alex Dunlop23-Apr-21 5:50
Alex Dunlop23-Apr-21 5:50 
AnswerRe: Separating numbers joined by "," into an array Pin
Richard MacCutchan23-Apr-21 6:09
mveRichard MacCutchan23-Apr-21 6:09 
AnswerRe: Separating numbers joined by "," into an array Pin
Dave Kreskowiak23-Apr-21 6:12
mveDave Kreskowiak23-Apr-21 6:12 
GeneralRe: Separating numbers joined by "," into an array Pin
Alex Dunlop23-Apr-21 6:53
Alex Dunlop23-Apr-21 6:53 
AnswerRe: Separating numbers joined by "," into an array Pin
OriginalGriff23-Apr-21 8:26
mveOriginalGriff23-Apr-21 8:26 
QuestionSeeking a better understanding of .NET multithreading and the System.Threading.Tasks.Parallel methods Pin
pr1mem0ver23-Apr-21 1:36
pr1mem0ver23-Apr-21 1:36 
AnswerRe: Seeking a better understanding of .NET multithreading and the System.Threading.Tasks.Parallel methods Pin
Richard Deeming23-Apr-21 6:37
mveRichard Deeming23-Apr-21 6:37 
Parallel.For doesn't return a Task which can be awaited; it has no choice but to block the current thread until the processing has been completed. By wrapping it in a Task.Run, you're blocking a background thread instead of the UI thread.

The Parallel methods also don't work well with async methods. The delegate you pass in is expected to run synchronously to completion.

You've declared your DoSomeMajorChanges method as async void. Your should avoid async void like the plague:
Avoid async void methods | You’ve Been Haacked[^]

The await Task.Run(...) lines in your DoSomeMajorChanges method serve no purpose. Since there are no other awaits in that method, you can simply make it synchronous.
C#
internal bool DoSomeMajorChanges(RichTextboxBuilder builder, StreamWriter changeLog)
{
    bool result;
    changeLog.LogAction(this);
    result = coreFile.DoChanges();
    builder.Control.BeginInvoke(new Action() builder.NotifyUser("Some Change Occurred", Color.Red));
    foreach (ResourceFile file in this.AssociatedFiles)
    {
        changeLog.LogAction(file);
        result |= file.DoChanges();
        builder.Control.BeginInvoke(new Action() builder.NotifyUser("Some Change Occurred", Color.Blue));
    }
    
    return result;
}

The delegate you're passing to Parallel.For is referencing captured variables (file and builder). This is not thread-safe, which is why you are seeing inconsistent results. Move the variable declarations inside the delegate:
C#
Task.Run(() =>
{
    Parallel.For(0, batch.Count, i =>
    {
        var file = batch[i];
        var builder = RichTextboxBuilder.BeginConcurrentAppend(i);
        file.DoSomeMajorChanges(builder, parseableActionLog)
        RichTextboxBuilder.EndConcurrentAppend(i);
    });
});
NB: You will not be able to refer to the last RichTextboxBuilder instance outside of the loop.

It's not clear what your RichTextboxBuilder methods are doing. They could potentially be harming your concurrency.



"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer

GeneralRe: Seeking a better understanding of .NET multithreading and the System.Threading.Tasks.Parallel methods Pin
pr1mem0ver24-Apr-21 4:09
pr1mem0ver24-Apr-21 4:09 
GeneralRe: Seeking a better understanding of .NET multithreading and the System.Threading.Tasks.Parallel methods Pin
Richard Deeming25-Apr-21 21:16
mveRichard Deeming25-Apr-21 21:16 
GeneralRe: Seeking a better understanding of .NET multithreading and the System.Threading.Tasks.Parallel methods Pin
pr1mem0ver24-Oct-21 11:49
pr1mem0ver24-Oct-21 11:49 
AnswerRe: Seeking a better understanding of .NET multithreading and the System.Threading.Tasks.Parallel methods Pin
Gerry Schmitz23-Apr-21 7:07
mveGerry Schmitz23-Apr-21 7:07 
QuestionIPC Implementation for sending an integer from C++ exe to C# exe Pin
LokeshVarman22-Apr-21 22:32
LokeshVarman22-Apr-21 22:32 
QuestionRe: IPC Implementation for sending an integer from C++ exe to C# exe Pin
Richard MacCutchan22-Apr-21 23:05
mveRichard MacCutchan22-Apr-21 23:05 
AnswerRe: IPC Implementation for sending an integer from C++ exe to C# exe Pin
Victor Nijegorodov22-Apr-21 23:11
Victor Nijegorodov22-Apr-21 23:11 
GeneralRe: IPC Implementation for sending an integer from C++ exe to C# exe Pin
LokeshVarman23-Apr-21 0:06
LokeshVarman23-Apr-21 0:06 
QuestionHow to access incrementing ID number in SQL CE? Pin
Alex Dunlop22-Apr-21 5:49
Alex Dunlop22-Apr-21 5:49 
AnswerRe: How to access incrementing ID number in SQL CE? Pin
OriginalGriff22-Apr-21 6:08
mveOriginalGriff22-Apr-21 6:08 
AnswerRe: How to access incrementing ID number in SQL CE? Pin
Dave Kreskowiak22-Apr-21 6:23
mveDave Kreskowiak22-Apr-21 6:23 
AnswerRe: How to access incrementing ID number in SQL CE? Pin
SeanChupas22-Apr-21 6:43
SeanChupas22-Apr-21 6:43 
QuestionHow to preserve DataGridView row and text color when using column filters? Pin
Alex Dunlop21-Apr-21 4:49
Alex Dunlop21-Apr-21 4:49 
AnswerRe: How to preserve DataGridView row and text color when using column filters? Pin
Gerry Schmitz21-Apr-21 6:22
mveGerry Schmitz21-Apr-21 6:22 
GeneralRe: How to preserve DataGridView row and text color when using column filters? Pin
Alex Dunlop21-Apr-21 6:34
Alex Dunlop21-Apr-21 6:34 
GeneralRe: How to preserve DataGridView row and text color when using column filters? Pin
Gerry Schmitz21-Apr-21 6:45
mveGerry Schmitz21-Apr-21 6:45 
GeneralRe: How to preserve DataGridView row and text color when using column filters? Pin
Alex Dunlop21-Apr-21 16:53
Alex Dunlop21-Apr-21 16: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.