Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.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# 如果用户没有';t单击消息框_C#_Multithreading - Fatal编程技术网

C# 如果用户没有';t单击消息框

C# 如果用户没有';t单击消息框,c#,multithreading,C#,Multithreading,背景:我想向用户显示一个带有“是/否”按钮的自定义消息框,如果用户单击其中每个按钮,我会将结果返回给调用者。另外,如果用户没有再次显示任何反应,我希望返回第三个结果(使用计时器事件)在word中,经过一段时间后或单击按钮后,该方法(在我的代码中显示)应返回一个值;我想等待它们中的任何一个发生 问题:UI看起来冻结,只有计时器事件触发 代码将在实际项目中使用(带有世系名称!): namespace消息框 { 公共部分类表单2:表单 { 公共表格2() { 初始化组件(); } 私有无效按钮1\u单

背景:我想向用户显示一个带有“是/否”按钮的自定义消息框,如果用户单击其中每个按钮,我会将结果返回给调用者。另外,如果用户没有再次显示任何反应,我希望返回第三个结果(使用
计时器
事件)在word中,经过一段时间后或单击按钮后,该方法(
在我的代码中显示
)应返回一个值;我想等待它们中的任何一个发生

问题:UI看起来冻结,只有
计时器
事件触发

代码将在实际项目中使用(带有世系名称!):

namespace消息框
{
公共部分类表单2:表单
{
公共表格2()
{
初始化组件();
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
Display();
}
}
公共类MyForm:Form
{
公共MyForm()
{
列表_buttonCollection=新列表();
FlowLayoutPanel_flpButtons=新的FlowLayoutPanel();
面板_plFooter=新面板();
_plFooter.Dock=DockStyle.Bottom;
_plFooter.Padding=新的填充(5);
_plFooter.BackColor=Color.FromArgb(41,47,139);
_plFooter.Height=75;
this.FormBorderStyle=FormBorderStyle.None;
this.BackColor=Color.FromArgb(20,37,105);
this.StartPosition=FormStartPosition.CenterScreen;
此填充=新填充(3);
这个。宽度=400;
_flpButtons.FlowDirection=FlowDirection.RightToLeft;
_flpButtons.Dock=DockStyle.Fill;
_plFooter.Controls.Add(_flpButtons);
this.Controls.Add(_plFooter);
按钮btnYes=新按钮();
b点击+=按钮点击;
btnYes.Text=“是”;
按钮btnNo=新按钮();
b否。单击+=按钮单击;
btnNo.Text=“否”;
_按钮收集。添加(btnYes);
_按钮收集。添加(btnNo);
foreach(按钮集合中的按钮btn)
{
btn.ForeColor=颜色。来自argb(170170170);
btn.Font=新系统.Drawing.Font(“Eurostile”,12,FontStyle.Bold);
btn.填充=新填充(3);
btn.FlatStyle=FlatStyle.Flat;
btn.高度=60;
btn.宽度=150;
btn.FlatAppearance.BorderColor=Color.FromArgb(99,99,98);
_flpButtons.Controls.Add(btn);
}
}
静态任务taskA;
私有无效按钮单击(对象发送者,事件参数)
{
_event.Set();
按钮btn=(按钮)发送器;
如果(btn.Text==“是”)
结果=1;
其他的
{
结果=2;
}
这个。关闭();
这个。Dispose();
}
静态自动恢复事件_事件=新自动恢复事件(假);
私有静态MyForm;
公共静态int显示()
{
form=新的MyForm();
StartTimer();
form.Show();
_event.WaitOne();
form.Close();
form.Dispose();
返回结果;
}
私有静态void StartTimer()
{
_定时器=新系统定时器定时器定时器(10000);
_timer.appead+=SetEvent;
_timer.AutoReset=false;
_timer.Enabled=true;
}
专用静态系统.Timers.Timer\u Timer;
公共静态int结果{get;set;}
私有静态void SetEvent(对象源,System.Timers.ElapsedEventArgs e)
{
_timer.Start();
结果=-1;
var timer=(System.Timers.timer)源;
timer.appeased-=SetEvent;
_event.Set();
}
}
}

注意:我知道问题是UI控件是在一个冻结的线程中创建的,但解决方案是什么?

在进程结束时为
MessageBox
创建一个对话框,并显示您要向用户显示的警报


您可以查看来源并在流程结束时使用

消息框
创建一个对话框,并显示要向用户显示的警报


您可以查看来源并与Mok一起发布,我认为这是您想要的。检查答案。@MarcLyon事实上,当我实现这段代码时,我查看了这段代码,首先它使用了我不想使用的钩子(虽然我尝试了,但它使事情变得太复杂),其次,如果UI空闲,SO不需要返回值。Mok,我想这就是你想要的。检查答案。@MarcLyon事实上,当我实现这段代码时,我查看了这段代码,首先它使用了我不想使用的钩子(尽管我尝试了,但它使事情变得太复杂),其次,如果UI空闲,SO不需要返回值。
namespace MessageBox
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            MyForm.Display();
        }
    }
    public class MyForm : Form
    {
        public MyForm()
        {
             List<Button> _buttonCollection = new List<Button>();

            FlowLayoutPanel _flpButtons = new FlowLayoutPanel();
            Panel _plFooter = new Panel();

            _plFooter.Dock = DockStyle.Bottom;
            _plFooter.Padding = new Padding(5);
            _plFooter.BackColor = Color.FromArgb(41, 47, 139);
            _plFooter.Height = 75;
            this.FormBorderStyle = FormBorderStyle.None;
            this.BackColor = Color.FromArgb(20, 37, 105);
            this.StartPosition = FormStartPosition.CenterScreen;
            this.Padding = new Padding(3);
            this.Width = 400;
            _flpButtons.FlowDirection = FlowDirection.RightToLeft;
            _flpButtons.Dock = DockStyle.Fill;
            _plFooter.Controls.Add(_flpButtons);
            this.Controls.Add(_plFooter);
            Button btnYes = new Button();
            btnYes.Click += ButtonClick;
            btnYes.Text = "Yes";
            Button btnNo = new Button();
            btnNo.Click += ButtonClick;
            btnNo.Text = "No";
            _buttonCollection.Add(btnYes);
            _buttonCollection.Add(btnNo);
            foreach (Button btn in _buttonCollection)
            {
                btn.ForeColor = Color.FromArgb(170, 170, 170);
                btn.Font = new System.Drawing.Font("Eurostile", 12, FontStyle.Bold);
                btn.Padding = new Padding(3);
                btn.FlatStyle = FlatStyle.Flat;
                btn.Height = 60;
                btn.Width = 150;
                btn.FlatAppearance.BorderColor = Color.FromArgb(99, 99, 98);
                _flpButtons.Controls.Add(btn);
            }


        }
        static Task taskA;
        private  void ButtonClick(object sender, EventArgs e)
        {
            _event.Set();
            Button btn = (Button)sender;
            if (btn.Text == "Yes")
                Result = 1;
            else
            {
                Result = 2;
            }
            this.Close();
            this.Dispose();
        }
        static AutoResetEvent _event = new AutoResetEvent(false);
        private static MyForm form;
        public static int Display()
        {
            form = new MyForm();
            StartTimer();
            form.Show();
            _event.WaitOne();
            form.Close();
            form.Dispose();
            return Result;
        }

        private static void StartTimer()
        {
            _timer = new System.Timers.Timer(10000);
            _timer.Elapsed += SetEvent;

            _timer.AutoReset = false;
            _timer.Enabled = true;
        }

        private static System.Timers.Timer _timer;
        public static int Result { get; set; }

        private static void SetEvent(Object source, System.Timers.ElapsedEventArgs e)
        {
            _timer.Start();
            Result = -1;
            var timer = (System.Timers.Timer) source;
            timer.Elapsed -= SetEvent;
            _event.Set();
        }
    }
}