Solved

Threading - How can I update an image box regularily from a thread

Posted on 2006-11-21
6
217 Views
Last Modified: 2012-05-05
Hi,

I have an application that receives and displays live video feed (Motion JPEG).  I have a tree view so the user can select which live video feed to view.  Originally the feed was displayed in an image box on my main form.  It was decided that the feed should be displayed in a pop up window.  This is where I have my problems.

I process the video feed in a worker thread and that is working fine.  When my thread and image box were on the main form, in my worker I had the line:
mCameraView.Image = lastFrame;
This was updating just fine.

Now that I've moved the thread and image box to a seperate form, this code works, but if I move or resize the dialog it crashes.  I was told that this may be due to the fact that I'm trying to access the UI from two different threads; myWorker and the original.

My solution was to save the image to a local variable instead of updating the UI directly
Call invalidate on the ImageBox
In the OnPaint update the ImageBox with the image.

Now my program is just freezing.  I assume it is deadlocking somehow.  Anyone know what is going on?  Or if there is a better solution?
0
Comment
Question by:alexatsearidge
  • 3
  • 3
6 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 17993701
The first step to solve such kind of problems is to make program working by consistent and predictable way. Add this line to the program initialization code:
Control.CheckForIllegalCrossThreadCalls = true;

In this case every direct cross-thread call causes exception immediately. Fix this by replacing direct call with Invoke and BeginInvoke.
0
 

Author Comment

by:alexatsearidge
ID: 17996392
Hi Alex, sorry you went over me head a little there.  I have pretty much no experience in thread programming.
Control.CheckForIllegalCrossThreadCalls = true; <- I cannot find this method for Control
Mind putting that in laymens terms with perhaps a code example for the Invoke/BeginInvoke?
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 17996522
This is from C# 8.0:
http://msdn2.microsoft.com/en-us/library/system.windows.forms.control.checkforillegalcrossthreadcalls.aspx

What version do you use?

About BeginInvoke/Invoke:
http://www.codeproject.com/csharp/workerthread.asp

.NET doesn't allow to call Windows Forms methods from worker threads. Instead of this, we must use BeginInvoke or Invoke calls. However, .NET 1.1 developers were not consistent, and sometimes illegal cross-thread calls work successfully. Sometimes they give exception, sometimes program hangs.
For backward compatibility, .NET 2.0 supports the same behavior. However, this is undesired because program results are unpredictable.

Control.CheckForIllegalCrossThreadCalls = true;

It is strongly recommended to add this call to the beginning of every .NET 2.0 program. In this case every illegal call causes exception - consistent behavior. You need to fix all these exceptions by replacing direct calls with BeginInvoke or Invoke. Only after this is done, and program still hangs, search for possible deadlock.
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:alexatsearidge
ID: 17998313
I'm using .NET ver 1.1
Does that mean I'm basically stuck?

I'm now using Invoke to call my method to add the image.
The form loads and the picture box streams the image successfully, but as soon as I move or resize the form my program crashes.
0
 
LVL 48

Accepted Solution

by:
AlexFM earned 125 total points
ID: 17998352
With .NET 1.1 this is more difficult. You need to check all your code and exclude all calls to Windows controls from worker threads. Even setting simple property is not allowed. For example:
button1.Enabled = true;
Running this code from worker thread can have unpredictable results.

After fixing this return to your original problem. Does program freezes or crashes - you posted two versions :)
0
 

Author Comment

by:alexatsearidge
ID: 18080543
Thanks for your help Alex,

I had some sample code that used GDI to draw the image instead of updating a picturebox.  I use Monitor.Enter/Exit around the drawing code and it works fine.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
network + 7 73
Good resource for accessibility implementation 1 46
import issue in bit values 3 44
Image(2) 3 26
Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now