Events Executes In Current Thread Or In A New One
May 18, 2010When an event is raised does the handler executes in the same thread as the main program or in another thread?.
View 1 RepliesWhen an event is raised does the handler executes in the same thread as the main program or in another thread?.
View 1 RepliesWhen an event is raised does the handler executes in the same thread as the main program or in another thread?
View 1 RepliesHow can I make my apparently so that when I click a button, it executes a sub of my choice on a seperate thread? I need this to send an email and when I ran it normally it crashed my GUI.
View 1 RepliesI'm getting the following error:"Current thread must be set to single thread apartment sta mode before ole calls can be made"
I am using Speech.Recognition.SpeechRecognizer and using it to open various dialog boxes. It works fine for the first few but on the third one when it calls the InitializeComponent in the Dialog's New, it throws the above error.
Anybody have any clue what causes this or how to fix it? The SpeechRecognizer seems to run on its own thread, so that seems to be at the root of the problem. If I open thedialog with a button click I don't get an error.
[Code]...
I have a windows application that need to process som quite time consuming jobs. In my first try i did all processing under
the form thread. The result was bad response and update of the form due to the heavy jobs.To get around the problem with bad response from the form i created a new class "processing" where i put all the data processing. Then i instanciated a background worker where i in the "doWork" sub created a new instance of "processing".The "processing" class creates a timer from system.timer, and the timer drives the processing.On the Timer event Elapsed the timer starts a new thread from the thread pool.
My problem is now when i want to asynchronously close the background worker (with the corresponding function call what ever it is called ...) there is still a timer thread out there that causes exceptions for me.
1. How can i close my background worker and at the same time have the timer to be stopped?
2. Is there a way to synchronize the timer event so that the timer executes from the background worker thread?
3. Is there a better approach for me to adapt?
I am in the planning stages for developing a VB.Net class (let's call it OpDAq) that is to obtain data via an API provided by a COM object (AxInterop.TWSLib) that makes requests to an Internet server. The API provides data via events. Class OpDAq should do its work on a separate thread; it takes several seconds (sometimes up to ~10) to collect the desired data. One should be able to create several instances of OpDAq in order to collect data in parallel. The class would be given a reference to the TWSLib object when its constructor is called, as well as several parameters. There needs to be a way for the class to provide results, preferably in the form of a generic list.
View 6 RepliesI have a simple 'Working' form that runs on its own thread to keep the user informed that the application hasn't died during long running operations. In order to get the working form to update I had to insert a DoEvents() call. I'm curious, will this only pump messages for the current thread I'm in, or will it do it for the whole application? I would prefer that the main window stay unresponsive till the operation finishes. Just to be clear, I'm fine with the code I have, but I would like to know how DoEvents() behaves with threads.
Below is the code for the working form.
Public Class frmWorking
''' <summary>
''' Creates and starts a new thread to handle the Working Dialog
''' </summary>
''' <returns>The thread of the Working dialog.</returns>
[Code] .....
I'm currently designing an assembly that will be used by third parties. One of the classes has a long process of TCP connections and it informs about its process using events. For example
''# Do stuff that takes some time
RaiseEvent CompletedFirstPartEvent()
''# Do stuff that takes some time
[Code].....
What I've seen if that if the handler of one of those events takes too long (or even worse, it blocks) I can have timeouts and it's hard for the developer to see that one class is not working fine because his handler is taking too long.
I was going to fire the events in a new Thread to avoid this issue but this looks strange to me because I've never seen something like that, what I've seen until now is the developer spawning a new Thread if his handler was going to be timeconsuming. So the question is:
What would you do? Create a new thread or force the user to create his own thread? (Or is -there a better approach that I don't know?)
I have a processing in a do loop. In variois points within the loop I check if the variable "bCancel" is true. If so then I exit the loop and terminate the treatment. When I click the "Cancel" button, I set the variable "bCancel" to true. Basic stuff, but I would like to know how the click event is ran while my current thread is busy looping? I've found a reference to a "Gui thread" on the net but Its not very clear is this thread in inside windows or the .NET frameworks?
View 7 RepliesA winforms dialog is using BackgroundWorker to perform some asynchronous operations with significant success. On occasion, the async process being run by the background worker will need to raise events to the winforms app for user response (a message that asks the user if they wish to cancel), the response of which captured in an CancelEventArgs type of the event.
Being an implementation of threading, I would have expected the RaiseEvent of the worker to fire, and then the worker would continue, hence requiring me to pause the worker until the response is received. Instead however, the worker is held to wait for the code executed by the raise event to complete.
It seems like method I am calling via the event call is actually on the worker thread used by the background worker, and I am surprised, since I expected to see it on the Main Thread which is where the mainform is running. Also surprisingly, there are no cross thread exceptions thrown.
I am building a code library to be used by a 3rd party developer. Neither my code or the 3rd party have any UI controls involved. It's purpose is a combination of providing the ability to look up information from a database while at the same time polling it and throwing events when a set of conditions are met. Naturally the only way it's going to be responsive to the 3rd party library's commands is via multithreading since everything in a single thread stops once you start polling the database.
The sub I am using is part of an object that I create in the main thread and attempt to have it run via a thread, when a condition is met I have it update a property in that object, throw the event (or execute a callback in my latest attempt) and then attempt to have a simulator for the 3rd party control read the property.It's easy to setup the multithreading part, the hard part appears to be throwing the event so it is seen in the original thread and the data is available there. Any attempt to do this generates a cross thread error when the data is read.
I attempted to use delegates and calling methods asynchronously and using the callback function but still get cross threading errors.Is my only possiblity to use a background worker, using either the progress changed or runworker completed events? Even then am I going to be able to set the property in the function from one thread and read it in another?
I have a very long running process that I start in a thread. This raises events with messages to say what it is doing. How can I update a label on screen with these messages? (The label is in an update panel if that makes it easier).
View 3 RepliesI have lost the small window which displays the objects and events of the current tab. How can I get that back?
This is a screenshot. I have lost the highlighted area!
I have a console application (VB.NET). When it is started, it spawns worker threads to find work in the database and do the task, then look for the next task (A workflow basically).
The problem that I have is that I have my Event Handler in the main thread that spawns the worker threads. Those workers, if they error, raise an event to a public delegate. However my main thread is not detecting these events, and I cannot figure out how to make it. My intention is to do all logging (text/database), email alerts, and restarting the worker to find another task that is not errored. (This will be expanded in the LogErrorMessage routine and such, for now I am just keeping it a simple Console.Writeline so that I can see if the method is being fired or not)
Here is the code around what I am trying to do:
Logger Class:
Public Delegate Sub LogDelegateError(ByVal Ex As Exception)
Public Class EventLogger
Public Event EventError As LogDelegateError
[Code].....
I am trying to create a new thread that will import from an excel spreadsheet, because it was doing all the import before the form actually displayed to the screen. So the user never saw the form while it was importing..In the form's load event I create the thread and start it, but I get this large error:An error occurred creating the form. See Exception.InnerException for details. The error is: ActiveX control '8856f961-340a-11d0-a96b-00c04fd705a2' cannot be instantiated because the current thread is not in a single-threaded apartment.
I am using visual studio 2005 pro on Vista Business?
I have an object that is defined as a global variable based on custom class. Within that class I have an event that gets fired a certain intervals. These events are fired on the same thread as where the object is declared. How do I create a global object, but have the events within that object fire on a separate thread?
View 2 RepliesI have 2 classes, one is frmMain a windows form and the other is a class in vb.NET 2003.
frmMain contains a start button which executes the monitor function in the other class. My question is, I am manually adding the event handlers -- when the events are executed how do I get them to excute on the "main thread". Because when the tooltip balloon pops up at the tray icon it displays a second tray icon instead of popping up at the existing tray icon. I can confirm that this is because the events are firing on new threads because if I try to display a balloon tooltip from frmMain it will display on the existing tray icon.
Here is a part of the second class (not the entire thing):
Friend Class monitorFolders
Private _watchFolder As System.IO.FileSystemWatcher = New System.IO.FileSystemWatcher
[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]....
Ok i have a Form with the following
Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
txtNotepad.Text = LoadFile("notes.txt")
[code].....
We can reproduce it to the point it happens whenever we click Tab, but short of putting a break point at the beginning of every method in the project to find out what is executing.
Is there a way to set visual studio to break whenever anything executes?