-1

how to run single winform inside project with many winforms without running entire project?

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;
    }
}

}

for winform without connection to database, i do this by using the Immidiate Window: Immidiate Window

but when i put my code with entity-framework connection (uncommenting EF Part), it gives me an error Immidiate Window with error

A first chance exception of type 'System.Threading.ThreadAbortException' occurred in EntityFramework.dll Evaluation requires a thread to run temporarily. Use the Watch window to perform the evaluation.

many thanks,

ps: i am using: - visual studio CE 2013 - c# winforms with .net version 4.5 - windows 7

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
handoko
  • 3
  • 2
  • change your startup form? – Jeremy Jan 10 '17 at 16:18
  • Is there a specific property that is throwing the exception? Try calling `Debugger.NotifyOfCrossThreadDependency();` in the get method of the property, right before the value is returned – Peter Jan 10 '17 at 16:20
  • Possible duplicate of [Evaluation requires a thread to run temporarily. Use the Watch window to perform the evaluation](http://stackoverflow.com/questions/4280604/evaluation-requires-a-thread-to-run-temporarily-use-the-watch-window-to-perform) – Yusril Maulidan Raji Jan 10 '17 at 16:27
  • @Jeremy you mean by change to code in Program.cs like Nick Allan pointed out? i have try this, but if i can run this from Immidiate Window it will be much nicer. thx. – handoko Jan 10 '17 at 16:29
  • @YusrilMaulidanRaji yes, i have read that post before and tried to change the debugger setting but with no luck. – handoko Jan 10 '17 at 16:31
  • @Peter i just create a winform, design the control, and run using Immideate window, this works fine. but after i add the EF part, when i try to run from Immidiate Window it throws the exeption on Immidiate Window. but when i run the project normally (login form, then click 2 more forms), or change this form as a startup form, it works without any error. – handoko Jan 10 '17 at 16:38
  • The error seems to originate from your Entity Framework `DbContext` class. I do not think this is related to the form or the control. – Peter Jan 10 '17 at 16:43
  • @Peter yes, i think when EF start, it kinda need some thread that does not provided by Immidiate Window. I wonder if there is any tweak for this. – handoko Jan 10 '17 at 16:53
  • Where is this exception thrown? Please show the relevant _code_ for the method or property which causes this exception. – Peter Jan 10 '17 at 17:01
  • @Peter i have updated my question with additional code and screen capture. thx. – handoko Jan 10 '17 at 17:23

2 Answers2

1

You could add a temporary call to your form from your Program.cs like so...

    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());
            }
        }
    }
Nick Allan
  • 387
  • 4
  • 10
0

I recommend adding a method like this to your HMERPEntities class, where SomeEntityType is replaced by your actual "warna" object type:

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

    return warna.ToList();
}

Then, in Program.cs edit the _initializeData() to use that method instead of accessing the "warna" property directly:

var queryDaftarSemuaWarna = context.GetWarna();
Peter
  • 674
  • 6
  • 14
  • i have try this, but it raise the same error. I am going to go with Nick Allan and Jeremy answer because it is the simplest one. thx. – handoko Jan 10 '17 at 18:09