C
C#Boggo

❔ Cross Thread synchronization of locks

In over my head, I am trying to use a lock to pause some code on one thread, and i want to release (Monitor.Exit()) from another thread. How do i synchronise this?
UU
Unknown User404d ago
B
Boggo404d ago
im not aware of how to synchronise because i am exiting from another thread that the lock was entered on, its throwing up an error
UU
Unknown User404d ago
B
Boggo404d ago
B
Boggo404d ago
so this runs on a thread the cw will be taken out, its just for testing purposes then i call _halter.Exit(); from another Thread
UU
Unknown User404d ago
B
Boggo404d ago
queue
UU
Unknown User404d ago
B
Boggo404d ago
the idea of the locks was that it wasnt gonna infinitely run a while true loop
UU
Unknown User404d ago
B
Boggo404d ago
how does channel work?
UU
Unknown User404d ago
M
MODiX404d ago
see $channel
UU
Unknown User404d ago
M
MODiX404d ago
Exploring System.Threading.Channels
A blog about software engineering and applied cryptography.
.NET Blog
An Introduction to System.Threading.Channels - .NET Blog
“Producer/consumer” problems are everywhere, in all facets of our lives. A line cook at a fast food restaurant, slicing tomatoes that are handed off to another cook to assemble a burger, which is handed off to a register worker to fulfill your order,
UU
Unknown User404d ago
B
Boggo404d ago
i would probably agree. however
UU
Unknown User404d ago
B
Boggo404d ago
in a hypothetical situatuation where i needed to use a lock and queue since this is for school xD
UU
Unknown User404d ago
B
Boggo404d ago
is it possible like how im trying?
UU
Unknown User404d ago
B
Boggo404d ago
i should probably read up a little on locks not sure i understand this
UU
Unknown User404d ago
B
Boggo404d ago
private static void StoreMessages()
{
while (true)
{
if (_messagesToStore.Count == 0)
{
lock (_halter)
{
}
}

var msg = _messagesToStore.Dequeue();
Console.WriteLine("Message Uploaded");
_db.AddNewTextMessage(msg);

}

}
private static void StoreMessages()
{
while (true)
{
if (_messagesToStore.Count == 0)
{
lock (_halter)
{
}
}

var msg = _messagesToStore.Dequeue();
Console.WriteLine("Message Uploaded");
_db.AddNewTextMessage(msg);

}

}
i have changed now trying to edit myself
private static void StoreMessages()
{
while (true)
{
Monitor.Enter(_halter);
try
{
if (_messagesToStore.Count == 0)
{
Monitor.Wait(_halter);
}

var msg = _messagesToStore.Dequeue();
_db.AddNewTextMessage(msg);
}
finally
{
Monitor.Exit(_halter);
}
}

}
private static void StoreMessages()
{
while (true)
{
Monitor.Enter(_halter);
try
{
if (_messagesToStore.Count == 0)
{
Monitor.Wait(_halter);
}

var msg = _messagesToStore.Dequeue();
_db.AddNewTextMessage(msg);
}
finally
{
Monitor.Exit(_halter);
}
}

}
heres original it holds it until i exit it from the other thread
UU
Unknown User404d ago
B
Boggo404d ago
someone else mentioned reset events too, i had a look but im pressed on time for this so looking for something simple as possible
UU
Unknown User404d ago
B
Boggo404d ago
no clue, its my coursework its due friday. Teacher suddenly said he wants it done haha
UU
Unknown User404d ago
B
Boggo404d ago
i need to finish the solution. Then complete testing and evaluation for my school, the computer science course requires we complete a full project Analysis, design, implementation, testing and evaluation
UU
Unknown User404d ago
B
Boggo404d ago
not sure, but i would be using months to measure not years
UU
Unknown User404d ago
B
Boggo404d ago
i have played about with it for maybe 1.5 years, but for the first year i was playing with strings and arrays
UU
Unknown User404d ago
B
Boggo404d ago
ok this is manual?
UU
Unknown User404d ago
B
Boggo404d ago
yes this one, so i use either or, or do i need both
B
Boggo404d ago
AutoResetEvent Class (System.Threading)
Represents a thread synchronization event that, when signaled, resets automatically after releasing a single waiting thread. This class cannot be inherited.
B
Boggo404d ago
i am reading here
UU
Unknown User404d ago
B
Boggo404d ago
so theres not a huge difference a bit like having a using statement rather that calling .Close() ?
UU
Unknown User404d ago
B
Boggo404d ago
oh, forget my question then haha
UU
Unknown User404d ago
B
Boggo404d ago
so i need manual i believe
UU
Unknown User404d ago
B
Boggo404d ago
i may have 10+ threads all adding to queue
UU
Unknown User404d ago
B
Boggo404d ago
ok i see. I will look at channel again, hopefully i get my head around it pretty quick
UU
Unknown User404d ago
UU
Unknown User404d ago
B
Boggo404d ago
ok sure, ill try plug it in and see what happens. But not gonna spend too long if it doesnt go well
UU
Unknown User404d ago
B
Boggo404d ago
yes, will try this first
UU
Unknown User404d ago
B
Boggo404d ago
i know a little about this i used to use await Task.Run(() => SomeAsyncTask)
UU
Unknown User404d ago
B
Boggo404d ago
i know haha it was very bodge job code
UU
Unknown User404d ago
B
Boggo404d ago
but i said, i am still new and learning. This is my first proper project
UU
Unknown User404d ago
B
Boggo404d ago
oke doke appreciate your time fingers crossed all goes well So i was being dumb i fixed using Locks and monitor i just never synchronised the two threads by entering on both only entered on one thread where i waited from pule i need to enter then pulse then exit i just pulsed
UU
Unknown User404d ago
B
Boggo404d ago
im aware, my bad
UU
Unknown User404d ago
MM
Mayor McCheese404d ago
Channel ftw
A
Accord403d ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server
More Posts
❔ Hi guys, help me, please with Asp.net coreHi guys, help me, please, I create a project on the C# ASP.Net Core But but I don’t create these fil❔ I need help running a github repositoryHello I am trying to run this project code I found on github, but it's not working. Does anyone have❔ How can I mock a Service class being used in Component class?The service also initializes a dapr component in it's constructor. I want to unit test a function of❔ Need help with Node LinkedListsThis is my first time using non built-in Lists so im a bit lost here; This is the task: Make a lisBatch script issue with spaces in stringsThe script:```cmd REM This script copies your project's outputs into the game folders. Then it ru✅ How to add data to a list that is outside of Main()?```cs using System.Collections.Generic; internal class Program { public static void insertData(s❔ SQLite name space stopped workingdownloaded the packages and was all working until i complied in unity and it stopped working✅ OpenFileDialog issueIm trying to make something like word, however i cant even make the GUI, since i found a issue with Properties.Settings.Default is not working (saying that it does not exist in the current namespace )So basically what I wanted to do is grab variables from .settings file in my app, but it does not woI want to check the program test.exe stops, can't run the main program.If I want to check if the program named test.exe has been stopped, test2.exe can't be run. What shou❔ What is easiest way to have the option to keep config file from last install with Wix Installer?I am using the Wix Toolset technology for my installers and I am wondering what the easiest way to m❔ Connection request timed outConnection request timed out in API❔ I'm trying to download a link in 4 chunks via HttpClient but it keeps breaking.Link to my GitRepository https://github.com/Error1OH1/FileDownloader I've been trying to download in❔ Nedd help with callbackdata telegram```cs [TelegramCommand("ping", false, false, "BUTTON:Ping")] public static async Task Ping(UMaybe someone knows it...I have a problem with Word and i need to fix it because its a part of my presentation...Properties.Settings not workingI can not get this to work I need it for my settings file and im using net core so that might be a pIs this bad practice?Is using a static property to get a singleton's instance variables something I shouldn't do? I'm ju❔ How would you write this service better?The service:❔ Passing around thread to perform work on?I'd like to call a method that runs on the main thread but offloads a portion of it's work onto a paClass library, cant connect to console appHello! First time trying this so dont know how to fix or whats really wrong. - any ideas?