2
System.Threading.Timer를 사용하고 타이머 틱 이벤트에서 Windows 양식을 초기화하면 양식이 응답하지 않게됩니다. 왜 그런가요? 어떻게 피할 수 있습니까?C# System.Threading.Timer tick에서 양식 만들기가 실패합니다.
이 간단한 샘플 코드는 문제를 보여줍니다. 두 개의 첫 번째 창 ("원본"및 "수동")은 정상적으로 작동하지만 "타이머"는 즉시 응답하지 않습니다. 그와 관련된 이벤트 루프가없는 즉 하나 -
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace WindowsFormsApplication1.Forms
{
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
}
private void frmMain_Load(object sender, EventArgs e)
{
this.Text = "Original";
this.Left = 0;
Form f = new Form();
f.Text = "Manual";
f.Show();
f.Left = this.Width;
TimerCallback tCallback = new TimerCallback(Timer_Tick);
System.Threading.Timer timer = new System.Threading.Timer(tCallback, null, 1000, System.Threading.Timeout.Infinite);
}
void Timer_Tick(object o)
{
Form f = new Form();
f.Text = "Timer";
f.Show();
f.Left = this.Width * 2;
}
}
}
필요 없어, 나는 system.windows.forms 타이머로 바꿨다. 방금 다른 프로젝트에서 사용했기 때문에 system.threading.timer를 사용하고있었습니다. 이번에는 정말로 생각하지 않았습니다. 감사! –
오른쪽 - 저에게 직업을 저장합니다 :) –