Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 形式转换很慢_C#_Multithreading_Forms - Fatal编程技术网

C# 形式转换很慢

C# 形式转换很慢,c#,multithreading,forms,C#,Multithreading,Forms,我的表单转换很慢,当我点击按钮时,我使用线程产生一个表单效果,表单不透明度从0.1开始,并增加数字。然后我有了一个方法,并从线程中的Form_Load开始该方法 private void RunTimer_Tick_Things() { if (flag) { while (this.Opacity <= cs.CheckMaxOpacityValue()) { Thre

我的表单转换很慢,当我点击按钮时,我使用线程产生一个表单效果,表单不透明度从0.1开始,并增加数字。然后我有了一个方法,并从线程中的Form_Load开始该方法

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();

        }
    }
我的问题是,当我点击这个按钮时,第二个窗体正在缓慢打开。 考虑一下,单击按钮,然后第一个窗体隐藏并等待1,5秒,然后打开第二个窗体。 注意:第二种形式有线程和相同的函数


有没有人经历过或者知道,有关于这个案例的知识?

起初我以为你的不透明度增加了0.01,所以15*100=1.5,这正是你所描述的,但是在你发送值后,我可以看到增加了0.06,所以我想你的所有功能都有一些问题(GetTimerSleepnumbertoIncreaseOCity,GetIncreasedOpacityValue)

尝试使用硬编码的值,而不是函数,然后逐步递增

我使用此代码,它工作正常:

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();
    }
}
公共部分类表单1:表单
{
公共表格1()
{
初始化组件();
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
var f=新的Form2();
f、 Show();
新线程(()=>
{
而(f.不透明度<1)
{
睡眠(15);
//if(this.Opacity==cs.CheckMaxOpacityValue())
//{
//thrdTimer.Abort();
//中断;
//}
f、 调用((操作)委托{f.Opacity+=0.06;});
}
}).Start();
}
}

您可能也对这个问题的答案感兴趣:您使用的是WinForms还是WPF?开始使用WPF…这样的东西会容易得多。它可能打开得很慢,因为UI线程正在为不透明度设置动画并显示新表单。您要花多少时间来执行线程睡眠功能?@AlexBell,没有标准窗口FormThank@Liran,我现在试过了,我发现了我没有写的另一个问题。谢谢,等待的表格还在继续。我不能投票给你的评论,因为我没有15个声誉。只要你发现问题就行
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();
    }
}