-3

My form transition is slow when I click button, I am using thread to have a form effect that form opacity starts from 0.1 and increase the number. Then I have a method and start the method from Form_Load in thread.

private void RunTimer_Tick_Things()
    {
        if (flag)
        {
            while (this.Opacity <= cs.CheckMaxOpacityValue())
            {
                Thread.Sleep(cs.GetTimerSleepNumberToIncreaseOcacity());
                if (this.Opacity == cs.CheckMaxOpacityValue())
                {
                    thrdTimer.Abort();
                    break;
                }
                this.Opacity += cs.GetIncreasedOpacityValue();

            }

        }
        else
        {
            while (this.Opacity >= cs.CheckMinOpacityValue())
            {
                Thread.Sleep(cs.GetTimerSleepNumberToDecreaseOpacity());
                this.Opacity -= cs.GetDecreasedOpacityValue();
            }

            thrdTimer.Abort();

        }
    }

And I have button in this form to open another form. Like this

private void button2_Click(object sender, EventArgs e)
    {
        Form2DatabaseSetup frm2 = new Form2DatabaseSetup();
        StopThread();
        this.Hide();
        frm2.Show();
        flag = false;

    }

My problem is when I click this button the second form is opening slowly. Consider like, you click the button then the first form hides and waiting for 1,5 second then the second form opens. Note: The second form has thread and same functions.

Does Anyone has experienced it or know , has a knowledge about this case?

dhh
  • 4,289
  • 8
  • 42
  • 59
Emrullah
  • 122
  • 1
  • 13
  • You might be interested in the answers on this question as well: http://stackoverflow.com/questions/12497826/better-algorithm-to-fade-a-winform – Sabre Jun 30 '15 at 15:22
  • Are you using WinForms or WPF? – Alexander Bell Jun 30 '15 at 15:22
  • Start using WPF ... stuff like this is a lot easier. It's probably opening slowly because the UI thread is animating the opacity as well as showing the new form. – 3-14159265358979323846264 Jun 30 '15 at 15:22
  • how much time you are passing to thread sleep function? – Liran Jun 30 '15 at 15:24
  • @AlexBell , No standard windows form – Emrullah Jun 30 '15 at 15:29
  • @Liran, The passing time is 15 milisecond to thread sleep – Emrullah Jun 30 '15 at 15:30
  • @Emrullah you mean Thread.Sleep(15); right? if so can you please add the rest of the ( relevent ) code so we can get the whole picture – Liran Jun 30 '15 at 15:33
  • @3-14159265358979323846264, Actually I cannot start to use WPF because I have 22 form. And I inherit the forms from my master form class which I created. – Emrullah Jun 30 '15 at 15:34
  • @Liran , yes Liran, Thread.Sleep(15); `while (this.Opacity <= 1.0) { Thread.Sleep(cs.GetTimerSleepNumberToIncreaseOcacity()); if (this.Opacity == 1.0) { thrdTimer.Abort(); break; } this.Opacity += 0.06; }` – Emrullah Jun 30 '15 at 15:36
  • Which thread is `RunTimer_Tick_Things` running in? What type of timer are you using? Note there are only a very few cases where `Thread.Sleep` is actually a legitimate thing to do. If you are using `Thread.Sleep` chances are you are doing it wrong. If your timer is being called at some interval, there should be no reason to need to `Sleep`. – Matt Burland Jun 30 '15 at 15:40
  • @MattBurland, thank. I was using timer. For now I am not. When I was using timer, I was not using thread. Then I deleted the timer and used the thread instead of timer. The function name was from timer. I just changed the inside of function, not function name. – Emrullah Jun 30 '15 at 15:44
  • That's your problem then. You can't `Sleep` the UI thread and expect the UI thread to do anything while it's sleeping. That's not what `Sleep` means. – Matt Burland Jun 30 '15 at 15:46
  • So you say the problem is because of the `Thread.Sleep` right. Sorry because I am confusing and I a few months passed that I coding in c# – Emrullah Jun 30 '15 at 15:48
  • @Sabre , Why is it better Sabre. It is similiar to my code. – Emrullah Jun 30 '15 at 15:52

1 Answers1

0

at first i thought you are increasing opacity by 0.01 so 15 * 100 =1.5 exactly what you described, but after you send the values i can see the increase is 0.06, so i think you have some problems with all the function you have ( GetTimerSleepNumberToIncreaseOcacity, GetIncreasedOpacityValue ) .

try instead of the functions to work with hard coded values and work your way up,

i use this code and it works fine:

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

    private void button1_Click(object sender, EventArgs e)
    {
        var f = new Form2();
        f.Show();
        new Thread(() =>
        {
            while (f.Opacity < 1)
            {
                Thread.Sleep(15);
                //if (this.Opacity == cs.CheckMaxOpacityValue())
                //{
                //    thrdTimer.Abort();
                //    break;
                //}
                f.Invoke((Action)delegate { f.Opacity += 0.06; });

            }

        }).Start();
    }
}
Liran
  • 591
  • 3
  • 13
  • Thanks @Liran , I tried it now and I figured out my other problem which I did not write. Thanks and the form waiting is still going on. I can't vote your comment because I don't have 15 reputation. – Emrullah Jun 30 '15 at 16:14
  • its ok as long as you found the problem – Liran Jul 01 '15 at 07:27