|
I don't know the details of your implementation, but I guess you're using Windows Forms.
Remember that having more than one open Form open doesn't mean they are running on different threads.
So I will give it for granted that you are running your communications code in two separate threads you created.
I guess your function library exposes functions to start the communications and callback events to signal the end of communications or errors (timeout, wrong response, etc.). You should use lock in your library's functions (or in calls to those functions, if you don't want to modify the library) in order to achieve a first level of thread-safety, but this won't solve the problem of not having the two threads trying to communicate to the same device at the same time if the communication is in progress between calls to the various library functions (i.e. if it is asynchronous). If the communication is synchrnonous, it will be enough.
If the communication is asynchronous, using thread locking to avoid concurrent access to the devices is usually not a good practice, since communications can take a long time (especially when checking timeouts), and you would keep you threads locked during all that time, thus losing all the benefits of async comms.
In that case, you should implement a (thread-safe) queue of requests coming from the two threads. Each request will be satisfied when the instrument is "free" and the result posted in another (thread-safe) queue or sent to the calling thread by means of events or similar. All this can be implemented in a "controller" class, which will stand between your calling threads and your library. The controller might also use a simple round-robin system (with priority management, if you need it) to manage access to the library.
If you want to be more OO, you can also create a class for each type of instrument and create instances for every physical instrument. These classes will expose properties and functions to read and write the desired values to/from the devices, and will stand between your calling threads and the controller class.
Hope this can be of some help.
2+2=5 for very large amounts of 2
(always loved that one hehe!)
|
|
|
|
|
rpm8200 wrote: I need to make sure that my static function library is locked if one of the threads executes read or write operations
It's my understanding that static methods are always thread safe. That if thread 1 is executing Read then thread 2 will have to wait if it calls Read.
If you want thread 2 to wait if it calls Write while thread 1 is executing Read, then simply create an object to lock on:
private static readonly object lockme = new object() ;
Then in each of your methods:
lock ( lockme ) { ... }
I don't think I've ever had to do that in static methods, but I expect it will work.
|
|
|
|
|
First of all: Thank you both for your replies on that subject.
I think I am a bit closer to finally solving my problems with semaphores. I have to do more testing but am confident up to now. I did use a codesnippet with a quite sophisticated semaphore implemented and did not work as desired. Back on msdn I reread the hints to semaphores and implemented the easiest way you can do and now seems to work.
Moreno Airoldi: Thank you for your hints. I am no C# crack but sure I know that creating a new window (I only do this for testing purposes -> the second thread later on won't have own window) does not mean to have a new thread. I forked off a thread creating that window. After first semaphore approach did not work and I almost tried anything (I at least thought so) I doubted my own implementation (really different thread??? ) and additionally did read out the thread IDs ... and... they are different (after I knew this definitly I've been wondering why I really doubted that )) ). To make long story short: Definitly there are two different threads with two different thread IDs (that I can also identify in MSProcessExplorer what I also did)
The problem why I want to lock the whole function library is not because two threads are communicating to the same device (wouldn't be a problem at all), the point is that two threads might communicate to the same object on my hardware controller. Especially the command handling is critical. A command is written to a reserved data object in the shared memory of that controller. If values (= Commands) change, the controller will examine the new command, copy it to its command register and quit the command by writing something in another shared memory as a reply. So if one thread "sends" a command and is interrupted anytime before it is acknowledged, that that command has been accepted by the hardware controller, it might happen that the other thread starts to write to the shared memory and corrupts the command.
The application on PC is a "simple" visualisation of what is going on at my hardware controller. I believe it will be no problem to lock the whole funclib while a command is wtitten (there are also other shared memories that can also be read or written that I don't want to be corrupted, only half updated and then read out by other thread aso....). Integrity/correctness of Data is primary goal.
As you pointed out, this is no good practice, but untill we have to manage communication by shared memory objects (and using another function library by the manufacturer of the controller to access that memories) this seems most practical solution (don't really need more). We have implemented timeouts to avoid deadlocks.
We are planning to do communication by ourselves and creating a tcp protocol to do communication. We wont use shared memory then and I will definitly do more work to accomplish this goal in a more OO way
PIEBALDconsult: I did already use lock to lock the single functions. Unfortunatly this is not enough as I also wanted to implement a handshake to the communication. Goal is to encapsulate "sending a command" (and other read/write operations) in a function library so that any software engineer working on that project can easily use it without dealing with eventhandling. So what I try to do is to force my funclib to do asynchronous communication synchronously (just for the goal to achieve usability and stability). Locking the single functions is not enough (unfortunatly). I really have to lock the whole static object (that might be kind of "thread-safe" itself but not in conjunction with the hardware it communicates with and the communication procedure that it has to follow).
Thank you both once again for writing me a reply! Its always interesting and helpful to know how other software engineers approach such a problem. Keep on coding!
PS: Moreno Airoldi: 2+2=5 ... can't google the solution -> whats it about?
|
|
|
|
|
Hi Guys,
I have a table that contains 100 disticnt record.(Table1)
I have a second table that contain 40 records(Table2).
when i run a script to get me a records that are in Table1 not in Table2.
Select * from Table1 where field1 not in (select field1 from Table2)
I should have at least 60 records.However ,it doesnot return anything?
Can you please advice?
Thank you very much.
|
|
|
|
|
Could we have the descrition of the 2 tables
|
|
|
|
|
Many thanks for your reply.Its Much appreciated
Table1
id int
Cli Nvarchar(200)
Timestamp datetime
100 records
Table2
id int
Cli nvarchar(200)
40 records
Select distinct(cli) from Table1 where Cli not in (select Cli from table2)
This should at least return 60 records.
Regards
Select
|
|
|
|
|
|
<blockquote class="FQ"><div class="FQA">kibromg wrote:</div>Select distinct(cli) from Table1 where Cli not in (select Cli from table2)</blockquote>
Select distinct(cli) from Table1 where Cli not in (select Cli from table2 where Cli is not null)
FTFY, SQL server has some floopy processing when it does 'not in' queries.
The true man wants two things: danger and play. For that reason he wants woman, as the most dangerous plaything.
|
|
|
|
|
and where does this happens: in code or in database
if it is in code try running it in some plsql developer and see what you get
|
|
|
|
|
Thanks very much for your reply.It runs on a database.
|
|
|
|
|
Select distinct(cli) from Table1 where cli not in (select Cli from table2)
try this with small c in second cli
|
|
|
|
|
Still the same.I have tried that also.
|
|
|
|
|
Have you got nulls in your fields? That would cause it.
Bob
Ashfield Consultants Ltd
Proud to be a 2009 Code Project MVP
|
|
|
|
|
Yes Please.when i select distinct(cli) there is one record NULL.
When i try to remove it delete from Table1 where cli like NULL it does nothing.
Can you please advice how can i remove it?
|
|
|
|
|
You cannot do like null (or = null either), you have to do
where cli is not null (or cli is null depending on whic you want)
Bob
Ashfield Consultants Ltd
Proud to be a 2009 Code Project MVP
|
|
|
|
|
this can help
select table1.cli from table1 left join table2 on table1.cli=table2.cli
where table2.cli is null
|
|
|
|
|
This is a SQL question. I would suggest you break it down to try to work out why it doesn't work.
Christian Graus
Driven to the arms of OSX by Vista.
"I am new to programming world. I have been learning c# for about past four weeks. I am quite acquainted with the fundamentals of c#. Now I have to work on a project which converts given flat files to XML using the XML serialization method" - SK64 ( but the forums have stuff like this posted every day )
|
|
|
|
|
Yes Please.when i select distinct(cli) there is one record NULL together with other records
When i try to remove it delete from Table1 where cli like NULL it does nothing.
Can you please advice how can i remove it?
|
|
|
|
|
Hi Christian,
Can you help me with a script on how to check and remove any Null script from a table.
Many thanks
|
|
|
|
|
You should try IS NULL instead of LIKE NULL
|
|
|
|
|
What is C#'s equivalent to VFP's CursorAdapter?
|
|
|
|
|
Depends, what does it do ? You can set the cursor with Cursor = Cursors.xxx
Christian Graus
Driven to the arms of OSX by Vista.
"I am new to programming world. I have been learning c# for about past four weeks. I am quite acquainted with the fundamentals of c#. Now I have to work on a project which converts given flat files to XML using the XML serialization method" - SK64 ( but the forums have stuff like this posted every day )
|
|
|
|
|
Hi,
I think you want to open a database cursor, aren't you? Then you're search for class:
System.Data.Common.DbDataReader
or if you're searching e.g. for MS SQL Server:
System.Data.SqlClient.SqlDataReader
or OleDb:
System.Data.OleDb.OleDbDataReader
or Odbc:
System.Data.Odbc.OdbcDataReader
king regards
|
|
|
|
|
|
How to connect to MS Access Database and display result to grid?
|
|
|
|