|
choppol wrote: I'm new to C# and was wondering what is the best way to execute a .ps1 file ..and that's where your specs should pause, and where you Google on how to execute a PowerShell script. Any tutorial and example should be considered.
choppol wrote: to perform a task in Exchange Prolly means executing it on that server, with some Exchange-specific libraries.
choppol wrote: I have a button that when click should execute the ps1 file and perform a test like get mailbox. Means you put the code from the tutorial under the click-event of that button. What kind of button is this btw, is it in WinForms, WPF, something webby?
choppol wrote: As part of the task, I am using a browse to all users to input and then display the output in a textbox beneath the button. How do you browse to all users and what does it have to do with the task?
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
To set things up, what I was trying to do was make a TextBox flash when a user misses a required input. I got it to work but I don't understand the error I was getting at first.
Code that had an exception:
private void FlashRequiredTextBox(TextBox target)
{
ColorAnimation flash = new ColorAnimation(Colors.White, Colors.Red, new Duration(TimeSpan.FromMilliseconds(75D))) { AutoReverse = true, RepeatBehavior = new RepeatBehavior(5D) };
target.Background.BeginAnimation(SolidColorBrush.ColorProperty, flash);
}
The exception that was thrown:
Cannot animate the 'Color' property on 'System.Windows.Media.SolidColorBrush' because the object is sealed or frozen.
I understand why the exception is thrown but what I don't get is why this code fixes it.
private void FlashRequiredTextBox(TextBox target)
{
ColorAnimation flash = new ColorAnimation(Colors.White, Colors.Red, new Duration(TimeSpan.FromMilliseconds(75D))) { AutoReverse = true, RepeatBehavior = new RepeatBehavior(5D) };
target.Background = new SolidColorBrush(Colors.White);
target.Background.BeginAnimation(SolidColorBrush.ColorProperty, flash);
}
Can somebody with a better understanding of WPF control rendering explain this?
if (Object.DividedByZero == true) { Universe.Implode(); }
Meus ratio ex fortis machina. Simplicitatis de formae ac munus. -Foothill, 2016
|
|
|
|
|
The default style for the TextBox control sets the background brush to:
<Setter Property="Panel.Background">
<Setter.Value>
<DynamicResource ResourceKey="{x:Static SystemColors.WindowBrushKey}" />
</Setter.Value>
</Setter>
(I couldn't find an official source, but you can view the default styles using Style Snooper[^].)
That configures the background to use the SystemColors.WindowBrush , and to dynamically update whenever the Windows colour settings are changed.
The WindowBrush property calls the MakeBrush method[^], which explicitly freezes the brush:
private static SolidColorBrush MakeBrush(CacheSlot slot)
{
SolidColorBrush brush;
lock (_brushCacheValid)
{
if (!_brushCacheValid[(int)slot])
{
brush = new SolidColorBrush(GetSystemColor(slot));
brush.Freeze();
_brushCache[(int)slot] = brush;
_brushCacheValid[(int)slot] = true;
}
else
{
brush = _brushCache[(int)slot];
}
}
return brush;
}
When you try to animate that brush from the code-behind, you then get an error because it has been frozen: Freezable Objects Overview[^]
With your second code block, you've set the background to a new SolidColorBrush , which hasn't been frozen. The animation then works as expected. You could also use:
private void FlashRequiredTextBox(TextBox target)
{
ColorAnimation flash = ...;
if (target.Background.IsFrozen) target.Background = target.Background.Clone();
target.Background.BeginAnimation(SolidColorBrush.ColorProperty, flash);
}
As far as I can tell, when you configure the animation in the XAML markup, the system ensures that the background brush is not frozen. When you configure the animation from the code-behind, that check is left up to you.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I love this place, almost every day I can find something new (to me) and interesting. I have a great dislike for grossly animated views however this may be useful to get in a users face
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Ah, it makes sense that default brushes are created and then frozen right away. If they weren't, I suppose it might cause all sorts of nasty side effects. Thanks for the detailed explanation.
if (Object.DividedByZero == true) { Universe.Implode(); }
Meus ratio ex fortis machina. Simplicitatis de formae ac munus. -Foothill, 2016
|
|
|
|
|
Could some one suggest some way to improve the performance of getting log description. We use FormatDescription() to read the description of the logs and the performance is pathetic.
To put the matter in perspective, if I comment the FormatDescription(), my application of parsing 28000+ logs completes in about 2 minutes, and on adding that line the application takes well over 1.3 hrs!
Regards,
Pitrak
|
|
|
|
|
FormatDescription is well known for being slow - the general recommendation is to drop down to the Win32 APIs instead.
This space for rent
|
|
|
|
|
Hi,
Thanks for the reply; could you please help me with the name of the Win32 API to use.
If you could provide a simple code snippet, that would be really helpful.
I am actually intending to use C# API's to build my application. So, how would I be using the Win32 API's?
Regards,
Pitrak
|
|
|
|
|
Have you found any workarounds for this? I'm actually facing the same issue and don't know what to do.
Regards!.
|
|
|
|
|
Guys,,, please help me to create a TCP connection C# to C++. is it possible to make a code C# SERVER and the Client is C++....please help me guys...thanks
|
|
|
|
|
Jarlo Belledo wrote: is it possible to make a code C# SERVER and the Client is C++
Yes.
Jarlo Belledo wrote: please help me to create a TCP connection
What have you tried?
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
|
|
TCP is TCP. What you need to do is search for an example of creating a C# TCP client/server and take the server part. Then you look for an example of creating a C++ client/server and take the client part. I suspect the question you are really asking is, what would the payload look like when communicating between them.
This space for rent
|
|
|
|
|
I suspect the question is will someone write this for me?
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
I know this isn't a Powershell forum, but the question is about PS in C#..
I have the following Powershell, that clears my languagebar, Setting Norwegian language. Adds Norwegian Apple Keyboard, removes, then Adds back to the end normal Norwegian keyboard:
$li = New-WinUserLanguageList "nb-NO";
$li[0].InputMethodTips.Add("0414:A0000414");
$li[0].InputMethodTips.Remove("0414:00000414");
Set-WinUserLanguageList $li -Force
$li = Get-WinUserLanguageList
$li[0].InputMethodTips.Add("0414:00000414");
Set-WinUserLanguageList $li -Force
This works Peachy when run from the powershell commandline. It updates the Language bar, and
set the Apple keyboard as default.
But when embedding it in C# like this:
string script = "";
script += "$li = New-WinUserLanguageList nb-NO;";
script += "$li[0].InputMethodTips.Add(\"0414:A0000414\");";
script += "$li[0].InputMethodTips.Remove(\"0414:00000414\");";
script += "Set-WinUserLanguageList $li -Force;";
script += "$li[0].InputMethodTips.Add(\"0414:00000414\");";
script += "Set-WinUserLanguageList $li -Force;";
using ( PowerShell ps = PowerShell.Create() ) {
ps.AddScript( script );
var result = ps.Invoke();
foreach ( var item in result ) {
}
}
The language bar does not get updated, although the language IS switched to Norwegian...
Is there any limitations on what kind of ps-scripts you can embed in C#?
(Both the script and the c# code runs as me/current user)...
TIA.
--
Dag.
|
|
|
|
|
Your two scripts aren't identical. The first line doesn't have nb-NO quoted, so try surrounding this with \"nb-NO\" . Also, you are missing $li = Get-WinUserLanguageList from your C#.
This space for rent
|
|
|
|
|
Thanks for commenting...
Thats not it, thou... Just me trying small adjustments.
With or without quotes, and with or without re-getting the newly set list.
Results are the same.
--
Dag.
|
|
|
|
|
Thanks for commenting...
The differences are just me trying small differences. They where the same.
Didn't work in c# when they were identical either...
(But I should of course have posted them equal, to avoid confusion).
--
Dag.
|
|
|
|
|
You might need to set the execution policy:
Set-ExecutionPolicy Unrestricted -Scope Process
This space for rent
|
|
|
|
|
Yeah... It looks like that is where the dog is buried...
Thanks...
|
|
|
|
|
No problem. Glad you got it working (and I like the analogy).
This space for rent
|
|
|
|
|
Its a Norwegian proverb... Don't know where/why... But it is a good one!
Still got some small problems, thou...
I turned on ExecutionPolicy so it looks like this:
$li = New-WinUserLanguageList "nb-NO";
$li[0].InputMethodTips.Add("0414:A0000414");
$li[0].InputMethodTips.Remove("0414:00000414");
Set-WinUserLanguageList $li -Force;
$li = Get-WinUserLanguageList;
$li[0].InputMethodTips.Add("0414:00000414");
Set-WinUserLanguageList $li -Force;
- This creates a new languagelist with Norwegian standard culture,
- Adds the Norwegian Apple input (keyboard).
- Removes the Standard Norwegian keyboard.
- Set the list to be the current one in windows.
- Add back the Norwegian standard keyboard.
- Reapply the list to windows.
The sole purpose of the operation is to set the language bar to "Norwegian, with Apple Keyboard" with Norwegian with std. keyboard as a second option.
And it *ALWAYS* does, when run from Powershell/Command prompt.
When run embedded in c#, it sets Input to Norwegian Standard, and does NOT show Apple keyboard as a first (or second) alternative...
This drives me NUTS!
--
Dag.
|
|
|
|
|
Hi there , I am new to using delegates. How can i display the value 8, after I call t he delegate which calls the method Myfunc which doubles the number ?
using System;
namespace Akadia.BasicDelegate
{
public delegate int SimpleDelegate(int a);
class TestDelegate
{
public int n2;
public static int MyFunc(int b)
{
Console.WriteLine("I was called by delegate ...");
TestDelegate t = new TestDelegate();
t.n2 = b * 2;
return t.n2;
}
public int N2
{
get { return n2; }
}
public static void Main()
{
SimpleDelegate simpleDelegate = new SimpleDelegate(MyFunc);
simpleDelegate(4);
Console.ReadKey();
}
}
}
|
|
|
|
|
You display it exactly the same way as if you had called MyFunc directly i.e.
Console.WriteLine(simpleDelegate(4));
or
int val = simpleDelegate(4);
Console.WriteLine(val);
Cheers,
Mick
------------------------------------------------
It doesn't matter how often or hard you fall on your arse, eventually you'll roll over and land on your feet.
|
|
|
|