|
Hello,
I have a weird problem with my c# code. It works fine on my pc sempron 1600+ but it cracks on dual core processors.
I have two classes (the main form, and one that implements some change to an image). I want to show the change in real time on the form on a picture box. So I created a thread and use delegates and beginInvoke method to refresh the pictureBox after I changed some pixels of the image that I display there.
The application works fine on low speed processors but on dual core or processor with 2ghz+ it cracks, it put the processor on 100% and does not make any changes to the pictureBox. Also I putted some breakpoints and I was stupefied, the application enters the section that changed the pixels, enters the delegate function that refresh the panel but it doesn't make any changes... I have not clue what I done wrong.
Note: I tried the Invoke method too but it the same problem.
I'm open to any suggestions
Thanks
Edit add some code
The delegates
<br />
public delegate void DelegatePictureBoxUpdate();<br />
public delegate void DelegatePictureBoxLoad(Bitmap image, int nr);<br />
public delegate void DelegateActualizareStatus();<br />
public delegate void DelegateActualizareFinal();<br />
<br />
Thread fir;<br />
ManualResetEvent m_EventStopThread;<br />
ManualResetEvent m_EventThreadStopped;<br />
<br />
public DelegatePictureBoxUpdate m_DelegatePictureBoxUpdate;<br />
public DelegatePictureBoxLoad m_DelegatePictureBoxLoad;<br />
public DelegateActualizareStatus m_DelegateStatus;<br />
public DelegateActualizareFinal m_DelegateFinal;<br />
<br />
The delegate function that loads and refresh the pctBox
<br />
public void LoadPictureBox(Bitmap final, int nr)<br />
{<br />
progressInpaint.Maximum=nr;<br />
progressInpaint.Step= nr/20;<br />
pctBoxReparat.Image=final;<br />
lblProprietati.Enabled=true;<br />
lblProprietati.Text+=":"+nr.ToString();<br />
}<br />
public void UpdateStatus()<br />
{<br />
progressInpaint.Value+=progressInpaint.Step;<br />
progressInpaint.Refresh();<br />
}<br />
<br />
void MainFormFormClosed(object sender, FormClosedEventArgs e)<br />
{<br />
<br />
if ( fir!= null && fir.IsAlive )
{<br />
m_EventStopThread.Set();<br />
<br />
while (fir.IsAlive)<br />
{<br />
if ( WaitHandle.WaitAll(<br />
(new ManualResetEvent[] {m_EventThreadStopped}), <br />
100,<br />
true) )<br />
{<br />
break;<br />
}<br />
<br />
Application.DoEvents();<br />
}<br />
}<br />
}<br />
The code in the other class:
<br />
m_form.BeginInvoke(m_form.m_DelegatePictureBoxLoad, new Object[] {final, nrPuncteDeterioate});<br />
<br />
<br />
m_form.BeginInvoke(m_form.m_DelegatePictureBoxUpdate,null);<br />
<br />
m_EventStopped.Set();<br />
modified on Wednesday, April 16, 2008 3:45 AM
|
|
|
|
|
Using some breakpoints to debug threading issues is not the best idea. If you could demonstrate what you have done maybe we can help you.
|
|
|
|
|
Hi,
I want to pass value of textbox1 that is in form1 to form2.
Thanks
|
|
|
|
|
What have you done? How are you trying to accomplish?
(delegates)
|
|
|
|
|
expose a property in the form that has the texbox and call it from the other form when you need it
public string TextBox_text
{
get{return textbox1.Text;}
}
|
|
|
|
|
Both answers you've got are good ( delegates are better ), but I will point out if you'd bothered to search the site, there is at least one excellent article on how to do this.
Christian Graus
Please read this if you don't understand the answer I've given you
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
Can u write down whether you are working on web forms or window forms?
Sarvesh Upadhyay
Senior Software Engineer
Birlasoft India Ltd.
Microsoft Certified Professional Developer in Dotnet 2.0 Enterprise Application
|
|
|
|
|
|
Hi all,
How can i create a group textbox in form with the same name? For example i can create it easily in vb by assign index to that control.But how can i do it in c#?
Any tips are welcome.
Note: I need to do it in design time
regards
cocoon
|
|
|
|
|
You can't. You can create textboxes, but you can't create an array that contains references to them, at design time.
Christian Graus
Please read this if you don't understand the answer I've given you
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
Isn't this close to what was there in VB?
TextBox[] aoTextBox = new TextBox[5];<br />
aoTextBox[0] = new TextBox();<br />
aoTextBox[0].Top = 5;<br />
aoTextBox[0].Left = 5;<br />
aoTextBox[0].Height = 20;<br />
aoTextBox[0].Width = 20;<br />
for(int i = 1; i < aoTextBox.Length; i++) {<br />
aoTextBox[i] = new TextBox();<br />
aoTextBox[i].Top = aoTextBox[i - 1].Top + 5;<br />
aoTextBox[i].Left = aoTextBox[i - 1].Left + 5;<br />
aoTextBox[i].Height = 20;<br />
aoTextBox[i].Width = 20;<br />
}<br />
this.Controls.AddRange(aoTextBox);
My idea of ideal life : Eat, Sleep, Repeat
|
|
|
|
|
Hi d@nish,
Thanks for your reply.Its useful to me.
|
|
|
|
|
Yeah, but he wanted design time support, so I didn't bother to point out how to do it in code. What makes more sense to me is
List<TextBox> _textboxes = new List<TextBox>(); // this is a member variable
foreach(Control control in Controls)
{
if (control is TextBox) _textboxes.Add((TextBox)control);
}
So, he gets a list of all textboxes, which he can add in the designer.
Christian Graus
Please read this if you don't understand the answer I've given you
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
Hi Christian Graus,
What you mean by "So, he gets a list of all textboxes, which he can add in the designer."?And where should i insert of your coding?
thanks in advance
regards
coccon
|
|
|
|
|
Hi,
Could you please let me know where can I get the sample XNA solutions. That solution should have used .tga files.
Thanks in advance.
|
|
|
|
|
creators.xna.com
It wouldn't matter if the solution used .tga, .bmp or .jpg... The content pipeline will do all the processing for you.
Cheers,
|
|
|
|
|
I'm coming from an embedded background using the Nucleus RTOS, and programming in C++. The system I worked on made very heavy use of constructs such as Semaphores, Mutexes, Pipes, Queues, and timers. I'm transitioning to a new project that will be using C# and .NET [I am currently unsure of what version it will be using; if it will be using the full blown .NET or the compact framework, however].
I'm trying to learn analogies for these constructs. I see that there are Mutexes, of course, but do the mutexes have timeouts available? i.e. you want to grab some resource, and if you can't grab it in X amount of time, fail, check an error condition {throw an event here instead?} and do something with that error. When we wanted to communicate between threads, we would use Pipes - we could read a pipe and suspend on it indefinitely to wait for data; or suspend on it with a timeout. What is analagous?
Also - I think I could benefit on a book that delves into these types of concepts. Most of the C#, .NET books I"ve looked at tend to go very lightly on the chapters to do with async communication. Most tend to talk about locking critical sections, but don't seem to talk about it further - a few have said these are advanced topics and are the scope of other types of books. Guess what, I need one?
Thanks all.
PS: Wasn't sure if I should post this in the C# forum or the .NET forum... Took a stab at the C# forum. Apologies if it's the wrong place.
|
|
|
|
|
Read more on the subject here[^]
|
|
|
|
|
I'm wondering if it's possible to have a method that accepts an anonymous delegate invoke on that delegate and have the delegate have a return value and then have that method return the value.
The requirements are:
1. The anonymous delegate must be able to incapsulate parameters, like the Action<> object.
2. The anonymous delegate must be able to have a return type like: string returnValue = actionMethod.Invoke();
Example of the concept:
public void CallRetrieveDataFunction()<br />
{<br />
IRandomInterface classMain = new ClassMain();<br />
<br />
Action <irandominterface> invokeMethod = delegate(IRandomInterface executeReference) { executeReference.GetProductByID(productID); };<br />
<br />
object response = RetrieveData(invokeMethod, classMain);<br />
}<br />
<br />
public object RetrieveData(Action<t> methodToExecute, U referenceOfObjectImplementingInterface)<br />
{<br />
object returnValue;<br />
returnValue = methodToExecute.Invoke(U);<br />
return returnValue;<br />
}<br />
</t></irandominterface>
Anybody have any ideas?
|
|
|
|
|
Action delegates don't allow you to return values. I think the Func<> delegate is what you need, it lets you specify up to 4 input parameter types and a return type. Here is your example, using Func:
<br />
public interface IRandomInterface<br />
{<br />
object GetProductById( );<br />
}<br />
<br />
public class ClassMain : IRandomInterface<br />
{<br />
public object GetProductById( ) { return new object(); }<br />
<br />
public static void RetrieveDataFunction( )<br />
{<br />
IRandomInterface classMain = new ClassMain();<br />
Func<irandominterface, object=""> invokeMethod = delegate( IRandomInterface executeReference )<br />
{<br />
return executeReference.GetProductById();<br />
};<br />
<br />
object response = invokeMethod( classMain );<br />
}<br />
}<br />
</irandominterface,>
|
|
|
|
|
Hi,
I was having a read of this article before.
When we override something like a keypress we use the below code:
<br />
protected override void OnKeyPress(KeyPressEventArgs e)<br />
{<br />
<br />
base.OnKeyPress(e);<br />
}<br />
In the article he declares the methods in the abstract class like this:
<br />
public String Delete()<br />
{<br />
return "Employee " + id + " " + <br />
lname + " " + fname + <br />
" deleted";<br />
}<br />
and implements it like this:
<br />
public new String Delete()<br />
{<br />
return base.Delete();<br />
}<br />
Question 1. What happened to the override like in the Microsoft class?
Question 2. How could I declare a method that looks the same as the Microsoft one?
Question 3. Does anyone know where the System.Windows.Forms abstract classes are located? I tryed to find the answer myself on reflector but could not find anything that is overrided under the namespace.
Cheers,
|
|
|
|
|
Hi Mark,
1.
override and new are opposites.
if you have:
class baze {
public virtual jef() {Console.WriteLine("baze.jef");}
public virtual pol() {Console.WriteLine("baze.pol");}}
class inh : baze {
public override jef() {Console.WriteLine("inh.jef");}
public new pol() {Console.WriteLine("inh.pol");}
}
static void main() {
baze D=new inh();
inh J=new inh();
D.jef();
D.pol();
J.pol();
}
you would get:
inh.jef // override means: use the method that fits the actual object
baze.pol // new means: don't override, use the method fitting the variable
inh.pol // dito
2.
by following their pattern? (and accepting their semantics)
3.
WebRequest is an abstract class
not sure there are any abstract classes in Windows.Forms
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips:
- before you ask a question here, search CodeProject, then Google;
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get;
- use PRE tags to preserve formatting when showing multi-line code snippets.
|
|
|
|
|
As always, thanks for your excellent answer Luc.
Cheers,
|
|
|
|
|
you're welcome.
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips:
- before you ask a question here, search CodeProject, then Google;
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get;
- use PRE tags to preserve formatting when showing multi-line code snippets.
|
|
|
|
|
System.Drawing.Brush and System.Drawing.Image are the abstract classes I use most often.
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips:
- before you ask a question here, search CodeProject, then Google;
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get;
- use PRE tags to preserve formatting when showing multi-line code snippets.
|
|
|
|