i have an application which turns your webcam on and off. It turns it on, on a new thread and im trying to make a button work from the main code to turn it off buts cross threading doesnt work and as their is no form, i cant invoke. here is the code:
public class Program
{
public static FilterInfoCollection CaptureDevicesList;
public static VideoSourcePlayer videoSourcePlayer = new VideoSourcePlayer();
public static VideoCaptureDevice videoSource;
public static System.Timers.Timer TimerClose = new System.Timers.Timer();
[STAThread]
static void Main()
{
TimerClose.Elapsed += (o, e) => TimerClose_Tick();
TimerClose.Interval = 10000;
TimerClose.Start();
new Thread(() =>
{
Thread.CurrentThread.IsBackground = true;
CaptureDevicesList = new FilterInfoCollection(FilterCategory.VideoInputDevice);
Class1.oncam();
}).Start();
Application.Run();
}
private static void TimerClose_Tick()
{
Class1.CloseCurrentVideoSource(); // <--- function cant run
}
}
So what im trying to do is get the close function to work which is trying to turn the webcam off which is running on a different thread. Here is class1:
class Class1
{
public static void oncam()
{
Program.videoSource = new VideoCaptureDevice(Program.CaptureDevicesList[0].MonikerString);
OpenVideoSource(Program.videoSource);
}
public static void OpenVideoSource(IVideoSource source)
{
CloseCurrentVideoSource();
Program.videoSourcePlayer.VideoSource = source;
Program.videoSourcePlayer.Start();
}
public static void CloseCurrentVideoSource()
{
if (Program.videoSourcePlayer.VideoSource != null)
{
Program.videoSourcePlayer.SignalToStop();
for (int i = 0; i < 30; i++)
{
if (!Program.videoSourcePlayer.IsRunning)
break;
System.Threading.Thread.Sleep(100);
}
if (Program.videoSourcePlayer.IsRunning)
{
Program.videoSourcePlayer.Stop();
}
Program.videoSourcePlayer.VideoSource = null;
}
}
}
Any help is appriciated