I will try explain as much as i can. Basically i'm using Global Hotkeys in my Formless System Tray Application. To do so i'm using this hotkey manager: Global hotkey in console application When calling the hotkeys i started receiving this exception:
Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked on it.
The way i 'fixed' the exception is: https://stackoverflow.com/a/6373700/6329242 My global hotkeys started working except one and I am getting the same exception for it. Couldn't really find a way to fix it.
Here's how i call them:
static void HotKeyManager_HotKeyPressed(object sender, HotKeyEventArgs e)
{
switch (e.Key)
{
case Keys.D1:
CaptureAreaOfScreenForm caos2 = new CaptureAreaOfScreenForm();
var thread1 = new Thread(new ParameterizedThreadStart(param => { caos2.Show(); }));
thread1.SetApartmentState(ApartmentState.STA);
thread1.Start();
break;
case Keys.D2:
CaptureWorkingArea cwa2 = new CaptureWorkingArea();
var thread2 = new Thread(new ParameterizedThreadStart(param => { cwa2.CaptureTheWorkingArea(); }));
thread2.SetApartmentState(ApartmentState.STA);
thread2.Start();
break;
case Keys.D3:
CaptureFullScreen cfs2 = new CaptureFullScreen();
var thread3 = new Thread(new ParameterizedThreadStart(param => { cfs2.CaptureDesktop(); }));
thread3.SetApartmentState(ApartmentState.STA);
thread3.Start();
break;
case Keys.D4:
CaptureAllScreens cas2 = new CaptureAllScreens();
var thread4 = new Thread(new ParameterizedThreadStart(param => { cas2.CaptureScreens(); }));
thread4.SetApartmentState(ApartmentState.STA);
thread4.Start();
break;
case Keys.D5:
UploadFromFile uff2 = new UploadFromFile();
var thread5 = new Thread(new ParameterizedThreadStart(param => { uff2.UploadFile(); }));
thread5.SetApartmentState(ApartmentState.STA);
thread5.Start();
break;
}
//collect the garbage
GC.Collect();
}
(case Keys.D1 is the problem). The difference between the problematic one and the others is i am calling a form class. (others are working fine)
P.S. I am new to programming if I can add something that would help solving it or anything more tell me to do so. Thanks.