Retain Executing Thread's Context During Call To QueueUserWorkItem In ASP.Net?
Nov 17, 2011
We have an ASP.NET application that queues some of its long-running operations (generating reports, for example) into the ThreadPool using System.Threading.ThreadPool.QueueUserWorkItem.
We run the ASP.Net application pool using a specific domain service account so that it can access remote resources, including files and databases. When the execution starts in the ThreadPool, the user identity of the thread is set to Network Service, which doesn't allow us to access the remote resources.The web application can be accessed by users in different countries simultaneously and the format of the data that we provide to each user is based on their culture settings (using the standard globalization settings in web.config). When the queued thread is started, it has also lost this information and reverts to the default culture for the operating system.
Now, we can probably get around most of these issues by capturing the current user identity and culture in a state object and passing that to the background worker, then impersonate the user and set the culture on the thread, but this seems like a very "unclean" way to resolve this issue.[code]...
However, I get an error in the call to BeginInvoke (the UpdateApplicationDataUI portion of the delegate is stating "Expression does not produce a value").
Me.Dispatcher.BeginInvoke(Function() New Action(AddressOf UpdateApplicationDataUI))
I have a GUI which executes on the GUI-thread. Further I have another thrad which does video processing using DirectShow (this is COM).
- Now I want to execute a function on the same thread as where the videoprocessing is executing. This is initiated by the GUI-thread. Touching the COM-referenced object goes wrong if a execute the function from another thread (I understand that). When a call the function from the same thread everything goes fine. But to do this I have the poll some flag-bits in the mainloop, and that is not a 'pretty' solution...
All discussions on the internet are about how change the GUI from a different thread.
I have tried several things:
1) Invoke the function using a delegate. This doesn't wordt, since the function is executed in the GUI thread. (checked bu ThreadID's)
2) BeginInvoke the function using a delegate. May be .net executes the function in a original thread where the object has been created. This does also not work: .net used random threads (probaby from the threadpool)
3) ISynchronizeInvoke, Threading.SynchronizationContext and some other features seems only to work on the GUI thread only...
The question: Is there a way to Post, Send of Invoke a delegate on the specific thread (for example by a reference to this thread or threadID)
I am using a .Net JavaScript implementation called Jurassic to run user-controlled scripts within my .Net 4 WPF application coded in VB.Net - C# answers are fine. The script engine runs on its own thread and provides an API for the script to interact with my application. This all works really nicely until a user executes a script that causes an infinite loop and takes out a core of the processor.
[Code]...
The reason I care about keeping the thread alive is because the user who wrote the script and the user who is running it may not be the same, and I want to keep the experience as smooth as possible the the user running the thread. There also might be legitimate situations in which a single JavaScript function would run for a long time and I do not want to kill that, I just want to stop being allowed to hog the resources.
Solutions that involve stopping the thread from slowing down the system but that still show high CPU usage are not preferable because I do not want the user to wrongly feel that the application is resource intensive.
I am using VB.NET 2008.Every now and then, ThreadPool.QueueUserWorkItem can take up to 500 msI checked the available thread, and it's not 0.What can cause ThreadPool.QueueUserWorkItem to take long sometimes, and how can I make it faster ?
Do While Not TerminateRisk While qRiskSync.Count > 0 sItem = qRiskSync.Dequeue
I am trying to execute a line of code that i'll call f(x) and include a timeout. f(x) is an function in excel that can fail catastrophically. I did not code f(x) and the failure generates a dialog box. When 'ok' is clicked, Excel immediately closes. BTW: error trapping (On Error Resume Next) does not trap this error.
I have a class that runs as a thread. While the thread is running, I want to call a method in that thread. I do this commonly where a thread calls a method in the GUI with a delegate. But I'm confused how to do this when the called thread is not the GUI and there are no parameters passed to the called method.
So, I'm having troubles implementing a separate thread. This is because I have a simple class, and in it I start a new thread. So, as it is not any form, I haven't found any way to make it call the function in the UI Thread.
So, I cannot use the Invoke method. Is there any way to call a function from another thread?
I am trying to call a method which has two parameters using thread. How can I do this? If I pass parameter to my thread.start() method, it is giving error.
Im trying to call a procedure to run into a thread..But this procedure requires two arguments. How can I pass these arguments in to the function when the thread call:
Dim myThread As New Threading.Thread(New Threading.ThreadStart(AddressOf PopulateTermList myThread.Start()
addressof PopulateTermList should only be called without parenthesis(the VB tells me this)
I've written the following code which works fine.I'm wondering if there is not a simpler way of writing it to achieve the same?
[Code]...
I've not found any other way than using a Timer to have the status updated and the other actions done, as as soon as I start the thread, I loose control and "GetList_Button_Click" does not wait or control the thread.
The goal of using the thread is to not freeze the application and also adding a button to cancel the search in the future.
I want to add a parameter to my thread call here is the code. I want to call th.Start(True, sender.Text) and Count(ByVal isRunning As Boolean) will then become Private Sub Count(ByVal isRunning As Boolean, Byval strID As String), but I can't do that. So how can I make this thread accept 2 parameters?
Private Sub btnStart1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart1.Click th = New System.Threading.Thread(AddressOf Count) th.Name = "Button1" th.Start(True) [Code] .....
I have a few classes. One of the classes handles everything to do with updating controls (Such as adding items to a listview, etc.) and another is handling loops. I'm trying to add something to a richtextbox from another thread.[code]..
I have a 3 functions that should be called in order, that I want to do this in a thread say about 20 for each argument I pass. So for example
sub process_work(arg) result1= function1( arg ) result2= function2(result1 ) result3= function3( result2 ) listview.items.add( result3 ) end sub
How do I call this sub and pass to thread on button click. Is it possible to spawn new thread it I pass argument to this sub and the current or last one is busy?
i got a background worker that has the following code in the do work.... one of the things i want to do is to add rows if theres none available ...but i got an error saying i need to do a safe call thread...i already read about it but im stuck i put control.invoke, but that gives me an error too saying Error 1 Reference to a non-shared member requires an object reference.
I am trying to make a thread-safe call to a button on another form and I cannot figure out how to do it.I have read all of the MSDN documentation on thread-safe calls .
I'm calling a class function in a thread function. I'm using invoke method to send a value to a progressbar in the main form and I don't know how to do that in an other class function called by the thread function.
My goal is to send a value to a progressbar to the main form from the thread function and even from the function called by the thread function.
My code: ' Invoke sub Public Sub UpdPgEvent(ByVal value As Integer) Me.pgFindEvent.Value = value
i got a background worker that has the following code in the do work.... one of the things i want to do is to add rows if theres none available ...but i got an error saying i need to do a safe call thread...i already read about it but im stuck i put control.invoke, but that gives me an error too saying Error1Reference to a non-shared member requires an object reference.what can i do to add rows safely?
vb.net Private Sub BackgroundWorker2_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker2.DoWork If DataGridView2.Rows.Item(num).Cells.Item(0).Value = "" Then Control.Invoke(DataGridView2.Rows.Add(1))
I'm going to level with you -- although I heard someone say a long time ago "Never use it"! -- and it stuck with me, if I didn't have this in my form load code, my program wouldn't work.Because, to be completely honest, I could never get my head around the issue regarding the serial port and using delegates. (Haven't given up though).
Anyway, the software works absolutely fine with this statement BUT I get a several errors without it based on that "separate threads" thing (to put it in hillbilly terms). is it still "Never use it"? or am I not alone, and some of you have also used "check for illegal cross threads = false"
I have a function in a module that returns a datatable.I need to call that function asynchronously and get the results in the Main thread (Main Form) in vb.net.
How do I dynamically call a function with delegates by passing a string to a background thread in VB.NET?
Currently my code is this
Public Sub invertImageBK(ByVal image As Bitmap) Dim snxl As System.ComponentModel.BackgroundWorker = createBW() snxl.RunWorkerAsync(New ImageProperties(image.Clone, "invertImage", Nothing))
When bulk renaming files (14000 of them) i get this error: The CLR has been unable to transition from COM context 0x4cefd8 to COM context 0x4cf148 for 60 seconds. The thread that owns the destination context/apartment is most likely either doing a non pumping wait or processing a very long running operation without pumping Windows messages. This situation generally has a negative performance impact and may even lead to the application becoming non responsive or memory usage accumulating continually over time. To avoid this problem, all single threaded apartment (STA) threads should use pumping wait primitives (such as CoWaitForMultipleHandles) and routinely pump messages during long running operations.
My code is as follows:
Private Sub frmTest_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim asd As List(Of String) = FileIO.FileSystem.GetFiles("D:Music").ToList Dim rs As New ADODB.RecordSet("SELECT * FROM Songs", My.Settings.Files_Database__Connection)
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".
"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.
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
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).