简体   繁体   中英

Using multiple Projects in NotifyIcon Application

So I have the weirdest of all problems. I just wanted to write a simple c# WinForms tool for personal use when I ran into it. I`ll begin at the very start, the real question will be at the very end.

So I made a NotifyIcon Application. This is very simple, I just did:

public class MyApp: ApplicationContext
{
    NotifyIcon icon;
    public MyApp()
    {
        // ...
    }

    // ...
}

And start the App just as every other WinForms App:

Application.Run(new MyApp());

Now I have added a new MyApp.Data Class Project, which contains a Local Database, and used the edmx helper to generate my EF Models. I have added a static class called Database which should contain all my Queries.

public static class DataBase
{
    static MyAppModel db = new MyAppModel();

    public static void AddEntry(SomeThing st) 
    {
        db.SomeThings.AddObject(st);
        db.SaveChanges();
    }

    public static String[] GetSomeThings(int numberOfEntries)
    {
        return db.SomeThings.OrderBy(x => x.Date).Select(x => x.Title).Take(numberOfEntries).ToArray();
    }
}

Back in the WinForms NotifyIcon Project, I add MyApp.Data Assembly (and EF Assembly) and when I call DataBase.AddEntry(x) everything works perfectly fine, but when I use Database.GetSomeThings(10) everything breaks.

var x = Database.GetSomeThings(10);
String[] y = Database.GetSomeThings(10);

just silently fails without an error. x and y both don't show up in locals and cannot be watched. Anybody got an idea?

edit: i bound directly to comboBox and it magically worked. now i turned the autocompletemode and source on, and i get an error message:

Für den aktuellen Thread muss der STA-Modus (Single Thread Apartment) festgelegt werden, bevor OLE-Aufrufe ausgeführt werden können. Stellen Sie sicher, dass die Hauptfunktion mit STAThreadAttribute gekennzeichnet ist.

unfortunately german, but i think i can solve this:

http://msdn.microsoft.com/de-de/library/ms182351%28vs.80%29.aspx

so i think this solved the problem (making a starting point for the app, and ofc i made it singleton everything else would have been stupid imho).

    public static class Program
    {
        private static MyApp activeInstance;

        [STAThread]
        public static void Main()
        {
            if (activeInstance == null)
            {
                activeInstance = new MyApp();
                Application.Run(activeInstance);
            }
        }
    }

still, if anybody knows what i have done here i would be thankful. those [ tags ] are probably the last thing in programming i didnt understand.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM