Visual C# BackgroundWorker Object is currently in use elsewhere - c#

Hello and thanks for reading my thread! I would like to seek advice for my code because after a lot of searching I couldn't find anything to solve this particular problem. I've googled and searched on stackoverflow and all the solutions somehow didn't work (or I didn't know how to implement them). Here is my code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AForge.Video;
using AForge.Video.DirectShow;
using System.Threading ;
namespace Motion_Detection
{
public partial class Form1 : Form
{
private FilterInfoCollection VideoCaptureDevices;
private VideoCaptureDevice FinalVideo;
private void FinalVideo_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
Bitmap video = (Bitmap)eventArgs.Frame.Clone();
pictureBox1.Image = video;
}
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
VideoCaptureDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
foreach (FilterInfo VideoCaptureDevice in VideoCaptureDevices)
{
devicesList.Items.Add(VideoCaptureDevice.Name);
devicesList.SelectedIndex = 0;
}
}
private void button1_Click(object sender, EventArgs e)
{
FinalVideo = new VideoCaptureDevice(VideoCaptureDevices[devicesList.SelectedIndex].MonikerString);
FinalVideo.NewFrame += new NewFrameEventHandler(FinalVideo_NewFrame);
FinalVideo.Start();
}
private void button2_Click(object sender, EventArgs e)
{
pictureBox1.Image = null;
FinalVideo.Stop();
}
private void button3_Click(object sender, EventArgs e)
{
worker.RunWorkerAsync();
}
private void button4_Click(object sender, EventArgs e)
{
worker.CancelAsync();
}
private void worker_DoWork(object sender, DoWorkEventArgs e)
{
while (!worker.CancellationPending)
{
Bitmap map = new Bitmap(pictureBox1.Image); // this line throws the error
for (int x = 0; x < pictureBox1.Width; x++)
{
for (int y = 0; y < pictureBox1.Height; y++)
{
Color pixel = map.GetPixel(x, y);
if (pixel.R == 255 && pixel.G == 0 && pixel.B == 0)
{
// detected red pixel
}
}
}
Thread.Sleep(100); // check every 100ms or any other given interval
}
}
}
}
So I'm using the Aforge video dlls to access my web camera. This part works, I can access and read the stream from it, when I dump it into picturebox1 it shows up perfectly, without any lag whatsoever.
Now I'm toying a bit around with motion detection and for the first, I wanted to see if I can detect a pixel of a certain color appearing in front of the camera. Because I need to loop through every pixel, I had to put this on a different thread else it kept freezing my GUI and the display started to lag.
The issue is because I did this, I don't know how to properly access the picturebox.image content from the background worker without triggering the error from the title. Some people on the internet suggested using lock() but I never did this nor I know what I should lock() here. I never worked with multithreading before just because in the end I could never handle the access violations..
To fix this problem I tried things like try finally block although even within the try block, I've gotten the same exception. I assume that there is a cleaner way to do what I've mentioned but I can't really get my head around which one that might be.
I hope that my first post here on the forums was clear and understandable as possible.
Thanks in Regards
~ Ilhan

You cant/shouldn't access pictureBox1 unless on the UI thread.
I think you need to do something like this:
private void GetImage(out Bitmap img)
{
img = new Bitmap(pictureBox1.Image);
}
void worker_DoWork(object sender, DoWorkEventArgs e)
{
Bitmap img = null;
Invoke(new Action(() => GetImage(out img)));
// Do what you want with the bitmap
}
Accessing a control on a winform will throw an exception if its not on the UI thread.
You can find out if you are on the correct thread with pictureBox1.InvokeRequired.
Calling Invoke will send a message to the UI thread to do the passed delegate and then it will wait for the passed delegate to complete. Calling BeginInvoke will send the message but not wait.

Related

c# VLC.Dotnet play and then pause issue

I hope someone will be able to help me with my issue. i want to take a screenshot of the video at a specific time index, however when i try to change the time all i get is a blank black screen. i added buttons which play and pause, it allows me to play and pause the video, if i do that and then change the time index, i get an image. im confused as to why it doesn’t work using code. i even preform btnplay.PeformClick(); to play the video and when i do btnpause.PerformClick() to pause the video it doesn’t.
it seems that i can only get an image of the video if i have to physically hit the play and then pause button on my form, im trying to achieve this using code
private void Form1_Load(object sender, EventArgs e)
{
////////////////////LC4 VLC Settings///////////////////////////////////////////////////////////////////////////////////////////
control = new VlcControl();
var currentAssembly = Assembly.GetEntryAssembly();
var currentDirectory = new FileInfo(currentAssembly.Location).DirectoryName;
var libDirectory = new DirectoryInfo(Path.Combine(currentDirectory, "libvlc", IntPtr.Size == 4 ? "win-x86" : "win-x64"));
control.BeginInit();
control.VlcLibDirectory = libDirectory;
control.Dock = DockStyle.Fill;
control.EndInit();
panel1.Controls.Add(control);
main_form_LC4_data();
}
void main_form_LC4_data()
{
long vOut3 = 20;
playfile("path to file");
First_Frame(vOut3);
}
void playfile(string final)
{
control.SetMedia(new Uri(final).AbsoluteUri);
control.Time = 0;
control.Update();
}
void First_Frame(long vOut3)
{
control.Time = vOut3;
}
private void button9_Click(object sender, EventArgs e)
{
control.Play();
Console.WriteLine("PLAY");
}
private void button8_Click(object sender, EventArgs e)
{
control.Pause();
Console.WriteLine("PAUSE");
}
Above is my code in a nut shell
i have tried things like this
private void button10_Click(object sender, EventArgs e)
{
First_Frame(first_frame); // jump to index
}
and then calling up button10.PerformClick(); however it doesnt seem to work. once again if i physically hit the buttons on my form it works perfectly, however not in the way of coding it.
as an example :
play.PeformClick();
Pause.PeformClick();
time = vOut3;
I do hope this isnt to confusing im really stuck and am still hoping someone can help me
Thank you
A few things:
control.Update();
this does nothing.
You need to wait for the Playing event to be raised after setting the time, otherwise libvlc doesn't have the time to decode the frame and display it (setting the time is asynchronous)

How do I start C# program without the window being active?

I'm trying to create a C# program, but I don't want the window to be active when I open it. I'd like it to open in the background, and the window to show up on top of my other programs, except I want my active window to stay the same. It's because I'm using full screen programs, and I don't want my little popup to take my out of the full screen mode.
Program Use (might help in understanding what I need): I'm creating a set of macros that turn a spare mouse into a media controller. The scroll wheel controls volume, left button controls play/pause, etc. I use Spotify for music, and I want to be able to change the volume of Spotify independently from my computer's global volume. I already have this figured out using code here. I want a popup to display telling me that when I use the scroll wheel, I'm changing the volume of Spotify opposed to global volume. I want to be able to activate the macro, display the popup, change the volume as I wish, and then deactivate the macro without exiting my full screen applications. Hopefully this helps, thank you!
Program Use Edit: Here's just an explanation video, should be easier than trying to explain. To clarify, I want the program to not change activated window when it starts and to always be top most, without me having to activate it first. Thank you!!! https://streamable.com/2pewz
I'm using a program called QuickMacros to open the popup and I've tried a few different settings in there but haven't had any luck. I don't have any experience with C#, so I haven't tried anything inside C#.
My code is unrelated to the issue, but here it is just in case. All this does is give me the ability to move the popup.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SpotifyPopup
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
this.Left += e.X - lastPoint.X;
this.Top += e.Y - lastPoint.Y;
}
}
Point lastPoint;
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
lastPoint = new Point(e.X, e.Y);
}
private void label1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
this.Left += e.X - lastPoint2.X;
this.Top += e.Y - lastPoint2.Y;
}
}
Point lastPoint2;
private void label1_MouseDown(object sender, MouseEventArgs e)
{
lastPoint2 = new Point(e.X, e.Y);
}
}
}
Thank you for your help!
Your question is a little bit unclear but if I am right what you want is to start your application in minimized state, to do that simply use code below in your form constructor
this.WindowState = FormWindowState.Minimized;
And when your event is fired and you want your app to be on top just use
this.TopMost = true;
this.WindowState = FormWindowState.Normal;
and for proper positioning of your form you can use this answer
Edit
Ok, now your needs are more clear, this a demo of what i think you want, in this example the form starts minimized and comes to top on mouse wheel, and then goes to background when idle, u can add more events to code and adapt it for your needs,
I used global hooks for this demo thanks to this link, so dont forget to add the proper nuget package based on the provided link.
here is the code:
using Gma.System.MouseKeyHook;
using System;
using System.Drawing;
using System.Windows.Forms;
public class Form1 : System.Windows.Forms.Form
{
private Timer timer;
private IKeyboardMouseEvents m_GlobalHook;
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
public Form1()
{
Subscribe();
timer = new Timer();
timer.Interval = 1000;
timer.Tick += Timer_Tick;
// Set up how the form should be displayed.
ClientSize = new System.Drawing.Size(292, 266);
Text = "Notify Icon Example";
WindowState = FormWindowState.Minimized;
Rectangle workingArea = Screen.GetWorkingArea(this);
Location = new Point(workingArea.Right - Size.Width - 100,
workingArea.Bottom - Size.Height - 100);
}
private void Timer_Tick(object sender, EventArgs e)
{
WindowState = FormWindowState.Minimized;
TopMost = false;
}
public void Subscribe()
{
// Note: for the application hook, use the Hook.AppEvents() instead
m_GlobalHook = Hook.GlobalEvents();
m_GlobalHook.MouseWheel += M_GlobalHook_MouseWheel;
}
private void M_GlobalHook_MouseWheel(object sender, MouseEventArgs e)
{
WindowState = FormWindowState.Normal;
TopMost = true;
timer.Stop();
timer.Start();
}
public void Unsubscribe()
{
m_GlobalHook.MouseDownExt -= M_GlobalHook_MouseWheel;
//It is recommened to dispose it
m_GlobalHook.Dispose();
}
}
Have a look here: Bring a window to the front in WPF
This thread discusses the general mechanism of presenting, activating and showing windows with WPF.

EmguCV not reading camera

I have the the following code in a normal windows form application with EmguCV 3.1
public Form1()
{
InitializeComponent();
_capture = new Capture("http://root:pass#192.168.1.27:80/axis-cgi/mjpg/video.cgi");
_capture.ImageGrabbed += ProcessFrame;
}
private void Form1_Load(object sender, EventArgs e)
{
_capture.Start();
}
private void ProcessFrame(object sender, EventArgs e)
{
Mat image = new Mat();
_capture.Retrieve(image);
imageBox1.BackgroundImage = image.Bitmap;
}
I have tested the above link in a browser it worked, I have also tested this using iSpy it also works there but using EmguCV ProcessFrame is never reached
I have also tried to connect to the camera using Luxand and it worked well but Luxand is not free so I have to use EmguCV to do face detection & recognition
Looking at this post, try adding ?x.mjpeg after .cgi in your url.

c# Taskbar Progress Bar, getting value from Windows Media Player currentMedia.currentPosition

What I tried to was getting the value in the following way, which didn't work:
private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
TaskbarManager.Instance.SetProgressState(TaskbarProgressBarState.Normal);
axWindowsMediaPlayer1.Ctlcontrols.play();
axWindowsMediaPlayer1.Ctlcontrols.currentItem =
axWindowsMediaPlayer1.currentPlaylist.Item[listBox1.SelectedIndex];
backgroundWorker2.RunWorkerAsync();
}
private void backgroundWorker2_DoWork(object sender, DoWorkEventArgs e)
{
double val = 100*axWindowsMediaPlayer1.Ctlcontrols.currentPosition/axWindowsMediaPlayer1.currentMedia.duration;
TaskbarManager.Instance.SetProgressValue((int)val,100);
}
I am also not sure, where would I put the line for stopping the progress, but I guess it is a bit early to think about it, since I can't get the progress to work anyway.
Is the problem in how I used the backgroundWorker or in how I update the value, or something else?
Thanks in advance,
~~hlfrmn
EDIT:
private void TaskbarProgressValueDeterminator()
{
TaskbarManager.Instance.SetProgressState(TaskbarProgressBarState.Normal);
while (axWindowsMediaPlayer1.Ctlcontrols.currentPosition < axWindowsMediaPlayer1.currentMedia.duration)
{
TaskbarManager.Instance.SetProgressValue((int)(100 * axWindowsMediaPlayer1.Ctlcontrols.currentPosition / axWindowsMediaPlayer1.currentMedia.duration), 100);
}
TaskbarManager.Instance.SetProgressState(TaskbarProgressBarState.NoProgress);
}
The background worker will update the progress only once. I expect you need to put in a while loop or a timer to update the progress repeatedly while the track is playing.

picturebox opening too late

I want to show "Loading.., please wait" gif by getting content from web.
I have tried the following code, but Picturebox opening too late.
private void buttonStart_Click(object sender, EventArgs e)
{
pictureBox1.Visible = true;
webSatList = new WebSatelliteList(this, XMLSatList, name);
webSatList.LoadTPList();
TPListToBeAdded = webSatList.GetTPListToBeAdded();
TPListToBeRemoved = webSatList.GetTPListToBeRemoved();
drawTPListGridView(TPListToBeAdded, TPListToBeRemoved);
}
public void drawTPListGridView(List<TPInfo> TPListToBeAdded, List<TPInfo> TPListToBeRemoved)
{
pictureBox1.Visible = false;
//draw TP List ..
}
Picturebox is openning after this line:
"TPListToBeRemoved = webSatList.GetTPListToBeRemoved();"
I have tried to fix this problem by using backgroundworker (the following code) and the same problem has been seen. Also, I have used the popup form instead of PictureBox nothing has changed.
private void buttonStart_Click(object sender, EventArgs e)
{
pictureBox1.Visible = true;
backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
backgroundWorker1.RunWorkerAsync();
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
webSatList = new WebSatelliteList(this, XMLSatList, name);
webSatList.LoadTPList();
TPListToBeAdded = webSatList.GetTPListToBeAdded();
TPListToBeRemoved = webSatList.GetTPListToBeRemoved();
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
drawTPListGridView(TPListToBeAdded, TPListToBeRemoved);
}
public void drawTPListGridView(List<TPInfo> TPListToBeAdded, List<TPInfo> TPListToBeRemoved)
{
pictureBox1.Visible = false;
//draw TP List ..
}
How can i fix this problem? Any help would be appreciated.
Not entirely sure what you're trying to do here, but chances are you want to implement the async / await pattern.
Mark your button press as async
private async void buttonStart_Click(object sender, EventArgs e)
For anything that you need to wait for should then be awaited and it will keep your form redrawing so it doesn't freeze up. For example, something like:
await Task.Run(() => loadPictureBox());
Or you could make your loadpicturebox method asynchronous by giving it a signature of async Task.
The problem you're likely having is that the form will not update or refresh itself until the click method has exited. This means if you first make it display a loading image, and then load the next image in the same method that the form will freeze up until both operations have finished and the method has exited.
The async await pattern means that while it's doing some processing or whatever, let windows carry on drawing the form and handling actions on it.

Categories