2015-02-09 109 views
-1

我有一个使用其他类(在seprated DLL)中的winform应用程序 它看起来像:C#呼叫类失败

public Form1() 

    { 

     foo = new Foo(2500, this.refreshGrid); 
    } 
    private void refreshGrid(List<int> source) 
    { 
     dgvDrivers.AutoGenerateColumns = true; 
     dgvDrivers.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells; 

     dgvDrivers.SynchronizedInvoke(() => dgvDrivers.DataSource = source); 
     dgvDrivers.AutoResizeColumns(); 
    } 

和扩展方法:

public static void SynchronizedInvoke(this ISynchronizeInvoke sync, Action action) 
    { 
     // If the invoke is not required, then invoke here and get out. 
      if (!sync.InvokeRequired) 
      { 
       // Execute action. 
       action(); 

       // Get out. 
       return; 
      } 

      // Marshal to the required context. 
      sync.Invoke(action, new object[] { }); 
     } 

现在我有一个初始化新System.Threading.Timer在specfic时间运行一次类:

public CreateTimer(int dueTime) 
{ 
     new System.Threading.Timer(SchudleStatusChange, null, dueTime, System.Threading.Timeout.Infinite); 
    } 

回到用户界面 - 有按钮,每次点击调用CreateTimer()10次 但定时器调用次数少于我的预期。 我想这是使用winforms和调用主线程的事情。

任何ideads?

回答

3
+0

OK,但是当我叫从控制台应用程序相同的代码这类工作得很好... – User1234 2015-02-09 18:24:41

+2

所以这表明GC没有在您的控制台上运行。垃圾收集发生在不确定的时间,取决于内存负载等。 – spender 2015-02-09 18:43:05

+0

感谢您的快速回复! :) – User1234 2015-02-09 18:58:35