2017-01-11 30 views
0

我有一個應用程序,其中一個類我正在初始化Windows FormSystem.Threading.Timer問題與System.Threading.Timer和模式ShowDialog()

如果timer,這使一些IPC基礎的東西檢查,遇到一個特定的值,它標誌着在同一個班級,然後更早初始化的對話框上調用ShowDialog()一個event

不幸的是,這個ShowDialog()Modal,停止計時器。

我的印象是System.Threaded.Timer是在與調用線程不同的線程中創建的,因此Timer將繼續在後臺運行。

編輯 - 一些代碼

public delegate void EventHandler(); 
class someClass 
{ 
    WrapperForm dlg = null; 
    public void CallToChildThread(Object stateInfo) 
    { 
     AutoResetEvent autoEvent = (AutoResetEvent)stateInfo; 
     //Check IPC 
     //Fire event 
     _show.Invoke(); 
    } 
    public someClass() 
    { 
     public static event EventHandler _show; 
     initializeDialog(); // Initialize the dialog. Standard new 
     var autoEvent = new AutoResetEvent(false); 
     var stateTimer = new System.Threading.Timer(CallToChildThread, 
            autoEvent, 1000, 250); 
     _show += new EventHandler(eventCheck); 
    } 
    void eventCheck() 
    { 
     //If some condition 
     dlg.ShowDialog(); //Timer stops 
    } 
} 

這怎麼可能解決?

+1

如果您可以讓我們看看您嘗試過的(某些代碼)會很棒。 – EpicKip

+0

模態不會爲我停止計時器o.O – EpicKip

+0

添加了一些代碼。 – user1173240

回答

2

該類System.Threading.Timer有一個已知問題,如果您不保留對它的引用,即使在運行時也可以由垃圾回收器收集。

切換到使用System.Timers.Timer這只是一個圍繞System.Threading.Timer的包裝或保留對定時器的引用,以便GC不會收集它並取消您的計時器。

public delegate void EventHandler(); 
class someClass 
{ 
    WrapperForm dlg = null; 
    System.Threading.Timer stateTimer; 
    public static event EventHandler _show; 

    public void CallToChildThread(Object stateInfo) 
    { 
     AutoResetEvent autoEvent = (AutoResetEvent)stateInfo; 
     //Check IPC 
     //Fire event 
     _show.Invoke(); 
    } 
    public someClass() 
    { 
     initializeDialog(); // Initialize the dialog. Standard new 
     var autoEvent = new AutoResetEvent(false); 
     stateTimer = new System.Threading.Timer(CallToChildThread, 
            autoEvent, 1000, 250); 
     _show += new EventHandler(eventCheck); 
    } 
    void eventCheck() 
    { 
     //If some condition 
     dlg.ShowDialog(); 
    } 
} 
+0

啊,這是priblem。用'System.Timers.Timer'代替它就行了。謝謝。 「System.Threading.Timer」的問題通常是否已知? – user1173240

1

與您的代碼我改變了我的例子中,我使用定時器現在(我還是用我的計時器,所以我可以看到其他計時器仍在運行)

public partial class Form1 : Form 
    { 
     Form frm1 = new Form(); 
     int i; 
     private System.Threading.Timer t; 
     //If the problem is garbage collecting then the line above is very important. 
     public Form1() 
     { 
      InitializeComponent(); 
      var autoEvent = new AutoResetEvent(false); 
      var stateTimer = new System.Threading.Timer(CallToChildThread, 
            autoEvent, 1000, 250); 
     } 
    private void CallToChildThread(object state) 
    { 
     i++; 
     //Updating value here, update in other timer (this is to avoid crossthreadEx) 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     frm1.ShowDialog(); 
     //Label keeps updating! 
    } 

    private void timer1_Tick(object sender, EventArgs e) 
    { 
     label1.Text = i.ToString(); 
    } 
} 

它仍然對我的作品,原因可能是因爲我沒有讓定時器變成局部變量,而是在類中聲明它。

+0

您正在爲您的測試使用錯誤類型的計時器,System.Threading.Timer沒有'Tick'事件,它使用委託回調。 –

+0

我已經添加了一些代碼。 System.Threading.Timer沒有Enabled屬性。 – user1173240