Click here to Skip to main content
15,886,519 members
Please Sign up or sign in to vote.
3.75/5 (4 votes)
See more:
The provided event executes all its internal routines "at once", not in steps as i intend...
How to make the routines visible, one at a time ?
C#
private void button1_Click(object sender, EventArgs e)
        {
            LabelProcessing.Visible = true; 
            searchForFiles(); //while this is executing,(takes ~15seconds)the label does not appear.
            LabelProcessing.Visible = false;
        }
Posted
Comments
Sergey Alexandrovich Kryukov 28-Jan-12 18:46pm    
This is not an event!
The question is not correct. What's one at a time. What's "a time"?
--SA
_Q12_ 28-Jan-12 18:59pm    
The "private void button1_Click(object sender, EventArgs e)" its NOT an event?
Then what is it?
If you do really read more carefully, you will see [IN THE CONTEXT] the "time" im referring to.
The question is correct as i can best formulate, because i really do programming for fun and i do NOT finished any special course to learn it, i learn it from internet,tutorials, and good forums with nice people who can have a little compassion for those who try their best to understand this obfuscated language... thank you very much for [downvote] every post i put here.
;)
[no name] 28-Jan-12 19:55pm    
There is nothing wrong with your question worth the one vote. I've tried to compensate as much as I could. You could have described the expected behavior a bit more clearly though.
BillWoodruff 28-Jan-12 22:25pm    
Mark, absolutely agree with you there is nothing wrong with the question asked. I've voted it up to balance the "undeserved" 1-vote, also. I think it's our "pleasurable duty" here to attempt to help folks clarify the question through comments, or even through editing the OP, particularly folks who are "beginners" or learning on their own, or for whom English may not be their primary language.

Try:
C#
private void button1_Click(object sender, EventArgs e)
{

    label1.Visible = true;
    label1.Update();
    // label1.Refresh() will work also

    string abcd;
    string efgh;

    // just some stuff to waste some time ...
    for (Int64 x = 0; x < Int64.MaxValue; x++)
    {
        abcd = "abcd" + x.ToString();

        for (Int64 y = 0; y < Int64.MaxValue; y++)
        {
            efgh =  abcd + "efgh" + y.ToString();
        }
    }
}
With using the 'Update, or 'Refresh method on the Label, it is instantly visible: without the use of the 'Update or 'Refresh method: it does not become visible until the "stuff" has completed.

Comment: the code that takes a long time in your button Click EventHandler should probably be run in a separate thread so the UI can stay responsive: unless, of course, you want it to to stay un-responsive. In this case: you may not want the Label to "disappear" until the long-running code is done: but does that mean you want all the rest of the UI to stay un-responsive, that you want to suspend even the end-user's ability to move the Form around by the TitleBar ?

Alternatives to consider: (imho, weak) disable the Label while long-running code is executing to indicate something is happening; and, (imho, much better) a visible "progress bar," or other indicator, that reflects to the user that things are "cooking" in your long-running code.

That may require re-design of your code.
 
Share this answer
 
v4
Comments
_Q12_ 28-Jan-12 22:41pm    
Now I remember using this [Update/Refresh] intensively in DirectDraw for drawing stuff and images. Very interesting turn. Thank you for your code and (all)comments.
I run a test on your code, and now I catch the idea.
----------------------------------------------------------------------
private void button1_Click(object sender, EventArgs e)
{
for (Int64 x = 0; x < Int64.MaxValue; x++)
{
string abcd = "abcd" + x.ToString();
for (Int64 y = 0; y < Int64.MaxValue / 10; y++)
{
label1.Update(); // << here is working right for showing the values.
label1.Text = abcd + "efgh" + y.ToString();
}
}
}
----------------------------------------------------------------------
BillWoodruff 28-Jan-12 22:55pm    
Please keep in mind that normally you'd want to use Update, Refresh ... after ... you've changed the visibility or content of some Control, not before.

And, if you change the text in the Label in the middle of the long-running code, and then use Update or Refresh: yes the Label text will change: but you will notice the rest of the UI will "freeze up" ... and you will slow down your long-running code (depending on what it does), perhaps by a significant amount. The best answer is to use threading on the long-running code with a callback that updates the UI thread at some reasonable interval of time.

We don't, of course, have enough detailed information here to even know how "long-running" your code is: big difference between a delay of 30 seconds and twenty minutes to the end-user !
_Q12_ 28-Jan-12 23:12pm    
I only know to use this from Threading:
----------------------------------------------------------------------
label1.Text = abcd + "efgh" + y.ToString();
label1.Update();
Thread.Sleep(1000); //but is just increment by the second in this case
----------------------------------------------------------------------
The form and other controls are still friezed up as you specified and I noticed before,but I said that I can wait 15-30s to finish its searches.
Can you specify how this (un-freezing)is done? Sorry if I ask too much.
Amir Mahfoozi 29-Jan-12 0:20am    
+5
Please see my comment to the question. Apparently, you don't really understand how even-oriented UI works — just yet. Not to worry…

You need to use threading. Your 15-second method should run in a separate thread; and the UI will be responsive during this period of time. The UI should be notified to hide and show your label when processing is started and when it is finished.

The problem is: you cannot call anything related to UI from non-UI thread. Instead, you need to use the method Invoke or BeginInvoke of System.Windows.Threading.Dispatcher (for both Forms or WPF) or System.Windows.Forms.Control (Forms only).

You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

See also more references on threading:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

—SA
 
Share this answer
 
v2
Comments
_Q12_ 28-Jan-12 19:18pm    
very complex responses.... I do not understand them in more depth, but for the moment what i need is a straight answer. Can you help me in this way? -Please.
Sergey Alexandrovich Kryukov 28-Jan-12 21:25pm    
No, more straight answer is only to write the code for you. No use.

The complex thing is not the answer, but the things themselves.

You need to learn some programming, there is not other way. If you down-vote answers, who will want to help you.

Sorry, but you are welcome to either ask further questions on the matter, or keep doing what you are doing on you own...
--SA
_Q12_ 28-Jan-12 22:11pm    
Sakryukov!
You said this: "..If you down-vote answers..". Come on. I simply react in defending my honor to your first down vote. I do not blindly press buttons because are pretty. I don't like my tail being under shoe because I'm small. If i hurt some feelings there, it's because mine where the same. Now, all are in the past... looking into future.
You are right in one way...i do need/and I want it very much even... to be helped, who don't in my position? And that's why this forum is all about.
The use of straight answers are very obvious, I apply them at my job with much success, and their's effect is this one: -easiness in extrapolation. Of course they do not answer every aspect possible to know about the matter but they are very straight forward in assimilating basic knowledge. And more in the second chapter. :)
Peace man and be prosperous.
[no name] 28-Jan-12 23:33pm    
There is no honor here to defend. This type of attitude and responses, by both, have no place here. The "honorable" thing to have done was not to reply at all.
Sergey Alexandrovich Kryukov 29-Jan-12 0:15am    
Nobody discussed any feelings before your post. If you want to focus on your technical problems and have some particular concerns, we can discuss it; everything else does not look to me to be interesting enough to take care of.
Thank you,
--SA
I assume from the context you want the label to show while the searchForFiles method is processing then be hidden when it completes.

For this behavior you may want to try using DoEvents[^]

C#
private void button1_Click(object sender, EventArgs e)
{
   LabelProcessing.Visible = true;
   Application.DoEvents();
   searchForFiles(); 
   LabelProcessing.Visible = false;
}


Rather than waiting for button click event to finish processing before updating the label this will cause the label events to process while the method is still executing.

BTW, as you have indicated coding is a hobby not a profession, the excepted standard for method names is to use Camel case, such as SearchForFiles, rather than the Pascal case as you have, searchForFiles.
 
Share this answer
 
Comments
_Q12_ 28-Jan-12 20:16pm    
THANK YOU!
1-for your straight forward correct response.
2-for your SUPPORT and understanding.
BTW ...I am conscious this way is not the best one in learning programming, and like me (i suppose) are a lot, who expect straight answers, and not delve (apparently) in too much reading first. In my defend, I read a lot of hard c,c++,c# e-books, tutorials and even "learning softwares", writing down with graphite pencil to faster erase mistakes,etc. I've tried everything, and still, after all that i still don't know everything. For knowing "everything" it's necessary a school, i finish one already, in a opposite direction than programming, im not starting one again, the work have a bad reputation in not let people advance in other directions,usually.
Thank you again Mark. 5+ for you.
;)
[no name] 28-Jan-12 20:56pm    
You don't necessarily need to go to school. There are many great self-taught developers around. A good school will give you a solid foundation but it can't possibly cover everything you'll need to be a good developer. The only way for this is to constantly learn and improve. To that end, often answers given here will not be straight forward but rather an attempt to get the person to learn. When given the answer you don't learn. When asked to think you learn more than what was asked.
Sergey Alexandrovich Kryukov 28-Jan-12 21:32pm    
DoEvents is really, really bad way of doing such things.
Mark, something tells me that you know that very well, too.
Just want to make it "easy" to OP?
--SA
[no name] 28-Jan-12 21:59pm    
Perhaps you would care to elaborate on the "really bad way" or even offer an alternative that meets the needs of the OP rather offer vague statements and criticism to someone trying to learn.

From your responses recently perhaps its time to take a break from the forums
Sergey Alexandrovich Kryukov 29-Jan-12 0:11am    
I understand this needs elaboration, but I though you personally know the disadvantage of this approach...

Thank you,
--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