2017-01-10 31 views
-1

如何在沒有運行整個項目的情況下在多個winforms項目中運行單個winform?以多種形式在項目內部調試單個winform

using System.Data; 
using System.Drawing; 
using System.Linq; 
using Syncfusion.Windows.Forms; 

namespace Desktop 
{ 
public partial class WarnaForm : MetroForm 
{ 
    private DataTable table; 

    public WarnaForm() 
    { 
     InitializeComponent(); 
     _initializeData(); 
     _initializeGridGrouping(); 

    } 

    private void _initializeData() 
    { 
     table = getTable(); 
     /* 
     using (HMERPEntities context = new HMERPEntities()) 
     { 
      var queryDaftarSemuaWarna = context.warna; 

      foreach (var w in queryDaftarSemuaWarna.ToList()) 
      { 
       DataRow rowNew = table.NewRow(); 
       rowNew["No"] = w.warna_id; 
       rowNew["Nama"] = w.nama; 
       table.Rows.Add(rowNew); 
       table.AcceptChanges(); 
      } 
     }*/ 
    } 

    public DataTable getTable() 
    { 
     DataTable table = new DataTable(); 
     table.Columns.Add("No", typeof (long)); 
     table.Columns.Add("Nama", typeof (string)); 

     return table; 

    } 

    private void _initializeGridGrouping() 
    { 
     gridGroupingControl1.DataSource = table; 
    } 
} 

}

對於沒有連接到數據庫的winform,我這樣做是通過使用Immidiate窗口: Immidiate Window

但是當我把我的代碼與實體框架連接(取消註釋EF部分) ,它給了我一個錯誤 Immidiate Window with error

類型的第一次機會異常'System.Threading.ThreadAbortException'發生在 EntityFramework.dll評估需要一個線程臨時運行。 使用觀察窗口執行評估。

千恩萬謝,

PS: 我使用: - 視覺工作室CE 2013 - C#和.NET版本的WinForms 4.5 - Windows 7的

+0

更改您的啓動窗體? – Jeremy

+0

是否存在拋出異常的特定屬性?嘗試在返回值之前在屬性的get方法中調用'Debugger.NotifyOfCrossThreadDependency();' – Peter

+0

[Evaluation可能重複需要一個線程臨時運行。使用Watch窗口執行評估](http://stackoverflow.com/questions/4280604/evaluation-requires-a-thread-to-run-temporarilyuse-the-watch-window-toper-perform) –

回答

1

你可以添加到一個臨時電話從Program.cs的表單是這樣的...

using System; 
    using System.Collections.Generic; 
    using System.Linq; 
    using System.Threading.Tasks; 
    using System.Windows.Forms; 

    namespace WindowsFormsApplication1 
    { 
     static class Program 
     { 
      /// <summary> 
      /// The main entry point for the application. 
      /// </summary> 
      [STAThread] 
      static void Main() 
      { 
       Application.EnableVisualStyles(); 
       Application.SetCompatibleTextRenderingDefault(false); 

       //Temporarily add this... 
       Application.Run(new frmYourForm()); 

       Application.Run(new frmMain()); 
      } 
     } 
    } 
0

我RECO mmend加入這樣的方法你HMERPEntities類,其中SomeEntityType是由您的實際「warna」對象類型替換:Program.cs中編輯

public List<SomeEntityType> GetWarna() { 
    System.Diagnostics.Debugger.NotifyOfCrossThreadDependency(); 

    return warna.ToList(); 
} 

然後,_initializeData()使用該方法,而不是訪問的「warna」屬性直接:

var queryDaftarSemuaWarna = context.GetWarna(); 
+0

很酷。去嘗試這個並回復你。 – handoko

+0

我已嘗試此操作,但會引發同樣的錯誤。我將與尼克艾倫和傑里米一起回答,因爲它是最簡單的一個。謝謝。 – handoko