Close Form In Thread?
Oct 16, 2010When I am inside a thread, I cannot simply call Me.Close or I get cross threading error!So I need to use some code like this and then call SetClose()
[code]...
When I am inside a thread, I cannot simply call Me.Close or I get cross threading error!So I need to use some code like this and then call SetClose()
[code]...
I have a main form that has a button with which a smaller form is shown. Think of the smaller form as the Find/Replace dialog in many applications, such as Notepad. It's important that the form is (what I believe is called) modal. What I mean is that it always stays on top of the main form. I ensure that by calling the Show method with "Me" as the owner argument. Whenever the small form loses focus it will not disappear into the background but stay visible (albeit out of focus). If you don't understand open up Notepad and have a look at the behavior of the Find/Replace dialog.
Here's my problem: instead of actually closing the form when the X is pressed, I want it to simply Hide itself, so its position and the state of any controls (checkboxes etc) is preserved automatically.To achieve this I simply cancel the FormClosing event and Hide it:
vb.net
Private Sub Form2_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
[code]....
To show the form, I use the following (note the (Me) to make the main form the owner of the form; this ensures that it remains visible even when it is out of focus):
vb.net
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Form2.Show(Me)
End Sub
Now. When I run my project, and open the Form2 (small form), then hide it again (by 'closing' it), I can no longer close the main form (Form1)! It seems the main form cannot close when the small form still exists (albeit invisible)...?? When I don't use the Me argument in the Show method, I don't get the behavior I want. I know I can set the TopMost property to True but that will also cause it to become visible on top of all the other forms, even windows not part of my application.
i am having a thread to keep the application in sleep for some time after the application process a data.
[Code]....
Currently, the application gets hanged if i press close button when thread.sleep() is running. How can i make the close button to close the application even when the thread.sleep is running.
I am trying to give some user entertainment, and show a "please wait" window, with Marquee, during the loading of a separate complex Window. I am attempting to do this by loading the Window in a new thread, like this:
Public Function ShowPleaseWait() As System.Threading.Thread
Dim PleaseWait As New System.Threading.Thread(AddressOf LoadPleaseWait)
PleaseWait.SetApartmentState(System.Threading.ApartmentState.STA)
[code]....
In the calling code, it calls ShowPleaseWait and saves the Thread for later.. To close the window, it calls Thread.Abort, on the saved thread. This in turn will causes it to enter the Catch. I have tried, many different ways, with and without the catch.This works incredibly, the first time it is called. However, additional calls will fail at window.Show() with the exception: The calling thread cannot access this object because a different thread owns it..This really puzzles me as the window was created one line above the call to window.Show and is local. How is it owned by a different thread? How can I fix this?
How can I close or stop a thread. For example
Dim trlisten As Thread
Is it trlisten.Stop? or trlisten.Close? or what?
I actually have gotten the events firing like I expect except for trying to close the dialog window.
Background: I have a wrapper around my "Jabber Client" that sets the delegates for the client wrapper and basically generates a messageProcess event. This event is wired in the main program Class[code]...
I have set up a notify icon for my form. I want to make it so that when the user clicks the close button that it doesn't close the form it just takes it to shows the notify icon. They can exit the program from the notify icon. Can someone tell me how to keep it running?
View 2 Replieswhen I try to perform a cross thread call to fill a text box with messages with the following code I have no problems:
Delegate Sub SetTextCallback(ByVal [text] As String)
Public Sub SetStatusBox(ByVal [text] As String)
If Me.statusBox.InvokeRequired Then
[code]....
However, I am having problems when I use the following code to do a cross thread call to close a form. The form just hangs there.
Public Delegate Sub setFormClosing(ByVal ctrl As Control)
Private Sub closeForm(ByVal ctrl As Control)
If Me.InvokeRequired Then
[code]....
sometimes, when I use Form.close() when debugging my program, although the form is closed, the application is still running. I noticed this behaviour when using the msgbox function.
View 3 Repliesmy web browser is my main form and it has a number of sub forms , how can i set it that when i close the main form my sub forms dont close ?
At the moment when i close the main form all the forms close
I try to transfer a vb6 application to vb2010. In the vb6 I start with a login panel that the user enter uid and password autenticate with the DBServer if ok show application main menu and close the login form. the order of the me.close() after the mainform.show() does not change, The application ends.
View 5 RepliesI have a situation where me.close() doesn't close the form. I have a form with a button with the code seen below. What happens when the button is clicked is the new form shows, but the original form(calling form) is still there. When I go into debug mode, I can see the me.close() execute, but nothing happens (calling form stays open). When I close the second form both forms close. If I comment out the call for the second form to open the first form closes without problem.
Private Sub cmdNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdNext.Click
Dim f As New frmOrder3
[Code].....
I am currently working on a project that has a splashscreen This splash screen is designed to be shown when the main form begins to load its requirements, and then closes once it has loaded... Currently it works like so:
Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
frmSplash.Show()
dolotsofloading()
frmSplash.Close()
End Sub
[Code]...
I am really new to Visual Studio and VB and I am having trouble closing a single form:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.Close()
End Sub
When I use Me.close() associated with this button it closes the entire project and not just the single form can anyone.
get a button on form A to open form B and then close (not hide) form A when clicked?Background: I am coding a VSTO application for Excel in VB2008.
Private Sub FormAButton_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles FormAButton.Click
Dim FormB As New FormB
[code]....
In one application, I need use 4 simultaneous threads.When the threads finished, I need to update the text of a TextBox.So, I create 4 Textbox, and I wanna the threads change the text to FINISHED.Each thread change one distinct TB.I use this "example"
Dim Terminados As Integer = 0
Private Sub frmEnviarMensagem_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Timer1.Enabled = True
Timer1.Start()
[code]....
The problem is, I have an error in the red lines.The error is: INVALIDOPERATIONEXCEPTION "Cross-Thread operation not valid: Control tbPlaca1 Accessed from a thread other than the thread it was created on."I don't use cross threading. Each TB only was accessed for the correspondent thread, and for the "main program".
This is the error message I am getting:
"Cross-thread operation not valid: Control 'Panel1' accessed from a thread other than the thread it was created on." The reason I am getting this error is because I am opening up a new form and then calling these three things:
Panel1.Show()
Label1.Show()
Label2.Show()
why I get this error message because it doesn't occur normally if I open Form2 after closing Form1, it only occurs when I open Form2 after closing Form4.
I have done a program using vb2005 to display reading from my microcontroller bs2 board but have encountered some problems. My code are as follows.
Dim Stop_Rx As Boolean
Private Sub btnRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRead.Click
SerialPort1.Open()
[code]....
I've encounter an error which is, (Cross-thread operation not valid: Control 'txt1' accessed from a thread other than the thread it was created on.)
When my client try to connect to server I'm getting this error : Cross-thread operation not valid: Control 'l_users' accessed from a thread other than the thread it was created on.
Private Sub _socketManager_onConnectionAccept(ByVal SocketID As String) Handles _socketManager.onConnectionAccept
l_Users.Items.Add(SocketID)
End Sub
Would anyone be able to help me here please. I'm fairly new to VB.net and threading so im just trying to figure out what is happening.When I debug this I am getting the error thread operation not valid: Control 'ProgressBar1' accessed from a thread other than the thread it was created on.
I'm a little lost as to why the error is occuring or how to fix it. I've had to put the progress bar in a separate thread otherwise the GUI crashes
[Code]...
My understanding is that thread.join will suspend the execution of code on the calling thread until the spawned thread finishes or is aborted...
With that in mind, I tried this:
For i = 1 to 50
threads = New Thread(AddressOf test)
threads.IsBackground = True
threads.SetApartmentState(ApartmentState.STA)
[CODE]...
However, the rest of the code runs when the loop finishes, not waiting for all the spawned threads to finish. Since the rest of the code needs the threads to finish (otherwise the rest will error).
I have written an application that loads a form - frmCad as well as AutoCad. It does this via a class which detects when AutoCad quits.
Friend Class CadApp Private WithEvents AppObject As AcadApplication... Private Sub AppObject_BeginQuit(ByRef Cancel As Boolean) Handles AppObject.BeginQuit RaiseEvent Quit() End SubEnd Class
My main class loads frmCad as well as CadApp.
Public Class Cad Private WithEvents frmCad As CadForm Private WithEvents app As CadApp... Public Sub ShowForm() If frmCad Is Nothing Then frmCad = New CadForm frmCad.Visible = True End Sub... Private Sub app_Quit() Handles app.Quit frmCad.Dispose() frmCad = Nothing .... End Sub
When I debug the program, it stops at frmCad.Dispose()The program continues to execute, but all the code after it fails to work.Looking more carefully I get an error message which contains:-Cross-thread operation not valid: Control 'Autocad' accessed from a thread other than the thread it was created on.
My error:Cross-thread operation not valid: Control 'label2' accessed from a thread other than the thread it was created on. I want to be able to give my Public RunBot() sub access to edit Labels, buttons, etc without an error.
[Code]...
I'm getting the 'System.InvalidOperationException' error on acccessing a thread from other than where it was created. My code is:
'Required by the Windows Form Designer
Public components As System.ComponentModel.IContainer ' changed from private
[code]....
My problem is Cross-thread operation not valid: Control 'statusHO' accessed from a thread other than the thread it was created on.
System.InvalidOperationException was unhandled
Message="Cross-thread operation not valid: Control 'statusHO' accessed from a thread other than the thread it was created on."
Source="System.Windows.Forms"
StackTrace:
[Code]...
i used it and it works fine then. but as it is mentioned its not a good practice to use it. So what should i do? is it ok if i use this as i am not able to use correct practice. Mine application is not for enterprise purpose. its just my class project, and it works fine even if i put application working in background. System works ok. I dont know how to correct this error.As i dont have that much knowlege of threads.
So please guide me how to fix this in correct way otherwise i am force to go for bad programming practice
Please refer to Windows Mobile Developer Center > Smart Device Development Forums > Windows Mobile Development > Appendtext not working for all my code details
Cross-thread operation not valid: Control 'lv1' accessed from a thread other than the thread it was created on.
Dim lvi As ListViewItem
For Each lvi In lv1.Items
[code]....
I'm currently in the process of building a text editor type program, and have run into a brick wall. I haven't done VB in years, so I may just need a little reminder on some things. I have coded everything so far as far as opening files, saving them, changing fonts, colors, etc. However, I'm looking to add a Close button to my menu strip that will just close the currently opened file, and not the entire program, while also ask the user if he/she would like to save before closing the file, and then if they select yes, it will show the save dialog, and if not, it will go ahead and close the currently opened item.
View 2 Repliesi am having some trouble in my Windows form application. i have 2 forms1st Main / Parent Form2nd Sub / Child Form (Menu)
View 7 RepliesI have been making a webbrowser for a while now, everything works well, except i am unable to make the 'open in a new window' button work right. So far i have managed to get a new instance of my form to open with the right url, but i am unable to keep that new form open when the orginal parent form closes.
Code:
Private Sub WebBrowser1_NewWindow(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles WebBrowser1.NewWindow
e.Cancel = True
Dim Href As String
[code]....
How do i load the Main form of a WPF so that a seperate thread goes and gets data from the database while the form is in an apartmentstate? (drag-able / movable) I Know this is possible with opening a new window from your main form like this :
Private Sub openOrderWindow()
Dim OrderWindow As Orders = New Orders
OrderWindow.ShowDialog()
End Sub
[code]....
I've tried putting MyBase.ShowDialog() and .Show() and .9000+ other things The only benefit I've got out of using a seperate thread to load from the databse is i can see the screen right away (as oppose to it being a transparent box until it loads) but i cannot move it around or minimize it?is there any way to make it Movable while it loads?