Click here to Skip to main content
15,888,579 members
Home / Discussions / C#
   

C#

 
GeneralRe: Non-XML ini-file supporting arrays, in plain text format? Pin
Dave Kreskowiak20-Jun-14 1:48
mveDave Kreskowiak20-Jun-14 1:48 
Question"One or more exceptions" occurred at Task.Start() Pin
Sharath C V19-Jun-14 3:23
professionalSharath C V19-Jun-14 3:23 
AnswerRe: "One or more exceptions" occurred at Task.Start() Pin
OriginalGriff19-Jun-14 3:55
mveOriginalGriff19-Jun-14 3:55 
SuggestionRe: "One or more exceptions" occurred at Task.Start() Pin
Richard Deeming19-Jun-14 4:07
mveRichard Deeming19-Jun-14 4:07 
GeneralRe: "One or more exceptions" occurred at Task.Start() Pin
OriginalGriff19-Jun-14 4:15
mveOriginalGriff19-Jun-14 4:15 
AnswerRe: "One or more exceptions" occurred at Task.Start() Pin
Sharath C V19-Jun-14 21:18
professionalSharath C V19-Jun-14 21:18 
GeneralRe: "One or more exceptions" occurred at Task.Start() Pin
Sharath C V19-Jun-14 19:10
professionalSharath C V19-Jun-14 19:10 
AnswerRe: "One or more exceptions" occurred at Task.Start() Pin
Richard Deeming19-Jun-14 4:06
mveRichard Deeming19-Jun-14 4:06 
Why are you starting a new task and then immediately waiting for it? You're blocking the UI thread waiting for a background thread to complete, so you might as well just do the work on the UI thread.

I suspect the reason you're getting an exception is because you're updating a UI control from a non-UI thread. You will need to use the Invoke method to ensure that the updates only happen on the UI thread.

Unfortunately, since you've chosen to block the UI thread waiting for the background thread to complete, this won't work - the UI thread is blocked waiting for the background thread to complete, and the background thread is blocked waiting for the UI thread to process the Invoke message.

You might be able to work around the problem by using BeginInvoke instead, which won't block the background thread. However, this will change the behaviour of your code, as the list items will not be added in the same sequence.

If you're using .NET 4.5, try using async / await instead:

C#
private async Task SynchronousWait200ms()
{
    await Task.Delay(200);
    listBox.Items.Add("Wait 200 ms, Thread Id = " + Thread.CurrentThread.ManagedThreadId);
}

private async void TaskRunButton_Click1(object sender, EventArgs e)
{
    listBox.Items.Clear();
    
    for (int i = 0; i < 10; i++)
    {
        await SynchronousWait200ms();
    }
    
    listBox.Items.Add("------------");
}


That should work, but you'll get the same thread ID for each item, as the code will be marshalled back to the UI thread.

Perhaps if you explain what you're trying to achieve, we might be able to help.



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



modified 19-Jun-14 10:19am.

GeneralRe: "One or more exceptions" occurred at Task.Start() Pin
Sharath C V19-Jun-14 20:02
professionalSharath C V19-Jun-14 20:02 
QuestionSendkeys to excel without open workbook - C# Pin
bonjot19-Jun-14 1:26
bonjot19-Jun-14 1:26 
AnswerRe: Sendkeys to excel without open workbook - C# Pin
Pete O'Hanlon19-Jun-14 2:11
mvePete O'Hanlon19-Jun-14 2:11 
GeneralRe: Sendkeys to excel without open workbook - C# Pin
bonjot19-Jun-14 2:27
bonjot19-Jun-14 2:27 
AnswerRe: Sendkeys to excel without open workbook - C# Pin
Dave Kreskowiak19-Jun-14 2:11
mveDave Kreskowiak19-Jun-14 2:11 
GeneralRe: Sendkeys to excel without open workbook - C# Pin
bonjot19-Jun-14 2:29
bonjot19-Jun-14 2:29 
QuestionC# FOR Loop Speed Issue Pin
exfosys18-Jun-14 21:02
exfosys18-Jun-14 21:02 
AnswerRe: C# FOR Loop Speed Issue Pin
Pete O'Hanlon18-Jun-14 21:18
mvePete O'Hanlon18-Jun-14 21:18 
AnswerRe: C# FOR Loop Speed Issue Pin
OriginalGriff18-Jun-14 21:51
mveOriginalGriff18-Jun-14 21:51 
GeneralRe: C# FOR Loop Speed Issue Pin
exfosys18-Jun-14 22:41
exfosys18-Jun-14 22:41 
GeneralRe: C# FOR Loop Speed Issue Pin
OriginalGriff18-Jun-14 23:12
mveOriginalGriff18-Jun-14 23:12 
AnswerRe: C# FOR Loop Speed Issue Pin
Mycroft Holmes18-Jun-14 22:31
professionalMycroft Holmes18-Jun-14 22:31 
GeneralRe: C# FOR Loop Speed Issue Pin
exfosys18-Jun-14 22:42
exfosys18-Jun-14 22:42 
GeneralRe: C# FOR Loop Speed Issue Pin
Mycroft Holmes18-Jun-14 22:54
professionalMycroft Holmes18-Jun-14 22:54 
GeneralRe: C# FOR Loop Speed Issue Pin
exfosys18-Jun-14 23:50
exfosys18-Jun-14 23:50 
GeneralRe: C# FOR Loop Speed Issue Pin
PIEBALDconsult19-Jun-14 4:11
mvePIEBALDconsult19-Jun-14 4:11 
SuggestionRe: C# FOR Loop Speed Issue Pin
Nathan Minier20-Jun-14 3:02
professionalNathan Minier20-Jun-14 3: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.