Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0
i'm calling two functions simultaneously in a button click event.
 

 
                 label1.text="hai";
                JukeBox_Timer.Stop();
                JukeBox_Timer.Start();
 
              thread.sleep(3000);
                clearfstext(pagenation_value);
 
                settheme("\\Page Selection.swf");
 

here i want to show the label then it waits for three second then the
settheme("\\Page Selection.swf");
event come
 
but here is the problem that it does not show the label.text the thread sleeps and directly go to the function settheme("\\Page Selection.swf");
Posted 12-Jul-13 1:58am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

lable1.Text = "hai"; doesn't get executed immediately. Like all GUI operations, it gets queued inernally until the next UI redraw. But the UI thread is blocked by thread.Sleep(3000);.
 
So you need to execute the sleep part in a worker thread. Something like
private void DelayedMethod()
{
    System.Threading.Thread.Sleep(3000);
 
    SetTheme("Whatever");
}
 
void YourCurrentMethodOnUiThread()
{
    label1.Text = "hai";
 
    System.Threading.Thread otherThread = new System.Threading.Thread(DelayedMethod);
    otherThread.Start();
}
  Permalink  
Comments
Aatif Ali from Bangalore at 12-Jul-13 7:18am
   
Lukeer U r right..
Tricube at 13-Jul-13 0:02am
   
thanks lukeer itz working perfect.
Tricube at 13-Jul-13 1:18am
   
can you help with multithreading with the sleep like the above example
Tricube at 13-Jul-13 3:11am
   
shows an error while closing " COM object that has been separated from its underlying RCW cannot be used."
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

A simple solution is to force immediate redraw of the label using the Refresh method.
 
               label1.text="hai";
               label1.Refresh();  
               JukeBox_Timer.Stop();
               JukeBox_Timer.Start();
 
               thread.sleep(3000);
               clearfstext(pagenation_value);
 
               settheme("\\Page Selection.swf");
  Permalink  
v2

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 575
1 Kornfeld Eliyahu Peter 409
2 Maciej Los 369
3 DamithSL 196
4 OriginalGriff 188
0 OriginalGriff 6,353
1 DamithSL 4,854
2 Maciej Los 4,476
3 Kornfeld Eliyahu Peter 4,058
4 Sergey Alexandrovich Kryukov 3,917


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 12 Jul 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100