VS 2008 Threads - Create New One Or Re-use Old One?
Apr 9, 2010
I need to 'download' the contents of an XML file that resides on some web address and parse them. I need to do this about once every 5 seconds. I use a HttpWebRequest to do it, but I don't think that's relevant.The 'problem' is that this request + parsing can take a little bit of time, and even more time if internet is a little bit slow. So, I decided to do it in a background thread.I have a method, let's call it GetXmlInfo(), which does all the hard work. Then, I have a timer that ticks every 5 seconds. Now I have two options for starting the thread.Option 1: creating a new Thread object every time the timer ticks:
vb.net
Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles Timer1.Tick Dim t As New Thread(AddressOf GetXmlInfo) t.Start()End Sub
[code].....
In option 2, when the GetXmlInfo method has not finished yet, no new thread will be created and the GetXmlInfo is 'skipped' for one timer tick (or possibly more). Could this create a sort of "deadlock" (I know it's not really a deadlock), like if the thread never finishes for some reason?
threads that watch folders and process files. This works fine, but every time needs change to add a new folder or just change some existing rules I have to go in and change the program. I want to builda front end that storesprocessing infofor each watchfolder. The program would then read the configs and build the threads based on them. I cannot find a way to loop through creating the threads.
Currently I create a thread the following way (the normal way)
Public loginThread As Thread Public loginThreadStart As New ThreadStart(AddressOf LogIntoWhatever) Public callLoggedIn As New MethodInvoker(AddressOf loggedIn)
However, what I want to be able to do is something along the lines of (this obviously does not work, and is entirely pseudocode)
dim i as integer = 0 for i = 0 to i = 25 Public loginThread(i) as Thread Public loginThreadStart(i) as New ThreadStart(AddressOf LogIntoWhatever) next i Public callLoggedIn as new MethodInvoker(AddressOf loggedIn)
Where I could change 25 to whatever I wanted and have that number of threads created. They would all be running an identical sub which does do not make calls of any kind to each other, they don't need knowledge of each other. Is anything like this possible?
Basically with the code below I would like to create multiple threads. Let's say user input was the number 100 for the "userVal" variable of type integer. With the first pass of the loop, starting with 1, I would like to[code]...
i need to create multiple threads to improve a function on my program. it is a simple thing really but it's slow and if multithreaded it can improve the speed some thousand times. my program uses a dll to check the MX record of an e-mail from a textbox called emails if the mx record is correct then it validates that domain, if not, it sends to an error textbox with that mail my problem is that im reading a lot on multithreading and i dont have an idea how to make something like this: for x=0 to emails.lines.count -1' start a thread that runs my function next if i can limit the number of threads to avoid crashing it would be good.
I just had a new, last-minute idea on to take on a task, so I am running to StackExchange for quick help.
What I want to do is execute a series of methods right in a row, each in their own threads. I want the application to wait until all of these threads are completed, after which the program will resume. It also has to use managed threading (thread pool).
What quick examples could you provide to help me along the way? If it's too complex, what things should I know about so that I can Google it on my own?
I have a main thread which is designed to be a batch processor - it spawns a series of 'child' threads from a loop (which can vary in terms of the no of items) - see below
[Code]...
However, each of the 'child' threads could take anywhere from milliseconds to about a minute to process - depending on the complexity of each calculation. There may also be a large number of child threads - e.g 100s. Creating large numbers of child threads is not efficient. I therefore want some way to effectively queue each child thread (and cancel if the process is taking too long to complete). Autoevents seems like one way of doing this, but the only examples I can find seem to assume there are two different processes on seperate threads, not one process being repeated.
I'm testing here something, but i don't know what's the best way to do this.I have a var that tells the application the number of threads that will be started, inside the thread/function that i call i need to return/get two values i have two sub functions, each one returns an integer.Then in the end i need to sum the values.
After all threads are over, i get the SF1Total = 2 + 1 and SF2Total = 3 + 5.
I'm thinking in using shared vars, and inside the function/subfunctions update the values. It's this a correct way to do it? Other question, the threads are assync, but i need to know when the last one finishes. How do i control this? The order they finish doesn't need to be the order they were called....
I have a process that may take a while, so I decided to put it into a thread and adding some GUI (progress bar) to calm the users down while it's working in the background.The thread itself is working properly.The problem is that the cursor, which I wrapped with a 'sandglass' before using the thread ("Me.Cursor = Cursors.WaitCursor"), has stopped working, and I can't figure out why. When I debug, it's getting inside the function and relevant line - but ignore and do not change the cursor.
Attached is the code, before and after:
Before - working properly:
Private Sub PictureBox1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetReport.Click Me.Cursor = Cursors.WaitCursor If IsValidatedFields() Then
I would like some advice on multi-threading. I currently utilize two threads, one for the GUI, and the other for calculations. This works well, but I want to improve my multi-threading techniques. Currently the data is with the calculation thread, and this occasionally this will cause a slow down. I want to put the data on its own thread. Giving me three threads, Data, UI, and Calculations. This should improve responsiveness, and not be overwhelmingly complex.
My problem is, this no longer fits into the parent child relationship well. All three threads need to interact with each other. My question is, what is the better way of dealing with more than two threads, when all the threads need to interact with each other. Should I pick one thread as the ultimate parent, say the data thread (I pick this one, because it seems to me this thread will be the most free.) Then have the calculation thread and the UI thread children of the data thread. Then when I have a need for the UI thread to explicitly call the calculation thread, I do this through a function in the data thread.
In the same line of this thread Now i have a problem with the control of the ending threads. This is my
vb.net threadsEnded = 0 For j As Integer = 0 To numThreads - 1
[Code]...
Some times the application doesn't finish and if i pause the application i see that the threadEnded var doesn't have the correct value, seems like some thread doesn't update the value of the var.
I am trying to get into vb.net but having an issue getting a variable value from a class library in a different thread.
What I am trying to do is create my own Winsock type control for my own use. Once data arrives I try and pass it back to the main application so that I can read it and process it.
So far I cant get it working at all. I have tried the following
Raiseevent
I try passing the value as a variable in the raiseevent property. This triggers an error about the value being in a different thread, I always thought that it would create a new variable in the thread of the application it was raising an event in and copy the value.
Function
I have tried writing a function into the class that I call (much like how the winsock control works) so that the class can return the value to the main application.
aka strData = Myclass.GetData
in the class it returns the value and clears it from its own local variable. This however means the value returns to the main application as nothing even if I step through and see it assigning a value.
I have a created a class, which contains a sub routine.This sub routine, should ideally be accessable by objects which are not members of the class, so I would like to make the sub, a 'shared sub' (call it Sub1).
Question: I am using many threads and there will be possibly 100s of 1000s of threads all wanting to use Sub1 (which is at present, is not shared).At present Sub1 , is not a shared sub, so every time a thread wishes to use Sub1 , I am creating a (dummy) instance of the class and then applying Sub1, to that instance.
My program works flawlessly using the above technique.Question: If I were to turn Sub1 (which is not currently shared) into a shared sub, and directly had various threads calling Sub1, would there be any issues with locking, ie. would it possible for 2 threads to enter that thread and start overwriting variables, which another thread has written to?
NOTE: there are no shared variables/properties in Sub1 and all variables used in Sub1 are declared within the sub itself.
I have got my multi threaded application running which is a Windows Service Project.
Basically when the service starts it starts a thread listening on a port for connections. The reason I started this on a new thread was so I could easily control it and not lock up that main worked thread.
Now on that thread when a client connects it dynamically assigns them another port number that is not currently in use and starts a new thread listening on that port and waits for the client to connect to do the actual data crunching.
So basically I have my main project thread, a Main listening thread then the possiblity of 0 to 100 more threads for each connected client.
Now this project should always be running but in the event it needs to be shut down (or server rebooted etc) I want to be able to shutdown the threads safely.
I can down my main listening thread safely but my trouble is all those other possible threads. What I would like is if the service is called to stop I want any of those other possible running threads which would have an active connection to the client to send a simple command like 'STOP' then close the connection gracefully (MyConnection.Close) and end itself.
The only solution I can think of which I don't really want to do is have a global varible (StopThreads as Boolean) and after each command in the thread check that boolean and if its set to true jump to some code to do what I want and end the thread but there must be an easier way? Or is this the best way?
Frankly, NONE of this makes sense to me..[URL]..They just jump right into code without baby steps that explain the situation.Every other aspect of VBnet has been well spelled out to me and I get it -- but this business of using the serial port and going nowhere because of a cross thread error has me 100% baffled.
If anyone can point me to a tutorial that really explains it simply and well (without jumping into friggin' code).Even in the above link, they show 3 different techniques of addressing the same problem ! (Like I CARE
I want to create a set amount of threads, and have those threads open the same file, and then each thread will go read a different line all the way down to the end of the file. Also I do not want to use thread pool, so I have to create the threads. I have the code to create threads, but I need help on the file part:
Is it safe to update an SQL database from multiple threads at the same time? I have a method which runs on up to 25 thread pool threads at the same time and it attempts to write to a text file and update an SQL database - I assumed that writing to the file would not work properly if all threads tried to write to it at the same time, so I have used SyncLock for that part of the method. Just wondering if I need to do the same thing for the part of the method that updates the SQL database? or is the framework (or SQL server) smart enough to either execute multiple update statements on the same table at the same time without a problem or queue them?
While the processes are executed (spends some seconds or minutes in completing all the tasks) I show form "Please wait to complete all tasks".
But one of these processes I have to show a form because the user has to choose a few data. But the problem is that this form is showed minimized.
Already I have tried to show it maximized and to bring it to the front but it does not do it.
how I can show this form to front and maximized?
My actual code is:
f= New frmLPlantillas f.WindowState = FormWindowState.Maximized f.BringToFront() If f.ShowDialog() = DialogResult.OK Then value1= f.xxxx value2= f.zzzzz value3= f.aaaa value4= f.bbbbbb Else Return False Exit Function End If
I am trying to stop the following piece of code from taking up too much processing power of the PC by using threads. But I keep getting errors when I use threads:
This is a very simple piece of code involving threading and events. I am just refreshing the old grey matter as I find myself off work sick for a period of time.
I have a function which does some processing. Lets name that function DoWork. This function takes some inputs as structure and a string. Now I want to start two threads which would read some elements in an array and then call DoWork with some data and value of that element. DoWork would do processing and thats it. I want two threads to run side by sid.
So I tried to write code but kinda failed. I wrote a function which starts a thread and calls DoWork. Now DoWork accepts structure and string etc. But when we do say abcthread = new threadig.thread(addressof <sub name>). here we cant pass any-value except object. That was first problem.
Anyway, main problem is after the thread has finished processing DoWork, how do I find that out and tell that thread to take another element from array and start again. This has to be managed for both the threads. I need to find out which thread has finished first and tell it to start again with another value. I have usually used one thread but not two in this way.
I need to run TWO Threads in a web application, .NET. 3.5 Both methods MakeRedCars and MakeBlueCars need to run at the same time as both take 5 seconds. I have tried many example codes with very bad results. I dont need anymore than 2 threads. The main issue I am having is knowing how to wait till they are finished and also how to store the results from each.Each of the Methods do different things, one is a web request, one is a sql call.
Both methods return a list of(cars) for example do:
carList.addrange(MakeRedCars()) carList.addrange(MakeBlueCars()) for each car in carlist<br> response.write(car.colour + "<r>") next
I have implemented the answered below and with empty objects all works well, as soon as you hook in a database call that returns the data/objects studio does a JIT and crashes.
I can only run "so many" threads concurretnly, a notion which I have seen elsewhere.As a threading novoice, how can I determine the maximum number of threads to use? Or is this a "how long is a piece of string" question? What does it depends on? Hardware config or what?
I am having an issue where I need to implement a type of thread scheduling... I'm looking to implement a list of tasks, each with a set time that they need to execute, and after the time is up they will execute the respective code that goes with that task. I would use a timer based solution, however, I don't think that it would be very efficient. There will be a very large list of tasks, some of which need to be executed within seconds of being placed in the list.
I have 2 thread.One that put some data inside a list?the second that take out these data from the same list.what is the best "list" that allows one thread to put something inside and another one that take stuff out>