-3

Well, it is simple, Creat a window form, put in it a button and a label, and give the button a click event.

    private void button1_Click(object sender, EventArgs e)
    {
        int xa;
        int ya;
        for (xa = 647; xa < 982; xa++)

            for (ya = 262; ya < 598; ya++)
            {
                label1.Text = xa.ToString() + " " + ya.ToString();
            }
    }

and program just stuck about 20 seconds when i click the button. How can i fix this?

Tranoze
  • 27
  • 1
  • 5
  • This happens because you're doing a long task on the UI thread. Look into `BackgroundWorker`, `async` and `StringBuilder` as a bonus. – SimpleVar Jun 20 '15 at 01:31
  • Your question as it stands now is "if you run code that takes some time and repeat multiple times it takes long time, how to fix it?" You may want to clarify what you actually want to achieve... – Alexei Levenkov Jun 20 '15 at 01:33
  • I dont see what i put in button is a long task, it just a 300x300 run, but what i suppose to see in this program is that y increase immediately while program is running, not when it complete. – Tranoze Jun 20 '15 at 01:33
  • 1
    updating the textbox is the long running task not the loop – RadioSpace Jun 20 '15 at 01:34
  • Actually concatenating the strings might even take most of the time. Working with strings is very costly, in the naive methods. – SimpleVar Jun 20 '15 at 01:35
  • so how can i wait until the textbox is updated instead of running while the textbox is updating? – Tranoze Jun 20 '15 at 01:35
  • @YoryeNathan thats not enough characters to be slow – RadioSpace Jun 20 '15 at 01:39
  • @Tranoze if you want to display a list. collect the answers then display. if you want it to tick up then see the first answer. – RadioSpace Jun 20 '15 at 01:41
  • 1
    @Tranoze you really should update your question with what you want to see as result... And run some basic estimations - i.e. to see 300*300 different pairs even assuming you can distinguish every frame at 60FPS will take 300*300/60 = 1500 seconds ... – Alexei Levenkov Jun 20 '15 at 01:42
  • @Tranoze label is not designed to store intermendiate computation results. Create a string, then assign it as whole to label. A similar issue was found in http://stackoverflow.com/a/21379663/125562 – Basilevs Jul 20 '15 at 03:54

1 Answers1

1

You've got to get it off of the UI thread. Try this:

private void button1_Click(object sender, EventArgs e)
{
    ThreadPool.QueueUserWorkItem(p => doit());            
}

private void doit()
{
    int xa;
    int ya;
    for (xa = 647; xa < 982; xa++)

        for (ya = 262; ya < 598; ya++)
        {
            this.Invoke(new Action(() => { label1.Text = xa.ToString() + " " + ya.ToString(); }));
        }
}
sarme
  • 1,337
  • 12
  • 19
  • 1
    The only thing that this change does is to make it even slower due to constant callbacks between threads... Not sure how helpful that would be... – Alexei Levenkov Jun 20 '15 at 01:34
  • @AlexeiLevenkov - It's slower, true. Thread marshaling is not cheap. It depends on what the goal is. I assumed the OP was trying to deal with the unresponsive UI while it was processing. – sarme Jun 20 '15 at 01:36
  • Small sleep on worker thread would give UI thread chance to render... Clearly not the best solution (should be using timer), but ok. – Alexei Levenkov Jun 20 '15 at 01:38
  • Yes, it slower, but i is good as my goal that program will wait ultil the label is updated. – Tranoze Jun 20 '15 at 01:38
  • Is this updating label control in UI context, doesn't looks like and if that's true then it would be an exception, or this is what this.Invoke does, I am not sure – Mrinal Kamboj Jun 20 '15 at 01:42
  • @MrinalKamboj check out help on `Control.Invoke` to confirm that this code is correct. – Alexei Levenkov Jun 20 '15 at 01:44
  • Why not use Aysnc - Await pattern for this, why use threadpool threads for doing this calculation, which will be an overhead – Mrinal Kamboj Jun 20 '15 at 01:46
  • @MrinalKamboj Async would probably work. I'm not familiar enough with it to tell you if it's better or not. – sarme Jun 20 '15 at 01:50
  • how is async less overhead. isn't that syntatic sugar? – RadioSpace Jun 20 '15 at 01:51
  • Async - Await is 100% better, since that does not involve any extra thread, it is just a asynchronous call back mechanism, so there's no overhead, it will do all the calculations and result can updated in label once it returns – Mrinal Kamboj Jun 20 '15 at 01:53
  • @MrinalKamboj you should post your own answer... Note that it is *hard* to achieve what you said (no extra thread) to start with and additionally not block the only UI thread you suggest to run code on... – Alexei Levenkov Jun 20 '15 at 03:22
  • I will try to create a code and post, even otherwise why use Threadpool directly why not Task parallel library – Mrinal Kamboj Jun 20 '15 at 07:42