如果调用的MessageBox.show计时器只是工作()计时器、工作、MessageBox、show

2023-09-05 00:22:54 作者:爷ミ淫领风骚

它是如何可能的?为什么呢?

我有一个被称为如果没有网络连接定时器,如方法下:

 公共无效美孚(){
     的for(int i = 0,数= MailList.CheckedItems.Count; I<计数;我++){
         / *检查计算机网络可用连接
              公共BOOL HasConnection(){
                返回System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable();
             }
         * /
         如果(!net.HasConnection()){
              SearchNetworkConnection.Start(); //启动定时器
         }
     }
}
 

定时器 _Tick 方法:

 私人无效SearchNetworkConnection_Tick(对象发件人,EventArgs的){
            ++ ATTEM preCONNECT;

            字符串currentState的=学尝试重新连接。;
            的MessageBox.show(currentState的,..,MessageBoxButtons.OK,MessageBoxIcon.Warning);

            如果(ATTEM preCONNECT> = ATTEM preCONNECTLIMIT){
                //做中止所有进程
                SearchNetworkConnection.Stop();
            }
      }
 

这工作很奇怪,只是如果我叫了的MessageBox.show() SearchNetworkConnection.Start()

多工计时器app下载 多工计时器下载安装中文版 v2.9.2安卓版

在换句话说,这是行不通的,定时器将不会运行:

 如果(!net.HasConnection()){
        SearchNetworkConnection.Start();
   }
 

调用的MessageBox.show(),它工作正常:

 如果(!net.HasConnection()){
        SearchNetworkConnection.Start();
        的MessageBox.show(笑);
 }
 

如果它是有用的,美孚()方法上线运行。

我希望这是显而易见的。任何帮助是非常AP preciated,预先感谢! :)

更新

所以......我认为这是一个有点怪。我写了一个简单的code的一些测试。我很惊讶的是,这个错误继续下去。 向下code正常工作。但如果你更改顺序

  timer.Start();
DialogResult的结果=的MessageBox.show(文字,标题);
 

  DialogResult的结果=的MessageBox.show(文字,标题);
timer.Start();
 

它不工作。定时器不启动。

 公共静态的DialogResult显示(文本字符串,字符串标题,INT dellay)
        {

            定时器定时=新的Timer();
            timer.Interval = dellay;
            timer.Start();
            DialogResult的结果=的MessageBox.show(文字,标题);
            timer.Tick + =新的EventHandler(委托
             {
                    IntPtr的手柄=的FindWindow(NULL,标题);

                    如果(处理!= IntPtr.Zero)
                    {
                        IntPtr的HRESULT = SendMessage函数(手柄,WM_CLOSE,IntPtr.Zero,IntPtr.Zero);

                        如果(HRESULT == IntPtr.Zero)
                        {
                            timer.Stop();
                            timer.Dispose();
                        }
                    }
            });

            返回结果;
        }
 

解决方案

您计时器需要messagepump运行。的MessageBox.show()被提供的。

但你要避免提示消息框共(看Systems.Diagnostsics.Debug.Print())。

您或许应该看看其他定时器(的System.Threading,System.Timers)。

第2部分

您指出美孚()在一个线程中运行。这听起来确定。 但是,你的Windows.Forms.Timer需要一个MessageBox发挥作用的事实意味着你以某种方式阻止你的主线程。所以,你的问题是不是在贴code,但在其他地方。

how it's possible? why?

I have a timer that is called if have no network connection,as in method down:

public void Foo() {
     for (int i = 0, count = MailList.CheckedItems.Count; i < count; i++) {
         /* Check for network available connection in computer
              public bool HasConnection() {
                return System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable();
             }       
         */
         if (!net.HasConnection()) { 
              SearchNetworkConnection.Start(); //start the timer 
         }
     }
}

and The _Tick method of Timer:

   private void SearchNetworkConnection_Tick(object sender, EventArgs e) {
            ++ATTEMPRECONNECT;

            string currentState = "attemp reconnect.."; 
            MessageBox.Show(currentState, "..", MessageBoxButtons.OK, MessageBoxIcon.Warning);

            if (ATTEMPRECONNECT >= ATTEMPRECONNECTLIMIT) {
                //do abort all process 
                SearchNetworkConnection.Stop();
            }
      }

That works oddly, just if I call the MessageBox.Show() after SearchNetworkConnection.Start().

In other words, it does not work, the Timer won't run:

if (!net.HasConnection()) { 
        SearchNetworkConnection.Start();
   }

calling MessageBox.Show(), it works fine:

if (!net.HasConnection()) { 
        SearchNetworkConnection.Start();
        MessageBox.Show("lol");
 }

if it can be useful, Foo() method run on thread.

I hope this is clear. Any help is very appreciated, thank in advance! :)

UPDATE

so.. I think it's a little strange. I wrote a simple code for some tests. and I'm surprise,the mistake continue. the down code works fine. but if you do change the order

timer.Start();
DialogResult result = MessageBox.Show(text, caption);

to

DialogResult result = MessageBox.Show(text, caption);
timer.Start();

it not works. the timer does not start.

public static DialogResult Show(string text, string caption,int dellay)
        {

            Timer timer = new Timer();
            timer.Interval = dellay;
            timer.Start();
            DialogResult result = MessageBox.Show(text, caption);
            timer.Tick += new EventHandler(delegate
             {
                    IntPtr handle = FindWindow(null, caption);

                    if (handle != IntPtr.Zero)
                    {
                        IntPtr hresult = SendMessage(handle, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);

                        if (hresult == IntPtr.Zero)
                        {
                            timer.Stop();
                            timer.Dispose();
                        }
                    }
            });

            return result;
        }

解决方案

Your Timer needs a messagepump to run. MessageBox.Show() is providing one.

But you'll want to avoid messageboxes altogether (look at Systems.Diagnostsics.Debug.Print()).

You should probably take a look at other timers (System.Threading, System.Timers).

Part 2

You state that Foo() runs on a Thread. That sounds OK. But the fact that your Windows.Forms.Timer needs a MessageBox to function means that you are somehow blocking your main thread. So your problem is not in the posted code but somewhere else.