Click here to Skip to main content
15,888,113 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
what is problem in the my code?
 private void button1_Click(object sender, EventArgs e)
{
     int i = 0;
     ParameterizedThreadStart start = new ParameterizedThreadStart(gh);
     Thread.CurrentThread.Priority = ThreadPriority.Lowest;

     while (i < 100)
     {
         i++;

         System.Threading.Thread u = new System.Threading.Thread(start);
         u.Priority = ThreadPriority.Highest;
         u.Start(i);
     }
 }


 void gh(object e)
 {
     b = delegate()
     {
         label1.Text = string.Format("Step is :{0}", (int)e);
     };
     Invoke(b);
 }



but this code execute correctly.
private void button1_Click(object sender, EventArgs e)
{
         ThreadStart start = delegate()
         {
             int i = 0;
             while (i < 100)
             {
                 i++;
                 b = delegate()
                 {
          label1.Text = string.Format("Step is :{0}", i);
                 };
                 Invoke(b);
             }
         };
     System.Threading.Thread u = new System.Threading.Thread(start);
     u.Start();
 }
Posted
Updated 3-Feb-11 4:32am
v2
Comments
Rob Philpott 3-Feb-11 11:22am    
It would help if you specified what the problem you are experiencing is. What do you expect to happen, and what actually happens?

In the first example, you are creating 100 threads. You can't know in which order they will be executed.

In the second example, you are creating only 1 thread. Since the while loop is executed within this one thread, the steps are executed in the expected order.
 
Share this answer
 
I can see a couple of things wrong, but it would help for you to post the error you receive when you are trying to run. My first thought is that you need to wrap the label1.Text = call with an InvokeRequired block. See the MSDN documentation for how to do that.
 
Share this answer
 
v2
Comments
Rob Philpott 3-Feb-11 11:38am    
Well, that's not really necessary because in this example the label is always updated by non GUI threads, and the Invoke takes care of marshaling the value over to the GUI thread.
Sergey Alexandrovich Kryukov 3-Feb-11 21:08pm    
Rob, what you say is absurd: yes, label is updated by non UI thread and this is wrong, second part is correct and this is what should used. There is no "marshaling the value": a delegate is created and put in a queue of the UI thread.
--SA
Sergey Alexandrovich Kryukov 3-Feb-11 21:06pm    
Marcus, you're right about invocation - my 5
--SA
Rob Philpott 4-Feb-11 4:15am    
Dude, you need to work on your interpersonal skills. Claiming a point someone else made is 'absurd' just gets their back up and doesn't help discuss the problem at hand. It's not absurd, it's correct. Let's go through this again.

Marcus is raising the issue that you cannot access a control on a thread other than the one that created it - all correct. Unless you've disabled the feature, on .NET 2 onwards you'll get an exception if you try. In the example though, *this never happens*. Therefore the InvokeRequired check is not required. An anonymous delegate is used to update the GUI, but this is is not called using delegate.Invoke but rather Control.Invoke(delegate), the synchronous version of Control.BeginInvoke you're probably hoping for. This is called thread marshaling. Hope that makes more sense.
Sergey Alexandrovich Kryukov 4-Feb-11 5:32am    
I'm working on that right now.
After you re-phrase it, it makes sense. One problem is probably your writing. When you said "that's not really necessary", "that's" creates ambiguity. You did not say InvokeRequired is redundant. I completely agree. As to marshaling: I still doubt it is called marshaling, may be, but marshaled is not the "value". I explain: the delegate (with all parameter instances required for the call) goes to the queue to be used by UI thread. As another interface to this functionality is Dispatcher, I would rather call it dispatching or invocation. I don't mind the term here. I do not concern your understanding. You should understand that inaccurate wording can cause misunderstanding.
If I hurt your feeling I apologize. I did not mean to.
--SA
Right, if you're expecting each started thread to update the label with the next value of i, you're going to need some form of synchonization, like WaitForSingleObject.

I don't believe your thread is a background thread unless you set it so. (e.g., "u.IsBackground = true"). So it blows through the "if" check.

The only thing helping you here is the Thread.Sleep call, and then only whenever the started thread completes before 10ms passes.
 
Share this answer
 
Comments
[no name] 4-Feb-11 16:04pm    
thank i solve the proble
I Improve my code but problem not solve.

    int i = 0;
 ParameterizedThreadStart start = new ParameterizedThreadStart(gh);
    Thread.CurrentThread.Priority = ThreadPriority.Lowest;

    Thread u = new Thread(start);
    u.Priority = ThreadPriority.Highest;

    while (i < 100)
    {
        if (u.IsBackground)
        {
            while (u.IsBackground)
            {
                if (!u.IsBackground) break;
            }
        }
            u = new Thread(start);

            i++;
            u.Start(i);

       System.Threading.Thread.Sleep(10);
    }
}


void gh(object e)
{
    if (InvokeRequired)
    {
        b = delegate()
        {
            label1.Text = string.Format("Step is :{0}", e.ToString());                     //
        };
        Invoke(b);
    }
    else label1.Text = string.Format("Step is :{0}", e.ToString());
}
 
Share this answer
 
Comments
Olivier Levrey 3-Feb-11 11:40am    
This piece of code doesn't make sense to me... Can you explain what you want to do?

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