Click here to Skip to main content
15,892,965 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
if possible,Determined what sendMesssage,postmessage,messaege pomp,message queue in the my code .too why "label1.Text = "Csharp"; must be wait to envent to be ended.


public partial class Form1 : Form
  {
      public Form1()
      {
          InitializeComponent();
      }

      private void button1_Click(object sender, EventArgs e)
      {

          y();
          label1.Text = "Csharp";
          ThreadStart p=new ThreadStart(x);
          Thread g=new Thread(p);
          g.Start();
          y();

      }



      void y()
      {
          label2.Text = "mehdi";

      }

      void x()
      {
          label2.Text = "mehdi";
          Thread.Sleep(100);

      }
  }
Posted
Updated 9-Feb-11 12:06pm
v2
Comments
Manfred Rudolf Bihy 9-Feb-11 18:26pm    
Are you trying to drive us crazy? You've posted enough questions on this and also received a lot of sufficient answers concerning GUI updates from a thread different from GUI thread.

Besides that you should reconsider your formulation of this question as it is absolutely unintelligible.

Sorry!
Ali Al Omairi(Abu AlHassan) 9-Feb-11 18:40pm    
Sir, be easy with him. he's just a student.
Sergey Alexandrovich Kryukov 9-Feb-11 18:45pm    
Well, amazingly bad student. Did you read all the post on the topic? All the Answers? Did you see the cases when Mehdi did directly opposite to what was advised and asked why it still hanging? Then do it first, and after that decide who you want to be nice to.

He must be just a bit more easy with us. And by the way, in my time students were expelled and went to serve as soldiers at the end of the term. Three failure during the same session and you're good to go. Quite fair for a free-of-charge services!

--SA
Manfred Rudolf Bihy 9-Feb-11 18:51pm    
I think we were really patient sofar, but OP keeps on coming back with questions on the same subject. It almost seems that the advice OP was given before does not have any effect. I'll try to keep my patience because I still can remember how hard it is for a beginner. :)
Sergey Alexandrovich Kryukov 9-Feb-11 19:01pm    
Agreed, but my answers are still real and constructive and not just rants. Not to demand nice or at least more considerate behavior though would be plain stupid.

Look at this answer: http://www.codeproject.com/Questions/153936/why-in-Monitor-Wait-Using-of-lock-object.aspx.

Not very simple, right?

Now, in next question, OP put the code following this pattern (very hard-to-explain effect of combination of lock and invoke) and asked why it hang. I answered again. 21 Questions with coming back to wrong things, ignoring advice, but with more and more "hi-tech" detail contributing to the mess.

--SA

1 solution

I mentioned to you many times: stop creating threads in the middle of your UI. This way you can spawn any number of threads, one per click. Why?

Create a permanent thread from the very beginning and feed tasks to it. You don't need more then one, right. Hold idle thread with EventWaitHandle, for example, or use blocking queue. I recommended it many times, see this: Simple Blocking Queue for Thread Communication and Inter-thread Invocation[^]. Alternatively, take a thread from the thread poll, but again, don't do it on every click.

Now button1_Click violates all possible Microsoft naming conventions. Don't tell me it generated by Microsoft's designer. Yes, but you're not a robot to repeat it! Use anonymous delegate, grow up.

This is all a foreword, now a word:

You're call x() in no-UI thread. It means you should not use UI operation label2.Text = "mehdi";. You need to Invoke it. A call to y() in your call is valid.

—SA
 
Share this answer
 
v2
Comments
Sandeep Mewara 10-Feb-11 0:36am    
Well, a 5!
Sergey Alexandrovich Kryukov 10-Feb-11 0:44am    
Again and again; it never ends, right?
Thank you Sandeep.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900