|
Hello!
I have an array of MemoryStreams. The main thread is updating one element at a time and the other threads are iterating over the array and checking if they are free to read the elements. If a MemoryStream is locked in the array, they won't read it and continue to another MemoryStream in the array.
How can I implement this scenario? I'm using the array as a buffer, the threads are reading from it and sending data over network, that's why I can't allow the main thread do the work of reading and sending the data. I need to make the other threads recognize and not read the element that is being updated.
Thank you
|
|
|
|
|
Hi,
szolDat wrote: I need to make the other threads recognize and not read the element that is being updated.
you could write a small wrapper around the stream to do this:
public class StreamLocker
{
private MemoryStream _stream;
private bool _isLocked;
public StreamLocker(MemoryStream stream)
{
_stream = stream;
_isLocked = false;
}
public MemoryStream LockStream()
{
lock (_stream)
{
if (_isLocked)
return null;
return _stream;
}
}
public void UnlockStream()
{
lock (_stream)
{
_isLocked = false;
}
}
}
Robert
|
|
|
|
|
Just my though.
However, there is two things wrong with your class.
First, you forgot to set _isLocked to true in the LockStream method.
Second, you should add an object to use for the locking. As the MemoryStream object is reachable outside the class, it could be used in a lock statement outside the class, which could cause a deadlock situation. Using a private object that isn't available outside the class prevents that.
---
"Anything that is in the world when you're born is normal and ordinary and is just a natural part of the way the world works. Anything that's invented between when you're fifteen and thirty-five is new and exciting and revolutionary and you can probably get a career in it. Anything invented after you're thirty-five is against the natural order of things."
-- Douglas Adams
|
|
|
|
|
Thank you for the answers.
What happens if a thread checks if a MemoryStream is available, sees that it is, and starts reading it, then the control is passed to the main thread. The main thread starts writing it, and if it gets to the point where the other thread is reading it, then that thread will return false data.
If I lock on a private object and not the MemoryStream itself, then how can I make sure that no one is reading that object?
|
|
|
|
|
szolDat wrote: What happens if a thread checks if a MemoryStream is available, sees that it is, and starts reading it, then the control is passed to the main thread. The main thread starts writing it, and if it gets to the point where the other thread is reading it, then that thread will return false data.
The main thread has to follow the same rules as the other threads, so that only one thread at a time has access to a specific memory stream.
szolDat wrote: If I lock on a private object and not the MemoryStream itself, then how can I make sure that no one is reading that object?
Using the lock statement on the memory stream doesn't protect the memory stream in any way. The object that is used in the lock statement is only used as an identifier, and the only thing that is protected is the code inside the lock statement block.
---
"Anything that is in the world when you're born is normal and ordinary and is just a natural part of the way the world works. Anything that's invented between when you're fifteen and thirty-five is new and exciting and revolutionary and you can probably get a career in it. Anything invented after you're thirty-five is against the natural order of things."
-- Douglas Adams
|
|
|
|
|
"Using the lock statement on the memory stream doesn't protect the memory stream in any way. The object that is used in the lock statement is only used as an identifier, and the only thing that is protected is the code inside the lock statement block."
I think I get it. So the only way to restrict access is to force all threads to execute the same block of code for accessing the elements of the array, for example using the same accessor method or property? There isn't any way to lock objects in a memory, like you lock files with different reading / writing permissions? (I mean something like the System.IO.FileAccess.Read/Write/ReadWrite enum.)
|
|
|
|
|
szolDat wrote: So the only way to restrict access is to force all threads to execute the same block of code for accessing the elements of the array, for example using the same accessor method or property?
Yes.
Well, the crucial thing is the object that is used in the lock statement. There can be different blocks of code that access the array as long as they use the same object for locking.
szolDat wrote: There isn't any way to lock objects in a memory, like you lock files with different reading / writing permissions?
No.
Well, there is of course memory protection support in the processor, but that's on a completely different level.
---
"Anything that is in the world when you're born is normal and ordinary and is just a natural part of the way the world works. Anything that's invented between when you're fifteen and thirty-five is new and exciting and revolutionary and you can probably get a career in it. Anything invented after you're thirty-five is against the natural order of things."
-- Douglas Adams
|
|
|
|
|
Thank you for your kind help!
|
|
|
|
|
HI,
i've got a simple APP with 2 Buttons :
Button 1 = Display 20 Leds & Move 20 TextBoxs To left
Button 2 = Hide .......
I've Got a Delay of 1-2 Sec when i press Button 1 Till i see Changes
My Computer is fine (enough Free Memory & etc.)
is there any method that can help me (Beside of "This.Refresh()")
THNKS In Advance
Have Fun
Never forget it
|
|
|
|
|
Hi,
this.Refresh will try to refresh all form... If you call it only on led containing panel will be less work...
Second, you're moving too many controls... Why don't you create a custom control using GDI? Securely if will be more faster!!
Visit my blog at http://dotnetforeveryone.blogspot.com
|
|
|
|
|
Got u
i'll Try It
THNKS
Have Fun
Never forget it
|
|
|
|
|
Hi All,
How can I record and play sound file in mp3 format ?
|
|
|
|
|
|
Hi all
I wount to ude the notify control
iam do this steps
1 add the control to the main form in project
2 determine the properites to it
but the control is not show in window in executing time
what is the problem or what is the step/s idont know and i dont do it in my project
thanks for any body hellp me
Thaer
|
|
|
|
|
Hi!
Did you set the Visible property to true?
Regards,
mav
--
Black holes are the places where God divided by 0...
|
|
|
|
|
thanks me for your hellp
but i set this properites to true but the control is not show in runing time
Thaer
|
|
|
|
|
Only making sure we're talking about the same thing:
You're speaking about "Notify Control", but there's no such thing. Do you mean the NotifyIcon control?
Did you try setting the Visible property after your form has been loaded (for example in the Load event handler?
Regards,
mav
--
Black holes are the places where God divided by 0...
|
|
|
|
|
Hi,
if you are refering to a NotifyIcon, then you should:
- add a NotifyIcon to your main form
- set its Visible property to true
- set its Icon property to some icon
- luanch your app
- look for the result in the Notify area of the task bar.
BTW: you may want to read my TrayIconBuster article.
Luc Pattyn [Forum Guidelines] [My Articles]
this months tips:
- use PRE tags to preserve formatting when showing multi-line code snippets
- before you ask a question here, search CodeProject, then Google
|
|
|
|
|
Maybe someone can point me to the right website, or even the right search phrase.
Basically, I want to make a program that takes all the paramaters and creates a program based on that.
I want a program that searches for a certain file in a certain folder, but I want these options to be variable, though I want them hard coded into the exe that is deployed.
I hope that makes sense.
|
|
|
|
|
This sounds a little like a compiler-compiler ...have you searched info on lexers and parsers? There are these programs out there called 'LEX' and 'YACC' and freeer ones called 'FLEX' and 'BISON' that create a hard coded compiler programs based on input...might be a place to start.
|
|
|
|
|
|
Hi there!
I have a question. Please Help me.
I have an Access database, the table have a field (data type Ole object). I want to input direct data (data type Ole object) into access table with C# Datagrid.
Thanks !
|
|
|
|
|
And what is exactly is your question?
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
|
|
|
|
|
I'm so sorry. My english very bad.
I want to embed access's table into datagrid. After input data on datagrid, click button save, data will be insert in access database.
Thanks!
|
|
|
|
|
Here is what I posted in your other post:
Just use a datagrid. After data is input, then you need to code the saving code on the save button. Not much else we can help you with since you did not give any real information about database schema, etc.
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
|
|
|
|