|
i don't call disconnect becaus it doesn't work when closing the tab
there is no point in opening a tab and closing the connection
somehow i guess i need to be able to refere to the docked rdp and close that but i have no clue how to access it
|
|
|
|
|
If you don't call Disconnect, you're not going to be able to release the active session. When you are closing the TabPage, you need to call Disconnect[^].
This space for rent
|
|
|
|
|
At a guess you abandon the connection when it moves out of scope and therefore you have nothing to disconnect.
Member 11441622 wrote: AxMsRdpClient9NotSafeForScripting rdpWindows = new AxMsRdpClient9NotSafeForScripting();
Try moving this to a class level variable instead of a method level variable. Then the tab close event should be able to pick up the rdpWindows variable and disconnect the session.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
i tried that once before and that results in an error when doing this:
rdpWindows.Server = tabControl1.SelectedTab.Name.ToString();
rdpWindows.Disconnect();
error trown:
System.Runtime.InteropServices.COMException: 'Error HRESULT E_FAIL has been returned from a call to a COM component.'
|
|
|
|
|
Member 11441622 wrote: rdpWindows.Server = tabControl1.SelectedTab.Name.ToString();
rdpWindows.Disconnect();
So you think a string = a rdp server. The tab name is a string not an object
Member 11441622 wrote: AxMsRdpClient9NotSafeForScripting rdpWindows Declare that as a class level variable.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
I have the following setup:
public interface ITest
{
TestResult PerformTest();
}
public class Test1 : ITest
{
public TestResult PerformTest()
{
Thread.Sleep(60 * 10 * 1000);
return TestResult.Failed;
}
}
public class Test2 : ITest
{
public TestResult PerformTest()
{
Thread.Sleep(60 * 2 * 1000);
return TestResult.Passed;
}
}
public enum TestResult
{
Failed,
Passed
}
class Program
{
static List<Task> tasks = new List<Task>();
static void Main(string[] args)
{
List<ITest> tests = new List<ITest>
{
new Test1(),
new Test2()
};
foreach (var test in tests)
{
var task = Task.Factory.StartNew(() =>
{
TestResult testResult = test.PerformTest();
return testResult;
});
tasks.Add(task);
}
try
{
Task.WaitAll(tasks.ToArray());
}
catch (Exception e)
{
throw e;
}
}
}
Inside test2 I need to know when test1 is finshed and pause execution until it is. Would a ManualResetEvent work here? If so, where do I define it? Would I then pass a reference to it to all other classes that need to know when Test1 is done?
If not, is there a better way to do this?
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
Something like this should work:
public class Test1 : ITest
{
private readonly ManualResetEventSlim _mre;
public Test1(ManualResetEventSlim mre)
{
_mre = mre;
}
public TestResult PerformTest()
{
try
{
Thread.Sleep(60 * 10 * 1000);
return TestResult.Failed;
}
finally
{
_mre.Set();
}
}
}
public class Test2 : ITest
{
private readonly ManualResetEventSlim _mre;
public Test2(ManualResetEventSlim mre)
{
_mre = mre;
}
public TestResult PerformTest()
{
Thread.Sleep(60 * 2 * 1000);
_mre.Wait();
return TestResult.Passed;
}
}
static class Program
{
static void Main()
{
var mre = new ManualResetEventSlim();
var tests = new List<ITest>
{
new Test1(mre),
new Test2(mre),
};
var tasks = new List<Task>();
foreach (ITest test in tests)
{
Task task = Task.Run(() => test.PerformTest());
tasks.Add(task);
}
Task.WaitAll(tasks.ToArray());
}
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
If you know the thread instance, then it's easy: Thread.Join Method (System.Threading)[^] will do it for you.
If you don't - and you probably don't in your example - then have you considered a lock statement?
lock Statement (C# Reference) | Microsoft Docs[^] If you lock a variable at the start of Thread1, and try to grab the lock at the end of Thread2, it will have to halt until the lock is available - which means that Thread1 is finished.
Sent from my Amstrad PC 1640
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
OK, I see.
Did you see Richard's response? I'm curious what your thoughts are on his reply.
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
I hadn't seen it, but now I have ... I've not used ManualResetEventSlim so I'll have to read up on it at some point - the MSDN description isn't exactly helpful, so I can't comment on his solution. Richard knows his stuff though, so it'll probably work!
Sent from my Amstrad PC 1640
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
|
Hello
i créate 10 threads to do a task.
i create a Semapohre to limit access at compteur by 3 threads
how to lock data (compteur)
namespace threads
{
class Program
{
public static int compteur;
public static object ob = new object();
public static SemaphoreSlim sem = new SemaphoreSlim(3);
public static void Thread_test() {
sem.Wait();
for (int i = 0; i < 1000000; i++)
{
compteur++;
}
sem.Release();
System.Console.WriteLine(compteur.ToString());
}
static void Main(string[] args)
{
Thread[] th = new Thread[10];
for (int i = 0; i < th.Length; i++)
{
th[i] = new Thread(Thread_test)
{
Name = "thread_" + i
};
th[i].Start();
}
System.Console.ReadLine();
}
}
}
Result:
thread_2 = 1264634
thread_0 = 1440996
thread_1 = 1645012
thread_3 = 2344585
thread_5 = 2694858
thread_4 = 2809001
thread_6 = 3628891
thread_8 = 3651918
thread_7 = 3991965
thread_9 = 4527904
and i want this without lock function just with Semaphore:
thread_3 = 1893992
thread_1 = 2735536
thread_4 = 4274643
thread_2 = 4349873
thread_0 = 6248051
thread_5 = 7430503
thread_6 = 8107979
thread_7 = 8937401
thread_9 = 9815069
thread_8 = 10000000
Thank you so much
|
|
|
|
|
Don't cross post: you already have this in QA, so posting it in multiple places just duplicates work and that annoys people. Pick one place, and stick to it.
Sent from my Amstrad PC 1640
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Based on the time-stamps, the copy in QA is the repost.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
You have up to three threads modifying the same shared variable at the same time, without any coordination. This will not end well.
For example:
Thread 0: Read compteur == 0
Thread 1: Read compteur == 0
Thread 0: Update compteur = compteur + 1 == 1
Thread 2: Read compteur == 1
Thread 2: Update compteur = compteur + 1 == 2
Thread 1: Update compteur = compteur + 1 == 1
Three threads have incremented the same variable, but the value has only increased by 1 .
Things get even more complicated when you start considering memory models, CPU cache, speculative execution, ...
To safely increment the value within your thread, you need to use Interlocked.Increment[^]:
public static void Thread_test()
{
sem.Wait();
int result = 0;
for (int i = 0; i < 1000000; i++)
{
result = Interlocked.Increment(compteur);
}
sem.Release();
System.Console.WriteLine(result);
}
To do anything more complicated, you will need to use locks or other coordination primitives to prevent the shared state from being modified by multiple threads at the same time.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
thank you for your answer,
I understand the principle for the increment.
if i understand Semaphore can just manage the number of threds to acces the data, but dont manage the lock of the data to use it.
it is necessary to use lock or other to lock the data? or i(t possible to do that with semaphore
Here i use an compteur for example.
i see a lot of youtube'movie but i'don't find a complete toturiel with lock mutex moniter somaphore threads.
thank you
|
|
|
|
|
The point is that your semaphore allows three threads to run the protected code block at the same time. All three threads are trying to modify the same shared state at the same time.
The ++ operator is not atomic - it reads the value, increments it, and writes it back, which takes three operations. With multiple threads, those operations can be interleaved, resulting in incorrect behaviour.
The Interlocked.Increment operator is atomic. No other thread can sneak code into the middle of the operation.
(Interlocked operations using a 64-bit type on a 32-bit OS will only be atomic with respect to each other. Accessing the value without using the Interlocked class will still have problems.)
For anything more complicated that a single numeric variable, you need to use some kind of coordination primitive to protect the shared state. What you use will depend on how the state will be accessed. For example, if most accesses are reading the state, and only a few are modifying the state, a ReaderWriterLockSlim[^] would probably be a good choice.
Ultimately, the best option is to avoid shared state wherever possible.
Threading in C# - Part 2 - Basic Synchronization[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
i am trying to make the simplest c# application - connect to the LG TV and send a command like KEY_UP.
the tv is using WEBOS 3.0 and it is possible to connect and control it using android remote when they are connected to the same network via wifi.
this is an example i found on the net:
LG tv app to display the list of messages - Stack Overflow[^]
i tried posting the json using postman to the TV's address and it replies with "hello world".
i have no idea how to pair with it in order to be able to make the command or any other command work.
Have anyone done this and can point me to an example? i couldn't find one that can help with this and LG doesn't seem to have client SDK for .net.
|
|
|
|
|
Is it worth having a look at the SDK itself?
Every day, thousands of innocent plants are killed by vegetarians.
Help end the violence EAT BACON
|
|
|
|
|
|
What UI are you writing for?
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
MyCroft is right - we need to know which UI you are working with, because there are different ListView controls for WinForms, WPF, websites, etc. and any solution will depend on exactly which one you are trying to use.
Sent from my Amstrad PC 1640
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
I'm looking for an example of highlighting cell the listView of winform or an example of an employee timestamp based on the listView cell color, you can see the attached image file above.
|
|
|
|
|
I have a problem with making a simple SelectedIndex to work with the SelectionChanged.
The same piece of code worked perfectly well with the button click as the event handler but it doesn't work with the selection changed...
All I want is to have a ComboBox with a default value selected,
TextBox displaying this value below, and changing every time I change the selection.
<ComboBox x:Name="BetonCombo" Margin="5" Grid.Column="0" Padding="2" HorizontalAlignment="Center" Width="150" SelectedValuePath="Content" SelectionChanged="BetonCombo_SelectionChanged">
<ComboBoxItem>C16/20 (B20)</ComboBoxItem>
<ComboBoxItem>C20/25 (B25)</ComboBoxItem>
<ComboBoxItem>C25/30 (B30)</ComboBoxItem>
<ComboBoxItem IsSelected="True">C30/37 (B37)</ComboBoxItem>
<ComboBoxItem>C35/45 (B45)</ComboBoxItem>
</ComboBox>
private void BetonCombo_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
int mBetonIndex = BetonCombo.SelectedIndex;
double[] Fck = new double[5];
Fck[0] = 20;
Fck[1] = 25;
Fck[2] = 30;
Fck[3] = 37;
Fck[4] = 45;
double mFck = Fck[mBetonIndex];
fck.Text = mFck.ToString() + " MPa";
}
I am getting this exception
System.NullReferenceException: 'Object reference not set to an instance of an object.' with the last line of code highlighted as the problem (fck.Text = mFck.ToString() + " MPa";).
I don't understand how I could get a null reference if the exact same code worked perfectly fine in the button click event handler? Do I need to set a default value of the ComboBox differently than with a IsSelected=true?
|
|
|
|
|
You get a null reference, probably because C# is case sensitive.
So fck is not the same as Fck and the chances are that when you SelectionChanged event happens, the rest of your code hasn't done anything useful with fck yet. But we can't see the code, so we can't help.
So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger.
Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.
Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
Sent from my Amstrad PC 1640
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|