|
|
While rummaging around in my brain a bit I suddenly had a case of deja vu.
The problem lies here in closures, lambda expressions and their interaction with threading.
I'm not sure if I found the post I read a couple of years ago, but here it goes:
Lambda expressions, captured variables and threading[^].
Especially enlightening is answer 2.
Thus please try to rewrite your code in this manner:
[OperationContract]
public string GetData(int value)
{
for (int i = 0; i < 4; i++)
{
Thread t = new Thread(() => {
int j = i;
simpleThread(j);
});
t.Start();
}
return "";
}
Regards,
— Manfred
"I had the right to remain silent, but I didn't have the ability!"
Ron White, Comedian
|
|
|
|
|
Thanks for your efforts, but it still didn't work Mr.Manfred
This produced a debug out like:
3 : 3
3 : 3
3 : 3
4 : 4
I tried Parameterized Thread start:
ParameterizedThreadStart pt = new System.Threading.ParameterizedThreadStart(simpleThread);
Thread t = new System.Threading.Thread(pt);
t.Start(i);
It produces:
0 : 0
2 : 2
3 : 3
1 : 1
It covered all the indexes, but still it's not in the right order. May be we can't expect this in the right order? as they are "Threads!"?
|
|
|
|
|
VKAnanth wrote: May be we can't expect this in the right order? as they are "Threads!"?
You hit it spot on. Threads are scheduled by the system when to run and for how long. So what you are observing now is the expected behaviour, eventhough there might be circumstances where the order would be what you presumed first .
Did you also try the third alternative from the SO post I referenced in my solution attempt?
Regards,
— ManfredPS: Skip the Mr. stuff, we're all peers here on CP.
"I had the right to remain silent, but I didn't have the ability!"
Ron White, Comedian
|
|
|
|
|
I'm just letting the monkey go off my head at the moment as the code works with the ParametereizedThread start. Packing the code and sending it to my mates.
Once done, will come back to explore the method that you pointed out. cheers! Mr. Manfred
|
|
|
|
|
Threading is not deterministic in the order of execution unless you add constructs to synchronize your threads, e.g. Thread 2 waits for completion of Thread 1 before it starts, Thread 3 waits for completion of Thread 2 before it starts, etc. But that would defeat the purpose of threading, if you create threads to allow parallel execution, but then you serialize all of their execution.
(On a multi-core machine, they can even be scheduled to execute physically at the same time, which opens up a can of even more indeterministic behavior )
|
|
|
|
|
If you don't want to execute this sequence on the current thread but still in the same order you could try to just execute the whole loop in another thread.
Change:
for (int i = 0; i < 4; i++)
{
Thread t = new Thread(() => simpleThread(i));
t.Start();
}
to:
Thread t = new Thread(() => {
for (int i = 0; i < 4; i++) {
simpleThread(i);
}
});
t.Start();
This would process all items in the right order while still running in another (not blocking) thread.
|
|
|
|
|
The problem is the lambda expression, it references the variable i of the outer method at the time of the thread execution, and that is likely after all threads are created. This is not like a passed parameter that gets copied at the time of the thread creation.
Try creating a thread that takes a parameter and pass the i there, this should resolve the issue.
|
|
|
|
|
|
VKAnanth wrote: And the problem is random
Ast stated elsewhere...
No the problem is that you are using threads and you think that there should be an order.
It is simple - stopping thinking that. There is no order. It doesn't matter how you create them nor execute them - there still is no order.
If you want an order then you must assert that order yourself into the code.
HOW you do that depends on what you actually want to acheive. It could be as simple as just ordering the final set after processing of all the threads completes. Or providing a bucket into which each thread puts its result.
|
|
|
|
|
Sometimes the execution order does not matter, but the publishing of the result (e.g. if your processing takes significant time before results are available). In this case you should spinup the threads and let them execute in any order as they will, but when the results are available wait for the completion of the "previous" thread and then publish your results.
Using a pattern like this would allow to use the parallel execution of the long processing and still have the serialized behavior at time of the publishing of the results.
|
|
|
|
|
In last week, I wanted to make a presentation viewer(Slide show) using C#.
I went through MS-PowerPoint and its done but licensed(So not of my use).
MS-PowerPoint Viewer(No support in C#).
Then LibreOffice and OpenOfficeOrg- Too much complicated to use, having errors in libraries.
So what may be the best way to implement a presentation controller in my form.
I can use C# or C++.
Please suggest your opinions.
Thanks!
|
|
|
|
|
|
This is the way, I used very first time but whenever the .ppt will be opened a new window will
bet there.
So that's not working.
any other suggestion to open this in my own form.
|
|
|
|
|
|
This helped me...
Can I do it with OpenOffice or LibreOffice.
Because MS-PowerPoint is a license Product.
Suggest me...
|
|
|
|
|
how to index all the data from disk automatically in background in c# application
modified 19-Mar-13 16:29pm.
|
|
|
|
|
Remove this post. It's VERY unlikely that the person you're addressing this to will ever see it and the only people interested in your email address are the spam bots that scan these forums pretty regularly looking for email addresses to send all kinds of spam to.
Post your question in the forum at the bottom of the article that you're talking about.
|
|
|
|
|
Hello All,
I am having issues with maintaining info when person uses browser back button. I work for a company which is an online traffic school course. The issues I am having is say a student starts on lesson 1. Within lesson 1 there are 10 pages of content that they must read and click Next button to go to next page. Once they then have read all 10 pages, then they go to a quiz where they are given one question at a time (there are 5 questions). They click Next button to move to each question. Once they have answered all questions, then they go to a grade page. If they passed, then they click on continue button to go to lesson 2. Then onto quiz for lesson 2. This part all works great and if they click our programmed Back buttons everything works great as well. The problem is if they go from say lesson 1 all the way to lesson 3 (just an example) and they decide to use the browser's back button to go all the way back to lesson 1, everything tends to get messed up. Since we are driving off of class Student which contains current lesson num, current page num, current question num, once we advance further in the course these values get updated. My problem is that when the student continuously clicks the browser's back button the lesson num, page num and question num (and other content) does not follow suit to what it was. I am not sure why, but the pages are not being cached. Is there anyway to maintain the lesson num, page num and question num as the student clicks browser back button? Meaning as they click back to quiz from lesson 2, it shows lesson 2 (it is currently showing lesson 3 since that is how far have advanced) and same with the lesson pages that they must read. I have tried to use hidden input, but it does not seem to work correctly all the time. Any ideas???
Thanks,
Brad
|
|
|
|
|
You might get a better answer if you ask in the ASP.NET area as this question has nothing to do with C#.
|
|
|
|
|
I was not sure where to post. My code behind is C#.
|
|
|
|
|
You'll be better of posting here as the problem is more to do with ASP.NET than C# in particular
ASP.NET forum[^]
Lobster Thermidor aux crevettes with a Mornay sauce, served in a Provençale manner with shallots and aubergines, garnished with truffle pate, brandy and a fried egg on top and Spam - Monty Python Spam Sketch
|
|
|
|
|
As others have said, this really should be in the ASP.NET forum. However, some food for thought for you - what you are looking at maintaining is effectively the maximum point they have reached. To this end, you might want to use a combination of the user Session and a database (if the user can log out and log back in) to hold this value.
|
|
|
|
|
I have since posted in ASP.NET forum.
Our database maintains current lesson, page, question, advanced (furthest point reached) lesson, page, question, and max (furthest point to reach) lesson, page, question. As the student moves forward, the current and advanced values get set. For example, when the student views the lesson the .ASPX page displays lesson # and page #'s available in lesson with current page highlighted and the lesson content (the verbage they must read to pass the quizzes) for that lesson/page combo is displayed. As they move forward in the pages, the highlighted page # changes and the lesson content changes. Once they reach the max page for that lesson and they click on Next button, they then go to quiz. On quiz ASPX page, the lesson and question # are displayed (lesson # and question # are set by current lesson and current question values) as well as the associated question and possible answers. Once student then goes to lesson 2, current lesson # = 2, current page # = 1, advanced lesson # = 2 and advanced page # = 1. Now I have noticed different things happen based on what browser is being used. For example in Firefox, once student goes all the way through lesson 1, takes lesson 1 quiz, passes and goes to lesson 2. Now if student clicks browser back button everything seems to be ok, it appears to be cached. If student clicks browser back button to lesson 1 quiz question 2, everything seems to be ok. That is it shows Lesson 1 Question 2 with the appropriate question and possible answers (with their answer selected). However, if student goes all the way back to lesson 1 quiz question 1, it displays Lesson 2 question 1 but with lesson 1 question 1 question and answer. Now if student clicks on our programmed Next button, the program now thinks we are on lesson 2 quiz and next shows lesson 2 question 2 along with a question and possible answers that are indeed from lesson 2 and none of the answer radio buttons are set since have not taken lesson 2 quiz.
I would have thought that everything would have been cached. So no matter how many times I click browser back button from my furthest point it would be showing exact pages I went through in reverse order, but this is not the case. Any ideas???
Thanks,
Brad
|
|
|
|
|