Home > Net >  Under the winform model update other Windows form after the show
Under the winform model update other Windows form after the show

Time:09-21

In the main window Form1 winform with a timer timing update Form1 controls, and click on the button in the main interface after Form2 patterns through window shows another form, also have a timer under Form2 update Form2 interface controls, found in the model window when Form2 Form1 controls also in the update,
Form1 code is as follows:
 System. Windows. Forms. The Timer Timer=new System. Windows. The Forms, the Timer (); 
Int ii=0;
Public _click ()
{
InitializeComponent();
}

Private void button1_Click (object sender, EventArgs e)
{
String MMM=System. Threading. Thread. CurrentThread. ManagedThreadId. The ToString ();
The timer. The Interval=1000;
The timer. Tick +=Timer_Tick;
The timer. The Start ();
}

Private void Timer_Tick (object sender, EventArgs e)
{
Ii++;
TextBox1. Text=ii. ToString () + "\ t Thread=" + Thread. The CurrentThread. ManagedThreadId. The ToString ();
}

Private void button2_Click (object sender, EventArgs e)
{
Form2 Form2=new Form2 ();
DialogResult DialogResult=form2. ShowDialog ();
If (dialogResult==dialogResult. OK)
{
Form2. The Dispose ();
}
}

Form2 code with Form1 is basically the same, except Form2 window display mode,
It is strange that when I click on the button in the Form1, UI thread is parked DialogResult DialogResult=form2. ShowDialog (), see said when use ShowDialog, will create an internal message loop, I want to ask is the main window controls in the Form1 update is done in the middle of internal message loop, rather than in Form1 done in its own external message loop?

CodePudding user response:

Modal message pump (message loop) to send and also process the message, such as codeproject posts demonstration, timer, will also get delivery processing,
(https://www.codeproject.com/Articles/348/Implementing-Modal-Message-Loops)

 BOOL fDone; 
INT nResult;

Int RunModalWindow (
The HWND hwndDialog,
HWND hwndParent)
{
If (hwndParent!=NULL)
The EnableWindow (hwndParent, FALSE);

MSG MSG.

For (fDone=FALSE; FDone; WaitMessage ())
{
While (PeekMessage (& amp; MSG, 0, 0, PM_REMOVE))
{
If (MSG) message==WM_QUIT)
{
FDone=TRUE;
PostMessage (NULL, WM_QUIT, 0, 0);
break;
}

if(! IsDialogMessage (hwndDlg, & amp; MSG))
{
TranslateMessage (& amp; MSG);
DispatchMessage (& amp; MSG);
}
}
}

If (hwndParent!=NULL)
The EnableWindow (hwndParent, TRUE);

The DestroyWindow (hwndDialog);

Return nResult;
}

CodePudding user response:

Winform timer and the difference between the timer Thread right here,
With the thread of the timer is not directly update interface,
Don't have to invoke and winform timer and so on
  •  Tags:  
  • C#
  • Related