Sharing Object Over Multiple Threads?

Aug 4, 2010

I have an application that needs to perform a series of different actions at specific points in time.To do this I have a timer with various actions that occur in its tick event.One possible action is to send out a few messages via a serial port and then wait (between 1- 300 mins typically) for the next timer tick.Another possible action is to continuously toggle one of the control lines on the serial port until the next tick (again 1-300 mins).

In most instances, i'm happy for everything to run on the main thread, but in the case of toggling the line endlessly, I thought it better to give it its own thread.My problem is that for the new thread to use the Serial Port, it must open the port. Likewise, it must close it before the main thread can have it back.

Here are 2 code snippets

Private Sub activityTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles activityTimer.Tick
Dim some variables here
activityTimer.Stop()
If speedThread.ThreadState = ThreadState.Running Then

[code]....

View 4 Replies


ADVERTISEMENT

Sharing Data Between Multiple Threads?

Jul 7, 2011

I know a little about threading and delegates, as well as socket programming. I'm working with a VB.NET-compatible SDK called TrueVision3D to create a small online game for me and my friends. The problem is that I cannot for the life of me figure out how to pass data from one thread to another. I've created 2 seperate threads on seperate forms.

The first thread is created on a form titled FrmLogin, and it contains all the socket code. I need to be able to update variables in the second thread, which mainly consists of a render loop. The second thread is created on a form titled FrmMain. I'm not quite sure if it matters what forms the threads are created on, I'm just trying to give as much information about the issue as I can. I've tried using delegates and events to pass information from the thread back to the form it was created on, which works fine, but how do I pass data to the second thread from the first?

View 1 Replies

Synclock Doesn't Prevent Sharing Errors When Multiple Threads Access Same File

Apr 14, 2010

I am using Synclock to prevent multiple threads from logging text messages to the same file at the same time, but this doesn't seem to work:I get frequent sharing errors on the logfile, within the synclock block.The Synclock is using the variable holding the filename, because it exists all the time and because it is always the same object, whatever class instance or thread I'm in.[code]Despite using Synclock and a Using block around the code, I keep getting this error in the sw.WriteLine(Msg) line:System.IO.IOException: The process cannot access the file '(filename)' because it is being used by another process.Should I use some other type of object than a string to synchronize on?I cannot use a user interface object (button or so) because it is a windows service project.It was written and compiled in Visual Studio 2008, but targeting the 2.0 framework because it has to be able to run on some old Win2000 machines.

View 12 Replies

Have Multiple Threads Run Through An Arraylist Of Object And Process Each Object?

Apr 30, 2009

I am trying to have multiple threads run through an arraylist of object and process each object. My problem is that each thread processes every object in the array.So they are not working together on the job. I made sample application to show my problem. I think the problem is somewhere in my ThreadSub() Here is the

Imports System.Threading
Public Class Form1
Dim myitems As New ArrayList

[code]....

View 2 Replies

Sync Locks - Manage Access To A Collection Object From Multiple Threads - Add, Update, Remove, Clear

Oct 20, 2009

I am considering a piece of code that will manage access to a collection object from multiple threads - add, update, remove, clear. OK ... so I have put a sync lock within each method, but what is not clear from the MS documentation is exactly how the sync lock is behaving. Is it locking the piece of code it wraps? Or is it locking the object? And if it is locking the object a private static object should lock out any code locked on the object within all instances of the object? i.e. if an add operation is underway, an update, delete or clear will wait for the lock to be released.

Finally, if I have multiple threads contending for the sync lock, how is the release managed? First come, first serve? random (from my p.o.v)? Is there a chance that two separate threads could blunder into 'locked' code simulataneously?

View 2 Replies

Multithreading - .NET 4.0 Execute Multiple Threads But Threads Are Completed Before Resuming?

Oct 19, 2011

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?

View 1 Replies

Pass Multiple Parameters To Multiple Threads?

Sep 15, 2011

I have a loop that creates several threads as such[code]...

View 1 Replies

Forms :: Sharing Data By Declaring Object

Mar 31, 2010

I have two forms A and B. B is a child of A - i.e. I have a button on A that pops up form B. I want B to be able to access and change data (e.g. an instance of an object etc.) in A and also to call some function in A. At the same time I'd like to preserve the right to alter the data in A without opening B. I've achieved this using the following (i.e. by declaring the object and method as Friend):

Public Class frmA
Friend _data as Hashtable
Friend Sub GetData()
If _data IsNot Nothing Then
_data As New Hashtable

Is there anything inherently wrong with the what I've done and if so how can I achieve a similar result? (It seems to work - I can read, add to and change the values in the _data Hashtable from within form B and when B is closed the values persist i.e. I can access the changed values from within form A. I just dont know if its a horrible fudge).

View 2 Replies

.net - Sharing Mdb Access Connection Among Multiple Forms?

Sep 7, 2010

I'm starting to put in a database into my application, however I'm drawing a blank on how to share a database connection among the dozen or so different forms in my MDI application. I'm assuming this has to do with interfaces or something but I can't find any relevant examples anywhere. Can someone help me out? Ideally what I'd like is when the app is loaded up there is a call to a function in the forms loading area which establishes a single connection to the mdb, that I can then call via any form so I don't always have to open/close connections everytime I need to update the db?

Here's a basic example of the mdb database access code I've got working:

Dim dt As DataTable = New DataTable()
Dim OleDbTran As OleDbTransaction = Nothing
Using connJET As OleDbConnection = New OleDbConnection("connection string here...")

[code]....

View 1 Replies

Sharing Structures Of Arrays In Multiple MDIchilds

Nov 10, 2010

I have the following code... in an MDI application. [code] Various lines of code which put data into the Myinstance and then save the instance into the ArrayList.I have buttons which run procedures and functions in the formchild and also in the Class1. I need it so you can jump between formchild instances, and when you do so and click a button on that form, it will run the Class1 code but using the data in the arraylist and myinstance that was set previously by the selected formchild. Then if you switch to another child it uses data from this form. Can this be done by declaring the arraylist with more dimensions and then refer to the records with the index of the selected form instance, e.g.Selected Child is index(3)

View 1 Replies

Sharing Structures Of Arrays In Multiple MDIchilds?

Nov 6, 2010

I have created an MDIparent form and a MDIchild. There is a button on the parent which makes multiple 'clones' of the child form, each time its clicked.I also want (maybe with a module) a set of variables, and array of a structure but these are available to each copy of the child, but each copy's variables, arraylist etc contain its own data?Therefore if you select child1 the variables and array will have different data to child2, child3...etc...

View 3 Replies

Multiple Full-Screen Forms Sharing Data?

Sep 13, 2011

My question is in two parts:

1) I am writing an application for a touchscreen which will have several different Forms - each of which will be full-screen. What is the best way to achieve this? Hide/show each Form as the user navigates between screens? MDI Parent with multiple Child Forms becoming visible 'on top'? Or is there a more elegant solution?

2) I would also like a 'banner' on the top of each Form (or always visible on the top of the MDI Parent, etc) similar to a 'navigation frame' on a website. This banner will display a clock with the current time, and the current GPS location (meaning that a Timer will be used to get/set these values every 1000 milliseconds). I thought of using a UserControl for the banner - but if this is used on multiple Forms then I will end-up with multiple Timers all waiting 1000 milliseconds to update essentially the same information.

Is there a best practice for this type of scenario? - my thoughts so far are using the "Main" Form for Global Timers, etc - but then how do I update all Form clocks each second? (without iterating through all visible Forms... which seems messy).

View 5 Replies

Sharing Data Between Multiple Projects In Single Application?

Jun 11, 2011

I have a windows forms application with one exe and several dlls(Class libraries) in a single solution. The application uses common data that is used across all the dlls. I would like to load the data when the application is starting up and use the loaded data at various points in the dlls so that I do not have to load the common data again and again. How can I share the data loaded in main EXE across the DLLs?

View 2 Replies

.net - Using GDI+ With Multiple Threads?

Apr 18, 2010

it would be best if I just copy and pasted the code (it's very trivial).

Private Sub Main() Handles MyBase.Shown
timer.Interval = 10
timer.Enabled = True
End Sub

[code]....

An exception, "The object is currently in use elsewhere", is raised during the tick event.

View 1 Replies

How To Run Multiple Threads With BackgroundWorker

Jan 13, 2010

I am writing a project in Visual Basic where I need to run several instances of the same method in parallel. I can do this using BackgrooundWorker by having several (4) copies of my code module (modules 1, 2, 3 and 4) with a call to background worker in each. I then have a Main form which invoke the 4 modules which setup 4 simultaneous background threads. Seeing as the code is the same I want to simply invoke the same module 4 times.

However when I do this I get an error from Backgroounworker that it is already busy. As I understand it this means that it is being shared. However, I have specifically instantiated it as "private" (private with events worker as new backgroundworker). I have tried running the same code in a Class instead of a Module but unless I make it public I have no way of invoking it from Main. I need to be able to pass each thread data from main for it to work on and if the class is private I cant invoke it.

View 2 Replies

How To Stop Multiple Threads

Nov 6, 2010

This code will show you how I started multiple threads:

For i As Integer = 0 To 10
Dim MyThread As New System.Threading.Thread(New System.Threading.ThreadStart(AddressOf MySub))
MyThread.Start()
Next

Now I'm trying to stop the threads before closing my application but I don't know how to do that?

View 6 Replies

Run Ffmpeg With Multiple Threads?

Jan 25, 2012

how to convert videos files using vb.net and ffmpeg. But as far as i know we cannot use multiple threads to run same exe file to convert files. Do you know how i can convert multiple videos using one external exe file (ffmpeg)?

I have not tried because my computer time was over. So iam just asking a general doubt whether if we attach it to one process then wouldn't the process get locked? Then can we multi-thread this application or not ?

View 1 Replies

VS 2008 - Multiple Threads - Get Two Value?

Jul 28, 2010

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.

Example:
Thread1/Function
SF1 - Returns 2
SF2 - Returns 3

Thread2/Function
SF1 - Returns 1
SF2 - Returns 5

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....

View 4 Replies

Value Of That Object Internally Within My Class From Different Threads?

Oct 22, 2009

Lets say I have got a class level boolean, defined like so:

vb Private _ServerRunning As Boolean = False

and it is exposed like so:vb Public ReadOnly Property ServerIsRunning() As Boolean
Get
Return _ServerRunning
End Get
End Property

Do I need to worry about thread safety when modifying the value of that object internally within my class from different threads?

View 4 Replies

.net - Update From From Multiple Threads Of The Same Class?

May 31, 2012

I have this class and I have a loop that launches multiple threads from the same class. This is all done from the main form.Now I want to update the richtextbox on the main from these classes.I have tried those begininvokes etc etc and nothing worked, no error but no output as well.Here the code for launching the threads :

Private PingObjects(100000) As Account 'Account is the class and login is the sub in it...
PingObjects(I) = New Account
Threads(I) = New Threading.Thread(AddressOf PingObjects(I).login)

[code]....

And to update the rtb, im using MainForm.log.text = "....." Nothing happens, no error.I have tried using the begin invoker method as well.

View 2 Replies

Creating And Aborting Multiple Threads

Sep 28, 2010

To get pass the cross referencing error when running a thread, I use this code

[Code]...

But the AddressOf doesn't accept parameters... So how do I get pass that? Or is there another way to use delegate?

View 6 Replies

Dividing Work Into Multiple Threads?

Aug 10, 2011

I've read a lot of different questions on SO about multithreaded applications and how to split work up between them, but none really seem to fit what I need for this. Here's how my program currently basically works:

Module Module1
'string X declared out here
Sub Main()

[Code]....

This is only a rough outline of what actually happens in my code by the way.

My problem being that if multiple threads start running Main2(), they're dealing with the same X value as in the other threads. The loop inside of main2 executes multiple times per millisecond, so I can't just stagger the loops. There is often duplication of work done.

How can I properly divide up the work so that the two threads running simultaneously never have the same work to run?

View 3 Replies

Implement Multiple Threads In Application

May 12, 2010

I need help to implement multiple threads in application.Assume ds.table(0) has 5 and sometime 6 rows rows for i=0 to ds.tables(0).rows.count-1.I have to apply multiple threads, means all five/six rows should start at once using thead and "Process 1" .."Process 4" should also handled by threads.This is hierarchy that I have to apply with threads, how can I do it. It is requested to put piece of code so I can understand better.

View 13 Replies

Using Multiple Threads For Faster Execution?

Jan 27, 2012

I have a map image with data associated with the map indicated by RGB index. The data has been populated into an MS Access database. I imported the information in the database into my program as an array and sorted them to go in the order I want the program to run.

I want the program to find the nearest pixel that has a different color from the incumbent pixel being compared. (Colors are stored as string attributes of object Pixel)

First question: Should I use integers to represent my colors instead of string? Would this make the comparison function run significantly faster?

In order to find the nearest pixel of different color, the program begins with all 8 adjacent pixels around the incumbent. If a nonMatch is not found, it then continues onto the next "degree", and in this fashion, it spirals out from the incumbent pixel until it hits a nonMatch. When found, the color of the nonMatch is saved as an attribute of incumbent. After I find the nonMatch for each of the Pixels, the data is re-inserted into the database

The program accomplishes what I want in the manner I've written it, but it is very very slow. After 24 hours, I am only about 3% through with execution.

Question Two: Does my program behavior sound about right? Is this algorithm you would use if you had to accomplish this task?

Question Three: Would it be appropriate for me to use threads in order to finish execution of the program faster? How exactly does that work?

Question Four: Would it be more "intelligent" for my program to find the nonMatch for each pixel and insert it into the database immediately after finding it? (I'm making a guess that this would be good in multi-threading, because while one record is accessing the database (to insert), another record is accessing the array of pixels (shared global variable in program).

Question Five: If threading is a good idea, I'm guessing I would split the records up into more manageable chunks (i.e. quarters), and have each thread run the same functions for their specified number of records? Am I close at all?

View 3 Replies

VS 2008 Multiple Threads Ending?

Jul 29, 2010

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.

View 5 Replies

VS 2008 Shared Sub And Multiple Threads?

Sep 4, 2010

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.

View 7 Replies

.net 2.0 Multiple Programs Each Running Threads On Xp, Will .net 2010 Be Better?

Oct 2, 2010

In the past few weeks, to speed up our database freshening process, I've created a couple of extra programs, basically copies of the exe file. The program itself, accesses web pages, images etc and uses threads to do so. The second program uses LIMIT 300,100 and the third LIMIT 600,100, to make sure they don't work on the same records.

Before the last few weeks, one occurrence of the program used to run without problem, from the task scheduler all through the day. Only one occurrence of each exe can run at a time.Since theres now three programs running, .net 2.0, on xp, the program often seems to get stuck and never ends. I have to kill it from task manager.

It runs fine in the IDE I just wonder if there would be any benefit from upgrading to vb.net 2010 ?

View 1 Replies

Can Share Data And Methods With Multiple Threads

Jun 15, 2010

I've used mutiple threads in a VB.NET program but I don't know if I did it right.I would like to start about 3 threads.Can they all use the same methods?Will a separate instance of each method automatically be created for each thread?If not, how do you make sure that the data within the method that is used by one thread is not used by another thread?I would like some data to be shared by all threads.How is data shared among threads?What is the protocol for changing the data and checking for changed data?Is there a good tutorial / website that explains how to handle methods and data when using multiple threads?

View 3 Replies

Get Multiple Threads To Store Data In One Array?

Sep 4, 2009

I have a application that use threadpool, and each thread stores data in the same textfile. I now want to have each thread store the data in the same array instead and use a timer to store all current data in the array into the text file after every couple of minutes.

So what I want to do is:

1. All threads store data in one array that they all have access to

2. After every couple of minutes the data in the array is stored into the text file.

I know number 2 can be done with a timer, but is the first one possible? If so how do I accomplish this task?

View 6 Replies

Multiple Controls On Winform Mutliple Threads?

Jul 3, 2010

I've created a news ticker control, which reads from RSS feeds and scrolls the text Horizontally. The plan is to have 3 of these on my Windows form, which will each display their own information.

The control is created as a label, which is populated with the text, and then scrolls right to left using a timer, once the label gets to the end of the text, and disappears of the left side, the rss is re-read, and then the cycle resumes. The problem is that with multiple copies of this controls on my form, once one gets to the end of it's run, all three freeze while the rss is reread, rather than just the control that needs to do it.

Is there a way to run each of these controls independently (threading?)? Or is there a glitch in my code causing this? (below - some elements not included, but it should be fairly obvious what is happening!)

[Code]...

View 7 Replies







Copyrights 2005-15 www.BigResource.com, All rights reserved