[RESOLVED] Code doesn't do anything?
Hey, how is everyone today?
I have a bit of a problem. Okay, so I have 3 buttons. On the click event, I start a thread. That thread calls a sub at the end, and then that sub calls another sub.
2 of the buttons work fine, they all do the same thing, just with different controls. However, the third button doesn't ever finish the last sub, however nor returns ANY errors or problems.
The code on the click event:
Code:
If My.Settings.au= False Then
auChange = New Thread(AddressOf auEnable)
auChange.IsBackground = True
auChange.Start()
ElseIf My.Settingsau = True Then
auChange = New Thread(AddressOf auDisable)
auChange.IsBackground = True
auChange.Start()
End If
On the auEnable I have the following code:
Code:
Panel24.BackColor = Color.FromArgb(100, 100, 100)
Label9.BackColor = Color.FromArgb(100, 100, 100)
Label8.ForeColor = Color.FromArgb(100, 100, 100)
Label9.Text = "enabled"
PictureBox8.Image = My.Resources.enabled1
My.Settings.au = True
modifyArea()
The same code is for the auDisable sub, except the color is different to 100,100,100 in rgb. It's 10,10,10 instead.
The modifyArea sub has the following code:
Code:
Private Sub modifyArea()
Dim au1On As Boolean = False
Dim au2On As Boolean = False
Dim au3On As Boolean = False
If My.Settings.au = True Then
au1On = True
End If
If My.Settings.au1 = True Then
au2On = True
End If
If My.Settings.au2 = True Then
au3On = True
End If
Dim count As Integer = 0
If au1On = True Then
count += 1
End If
If au2On = True Then
count += 1
End If
If au3On = True Then
count += 1
End If
If count = 3 Then
setCorrectArea()
ElseIf count < 3 Then
setBadArea()
End If
End Sub
Now here's where the problem occurs. In the setBadArea sub which is called, the backcolor changes however the text which I have coded in to change and the color of the text DOES NOT.
The code for setBadArea is as follows:
Code:
Private Sub setBadArea()
Panel6.BackColor = Color.FromArgb(60, 60, 60)
Panel7.BackColor = Color.FromArgb(60, 60, 60)
PictureBox2.Image = My.Resources.incorrectimg
PictureBox2.Left = (Panel7.ClientSize.Width / 2) - (PictureBox2.Width / 2)
Label4.Text = "Incorrect 1"
Label4.Left = (Me.ClientSize.Width / 2) - (Label4.Width / 2)
Label4.ForeColor = Color.FromArgb(60, 60, 60)
Label7.Text = "Incorrect 2" + Environment.NewLine + "Incorrect 3"
Label7.Left = (Panel7.ClientSize.Width / 2) - (Label7.Width / 2)
Label17.Text = "Incorrect 4" + Environment.NewLine + "Incorrect 5"
Label17.Left = (Panel7.ClientSize.Width / 2) - (Label17.Width / 2)
End Sub
As you see in the code, I change text of Label4, Label7, Label 17... And I change the forecolor of Label4 but none of that happens. Only the picturebox and the panel6 backcolor changes. HOWEVER, when I click the other buttons it all works fine. I don't understand why this is? The code for the other buttons are exactly the same, except with au1 and au2 on the settings and not au.
Re: Code doesn't do anything?
Anyone there? I don't know why I have this issue, I really do hope someone out there knows how to fix it... :/
Re: Code doesn't do anything?
It's Saturday. Not the most active day of the week on the forums.
Why is any of this on a background thread? The thread alone is going to cause you trouble, since all you are doing on it is changing a bunch of UI elements, work that is necessarily done on the UI thread. That suggests that the problem doesn't have anything to do with the code shown. My reasoning is that you felt that you needed to do this on a different thread, so you must have been expecting the UI thread to be busy with something else. Whatever that something else is seems likely to be the real source of the problem, because the stuff in the background thread HAS to happen on the UI thread.
In other words, I suspect that you have divided up the tasks in a bad fashion such that the UI thread is blocking the background thread from ever getting a chance to do what you are having it do, which shouldn't be possible unless you turned off CheckForIllegalCrossThreadCalls....which are illegal for a reason.
Re: Code doesn't do anything?
Quote:
Originally Posted by
Shaggy Hiker
It's Saturday. Not the most active day of the week on the forums.
Why is any of this on a background thread? The thread alone is going to cause you trouble, since all you are doing on it is changing a bunch of UI elements, work that is necessarily done on the UI thread. That suggests that the problem doesn't have anything to do with the code shown. My reasoning is that you felt that you needed to do this on a different thread, so you must have been expecting the UI thread to be busy with something else. Whatever that something else is seems likely to be the real source of the problem, because the stuff in the background thread HAS to happen on the UI thread.
In other words, I suspect that you have divided up the tasks in a bad fashion such that the UI thread is blocking the background thread from ever getting a chance to do what you are having it do, which shouldn't be possible unless you turned off CheckForIllegalCrossThreadCalls....which are illegal for a reason.
Yes, I had turned off CheckForIllegalCrossThreadCalls on form load. Okay, so I will remove the thread for it and check it works.
Do you know how I can make my whole UI changing subs fast then because without the thread it will be slower.
Re: Code doesn't do anything?
Quote:
Originally Posted by
Shaggy Hiker
It's Saturday. Not the most active day of the week on the forums.
Why is any of this on a background thread? The thread alone is going to cause you trouble, since all you are doing on it is changing a bunch of UI elements, work that is necessarily done on the UI thread. That suggests that the problem doesn't have anything to do with the code shown. My reasoning is that you felt that you needed to do this on a different thread, so you must have been expecting the UI thread to be busy with something else. Whatever that something else is seems likely to be the real source of the problem, because the stuff in the background thread HAS to happen on the UI thread.
In other words, I suspect that you have divided up the tasks in a bad fashion such that the UI thread is blocking the background thread from ever getting a chance to do what you are having it do, which shouldn't be possible unless you turned off CheckForIllegalCrossThreadCalls....which are illegal for a reason.
I have just removed the threads for the process and now I am stuck with the problem I have had in the past before I added those threads.
Basically, the first 2 switches, like before with the threads, WORK PERFECTLY. However, when I click the third switch, ONE of the panels which color was meant to change changes and the text, picturebox etc doesn't change. I don't understand why this is anymore. It's not running on a thread anymore. But, once I click the third switch, all the other switches (buttons) suddenly stop working. Once I click the third button again to turn it "off", the other buttons start working again... Any ideas on that?
Re: Code doesn't do anything?
It is a threading problem. You're overloading the UI thread. As Shaggy Hiker told you, the code you tried to move to secondary threads was unsuitable for moving to secondary threads, as it interacted heavily with UI elements (controls).
Re: Code doesn't do anything?
Quote:
Originally Posted by
.paul.
It is a threading problem. You're overloading the UI thread. As Shaggy Hiker told you, the code you tried to move to secondary threads was unsuitable for moving to secondary threads, as it interacted heavily with UI elements (controls).
What should I do then to carry out the task? With or without threads I have the bug. I've never had this sort of issue before so I don't know what I should do to get the code working now... :(
Re: Code doesn't do anything?
move part of your code to a secondary thread, but not code that accesses controls
Re: Code doesn't do anything?
Quote:
Originally Posted by
.paul.
move part of your code to a secondary thread, but not code that accesses controls
I don't know what you mean (sorry I'm stupid). I just removed the threading but it still doesn't work? But I'm not using threads any more so it should be working?
I also tried using Delegate and Me.Invoke and it still doesn't work... :( :/
Re: Code doesn't do anything?
Quote:
Originally Posted by
visualBEER310
Yes, I had turned off CheckForIllegalCrossThreadCalls on form load. Okay, so I will remove the thread for it and check it works.
Ouch! Never do that again! If your code needs stuff like that to run, it is a sure sign that you should stop and ask for advice.
I cannot see anything in your code that would benefit from being put on a secondary thread and it is good that you have removed that from your code.
Quote:
Originally Posted by
visualBEER310
Code:
If My.Settings.au= False Then
auChange = New Thread(AddressOf auEnable)
auChange.IsBackground = True
auChange.Start()
ElseIf My.Settingsau = True Then
auChange = New Thread(AddressOf auDisable)
auChange.IsBackground = True
auChange.Start()
End If
I do not know if the above code got mangled during posting or if it reflects your actual code, but should this
Code:
ElseIf My.Settingsau = True Then
have been
Code:
ElseIf My.Settings.au = True Then
i.e. do you have a variable named "Settingsau" somewhere is your form class definition?
I am assuming that this is a posting mistake and will proceed on that assumption.
I do not like commenting on coding style, but your code reflects inexperience and we have all been there ourselves whether or not we like to admit it. :o Please name your controls something meaningful instead of retaining the default names. Also, there is no need to test whether a Boolean variable is true or false as those are the only two possibilities and the result of that comparison is itself a Boolean. I have rewritten much of it to show you an alternative that may help you recognize logic errors without needing to reading so much unnecessary logic.
Code:
Private Color10 As Color = Color.FromArgb(10, 10, 10) ' name these something that reflects their usage
Private Color60 As Color = Color.FromArgb(60, 60, 60)
Private Color100 As Color = Color.FromArgb(100, 100, 100)
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
If My.Settings.au Then
Set_au(Color10, "disable")
Else
Set_au(Color100, "enable")
End If
My.Settings.au = Not My.Settings.au
End Sub
Sub Set_au(controlColor As Color, label9Text As String)
Panel24.BackColor = controlColor
Label9.BackColor = controlColor
Label8.ForeColor = controlColor
Label9.Text = label9Text
PictureBox8.Image = My.Resources.enabled1
modifyArea()
End Sub
Sub modifyArea()
If My.Settings.au AndAlso My.Settings.au1 AndAlso My.Settings.au2 Then
setCorrectArea()
Else
setBadArea()
End If
End Sub
Re: Code doesn't do anything?
Quote:
Originally Posted by
TnTinMN
Ouch! Never do that again! If your code needs stuff like that to run, it is a sure sign that you should stop and ask for advice.
I cannot see anything in your code that would benefit from being put on a secondary thread and it is good that you have removed that from your code.
I do not know if the above code got mangled during posting or if it reflects your actual code, but should this
Code:
ElseIf My.Settingsau = True Then
have been
Code:
ElseIf My.Settings.au = True Then
i.e. do you have a variable named "Settingsau" somewhere is your form class definition?
I am assuming that this is a posting mistake and will proceed on that assumption.
I do not like commenting on coding style, but your code reflects inexperience and we have all been there ourselves whether or not we like to admit it. :o Please name your controls something meaningful instead of retaining the default names. Also, there is no need to test whether a Boolean variable is true or false as those are the only two possibilities and the result of that comparison is itself a Boolean. I have rewritten much of it to show you an alternative that may help you recognize logic errors without needing to reading so much unnecessary logic.
Code:
Private Color10 As Color = Color.FromArgb(10, 10, 10) ' name these something that reflects their usage
Private Color60 As Color = Color.FromArgb(60, 60, 60)
Private Color100 As Color = Color.FromArgb(100, 100, 100)
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
If My.Settings.au Then
Set_au(Color10, "disable")
Else
Set_au(Color100, "enable")
End If
My.Settings.au = Not My.Settings.au
End Sub
Sub Set_au(controlColor As Color, label9Text As String)
Panel24.BackColor = controlColor
Label9.BackColor = controlColor
Label8.ForeColor = controlColor
Label9.Text = label9Text
PictureBox8.Image = My.Resources.enabled1
modifyArea()
End Sub
Sub modifyArea()
If My.Settings.au AndAlso My.Settings.au1 AndAlso My.Settings.au2 Then
setCorrectArea()
Else
setBadArea()
End If
End Sub
Thank you! I really appreciate your help there. Yes, the My.Settingsau was a problem. It was meant to be My.Settings.au and it must have gotten mixed up/mangled whilst making the original post.
I will change the code now to your suggestions and get back to you on whether it was working fine.
Re: Code doesn't do anything?
If it is not, the next step is to do some debugging, because the code you have shown should be working VERY fast. None of that should take more than a fraction of a millisecond, and so, should appear instantly. The fact that it is not could have a couple causes, but you'd have to do a bit of debugging for that. If you haven't used breakpoints and stepping through the code, now is the time to learn, because nothing is more useful (and simple, though it is often not covered anywhere). Put a breakpoint in the click event. When execution stops on the breakpoint, step forwards through the code. Either you will reach a loop (very unlikely, as your code would lock up the computer, which is not the problem you reported), or you may be setting the controls back inadvertently (they change, then change back, all too fast for you to see)....or you are setting different controls from what you thought you were setting, but I'm not seeing that.
Re: Code doesn't do anything?
Quote:
Originally Posted by
TnTinMN
Ouch! Never do that again! If your code needs stuff like that to run, it is a sure sign that you should stop and ask for advice.
I cannot see anything in your code that would benefit from being put on a secondary thread and it is good that you have removed that from your code.
I do not know if the above code got mangled during posting or if it reflects your actual code, but should this
Code:
ElseIf My.Settingsau = True Then
have been
Code:
ElseIf My.Settings.au = True Then
i.e. do you have a variable named "Settingsau" somewhere is your form class definition?
I am assuming that this is a posting mistake and will proceed on that assumption.
I do not like commenting on coding style, but your code reflects inexperience and we have all been there ourselves whether or not we like to admit it. :o Please name your controls something meaningful instead of retaining the default names. Also, there is no need to test whether a Boolean variable is true or false as those are the only two possibilities and the result of that comparison is itself a Boolean. I have rewritten much of it to show you an alternative that may help you recognize logic errors without needing to reading so much unnecessary logic.
Code:
Private Color10 As Color = Color.FromArgb(10, 10, 10) ' name these something that reflects their usage
Private Color60 As Color = Color.FromArgb(60, 60, 60)
Private Color100 As Color = Color.FromArgb(100, 100, 100)
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
If My.Settings.au Then
Set_au(Color10, "disable")
Else
Set_au(Color100, "enable")
End If
My.Settings.au = Not My.Settings.au
End Sub
Sub Set_au(controlColor As Color, label9Text As String)
Panel24.BackColor = controlColor
Label9.BackColor = controlColor
Label8.ForeColor = controlColor
Label9.Text = label9Text
PictureBox8.Image = My.Resources.enabled1
modifyArea()
End Sub
Sub modifyArea()
If My.Settings.au AndAlso My.Settings.au1 AndAlso My.Settings.au2 Then
setCorrectArea()
Else
setBadArea()
End If
End Sub
I've done what you said and the third switch is still behaving weirdly and the other switches are more buggy than before? :(
Re: Code doesn't do anything?
Please post all of your code for the Form, not just fragments, so that we can get a better idea of what it is doing.
Re: Code doesn't do anything?
Quote:
Originally Posted by
TnTinMN
Please post all of your code for the Form, not just fragments, so that we can get a better idea of what it is doing.
There is more than 2000 lines of code in the form not to mention my work would kick me out. Looking at the code you provided I think I see a solution to the problem. Hold on, I'll check. What I'll do is I'll just delete the switches and re-code them following what you did from the start again and see if I had accidentally made a mistake somehow.
I'll report back and see if that worked in a few minutes time or so.
Re: Code doesn't do anything?
Quote:
Originally Posted by
Shaggy Hiker
If it is not, the next step is to do some debugging, because the code you have shown should be working VERY fast. None of that should take more than a fraction of a millisecond, and so, should appear instantly. The fact that it is not could have a couple causes, but you'd have to do a bit of debugging for that. If you haven't used breakpoints and stepping through the code, now is the time to learn, because nothing is more useful (and simple, though it is often not covered anywhere). Put a breakpoint in the click event. When execution stops on the breakpoint, step forwards through the code. Either you will reach a loop (very unlikely, as your code would lock up the computer, which is not the problem you reported), or you may be setting the controls back inadvertently (they change, then change back, all too fast for you to see)....or you are setting different controls from what you thought you were setting, but I'm not seeing that.
Okay I will try this also.
Re: Code doesn't do anything?
your work would kick you out? why?
Re: Code doesn't do anything?
Quote:
Originally Posted by
ident
your work would kick you out? why?
If I were to go and post the source code to the whole form that would be giving away the main product considering this is the main form of the application... That would not be good. And there are rules - say someone from Microsoft gave away the source code to Windows on a forum where they have a bug in Assembly. Now you get what I mean?
But I'll mark this thread as solved as the code suggestions above may help others. I decided to redesign what I had been doing with the switches, therefore it will work differently and no longer have this "bug", or should I say fault on my part with the coding, which I agree I had done terribly bad.
Re: [RESOLVED] Code doesn't do anything?
yeh but who would nick this code?
Re: [RESOLVED] Code doesn't do anything?
Quote:
Originally Posted by
ident
yeh but who would nick this code?
I'm not talking about the UI parts which are done badly. I'm talking about the engines to the application which were paid lots of money to be made by people (not me but others). I doubt anyone would want the UI...