|
You cannot; the background-color is owned by the form, owned by the mainthread.
How are you querying this value from the worker-thread? Are you polling it, checking it every so seconds?
Bastard Programmer from Hell
if you can't read my code, try converting it here[^]
|
|
|
|
|
The Control.Invoke method has an object return type and so with an appropriate delegated method any information can be obtained from a Control.
e.g.
private delegate Color ColourDelegate(Control c);
private void GetColourInfo(Control c) {
Color bgcol = (Color)c.Invoke(new ColourDelegate(GetColour), c);
Color bgcol2 = (Color)c.Invoke(new ColourDelegate(delegate { return c.BackColor; }));
}
private Color GetColour(Control ctrl) {
return ctrl.BackColor;
}
Alan.
|
|
|
|
|
MichCl wrote: I have a current form that is either lime or red. ... check the color of the form. How do I do this? That doesn't smell right.
The color of the form should be driven the state of an object, not the other way around. It seems what you want to do is to raise an event when the status of your object changes (presumably from "good" to "bad"). Any number of observers can listen for this change and respond to it an appropriate manner.
/ravi
|
|
|
|
|
Hello,
I have added an Extended attribute (extensionAttribute1) to represent a special app ID (string) in Active Directory.
Now, I have extended the UserPrincipal class so I can see this new attribute but I get exceptions when running the code.
Any help to figure out what is wrong will be much appreciated.
Exceptions:
2012-11-02 03:14:43,659 [10] DEBUG Classes.Utils.MyHelper - DoesUserExist: Exception while trying to query Active Directory. at System.DirectoryServices.AccountManagement.Principal.MakePrincipal(PrincipalContext ctx, Type principalType)
at System.DirectoryServices.AccountManagement.SDSUtils.SearchResultToPrincipal(SearchResult sr, PrincipalContext owningContext, Type principalType)
at System.DirectoryServices.AccountManagement.ADStoreCtx.GetAsPrincipal(Object storeObject, Object discriminant)
at System.DirectoryServices.AccountManagement.ADStoreCtx.FindPrincipalByIdentRefHelper(Type principalType, String urnScheme, String urnValue, DateTime referenceDate, Boolean useSidHistory)
at System.DirectoryServices.AccountManagement.ADStoreCtx.FindPrincipalByIdentRef(Type principalType, String urnScheme, String urnValue, DateTime referenceDate)
at System.DirectoryServices.AccountManagement.Principal.FindByIdentityWithTypeHelper(PrincipalContext context, Type principalType, Nullable`1 identityType, String identityValue, DateTime refDate)
at System.DirectoryServices.AccountManagement.Principal.FindByIdentityWithType(PrincipalContext context, Type principalType, String identityValue)
at
Classes.Utils.UserPrincipalExtension.FindByIdentity(PrincipalContext context, String identityValue) in C:\visual studio 2010\Projects\App\Classes\Utils\UserPrincipalExtension.cs:line 61
at App.Classes.Utils.MyHelper.DoesUserExist(String usr) in C:\visual studio 2010\Projects\App\Classes\Utils\MyHelper.cs:line 69
2012-11-02 03:14:43,659 [10] DEBUG App._Default - UserID_Changed: Exception trying to lookup user in Active Directory. at Classes.Utils.MyHelper.DoesUserExist(String usr) in C:\visual studio 2010\Projects\App\Classes\Utils\MyHelper.cs:line 94
at App._Default.UserID_Changed(Object sender, EventArgs e) in C:\visual studio 2010\Projects\App\Default.aspx.cs:line 105
Extension Class:
[DirectoryObjectClass("person")]
[DirectoryRdnPrefix("CN")]
public class UserPrincipalExtension : UserPrincipal
{
public UserPrincipalExtension(PrincipalContext context, string userName) : base(context) { }
public UserPrincipalExtension(PrincipalContext context,
string samAccountName,
string password,
bool enabled)
: base(context,
samAccountName,
password,
enabled)
{
}
UserPrincipalExSearchFilter searchFilter;
new public UserPrincipalExSearchFilter AdvancedSearchFilter
{
get
{
if (null == searchFilter)
searchFilter = new UserPrincipalExSearchFilter(this);
return searchFilter;
}
}
[DirectoryProperty("extensionAttribute1")]
public string extensionAttribute1
{
get
{
if (ExtensionGet("extensionAttribute1").Length != 1)
return null;
return (string)ExtensionGet("extensionAttribute1")[0];
}
set { this.ExtensionSet("extensionAttribute1", value); }
}
public static new UserPrincipalExtension FindByIdentity(PrincipalContext context,
string identityValue)
{
return (UserPrincipalExtension)FindByIdentityWithType(context,
typeof(UserPrincipalExtension),
identityValue);
}
public static new UserPrincipalExtension FindByIdentity(PrincipalContext context,
IdentityType identityType,
string identityValue)
{
return (UserPrincipalExtension)FindByIdentityWithType(context,
typeof(UserPrincipalExtension),
identityType,
identityValue);
}
}
public class UserPrincipalExSearchFilter : AdvancedFilters
{
public UserPrincipalExSearchFilter(Principal p) : base(p) { }
}
Application Call:
public static bool DoesUserExist(string usr)
{
string methodName = "DoesUserExist: ";
bool result = false;
PrincipalContext context = null;
UserPrincipalExtension user = null;
try
{
context = new PrincipalContext(ContextType.Domain, "MYDOM.LOCAL");
user = UserPrincipalExtension.FindByIdentity(context, usr);
if ((user != null) && (user.extensionAttribute1 != null))
{
log(methodName, "User does exist. UserID=" + usr + "extension=" + user.extensionAttribute1);
result = true;
}
else
{
log(methodName, "User does NOT exist.");
}
}
catch (Exception ex)
{
log(methodName, "Exception while trying to query Active Directory. " + ex.StackTrace);
throw ex;
}
finally
{
user.Dispose();
context.Dispose();
}
return result;
}
|
|
|
|
|
Log the entire exception, not just the stacktrace. There are six lines on which "an" exception might occur. Can you point out to us which line it is?
Bastard Programmer from Hell
if you can't read my code, try converting it here[^]
|
|
|
|
|
It seems that it complains about the Extension class not having the right constructor. But I do have it. Please see the original post and you will see the constructor that the exception is saying I don't have.
Here is the entire trace:
2012-11-02 10:24:41,921 [10] DEBUG App.Classes.Utils.MyHelper - DoesUserExist: Exception: System.NotSupportedException: Extension class must define a constructor that accepts a PrincipalContext argument.
at System.DirectoryServices.AccountManagement.Principal.MakePrincipal(PrincipalContext ctx, Type principalType)
at System.DirectoryServices.AccountManagement.SDSUtils.SearchResultToPrincipal(SearchResult sr, PrincipalContext owningContext, Type principalType)
at System.DirectoryServices.AccountManagement.ADStoreCtx.GetAsPrincipal(Object storeObject, Object discriminant)
at System.DirectoryServices.AccountManagement.ADStoreCtx.FindPrincipalByIdentRefHelper(Type principalType, String urnScheme, String urnValue, DateTime referenceDate, Boolean useSidHistory)
at System.DirectoryServices.AccountManagement.ADStoreCtx.FindPrincipalByIdentRef(Type principalType, String urnScheme, String urnValue, DateTime referenceDate)
at System.DirectoryServices.AccountManagement.Principal.FindByIdentityWithTypeHelper(PrincipalContext context, Type principalType, Nullable`1 identityType, String identityValue, DateTime refDate)
at System.DirectoryServices.AccountManagement.Principal.FindByIdentityWithType(PrincipalContext context, Type principalType, String identityValue)
at App.Classes.Utils.UserPrincipalExtension.FindByIdentity(PrincipalContext context, String identityValue) in C:\visual studio 2010\Projects\App\Classes\Utils\UserPrincipalExtension.cs:line 61
at App.Classes.Utils.MyHelper.DoesUserExist(String usr) in C:\visual studio 2010\Projects\App\Classes\Utils\MyHelper.cs:line 70
Code where exception happened(Line 61 in the Extention class):
public static new UserPrincipalExtension FindByIdentity(PrincipalContext context,
string identityValue)
{
return (UserPrincipalExtension)FindByIdentityWithType(context,
typeof(UserPrincipalExtension),
identityValue);
}
Problem happens in the return line of that code.
|
|
|
|
|
I see one constructor, where your base-class defines two of them; is there an implementation for this[^] signature?
Bastard Programmer from Hell
if you can't read my code, try converting it here[^]
|
|
|
|
|
Eddy,
Thanks a lot for the observation. I am glad I posted here to have other eyes look at it. I cannot believe I had such a small issue. hehe. The problem was that I had an extra parameter sent in to my basic constructor and that was not good. I am not even sure why I was sending in the userName.
Here is what I had:
Faulty Constructor:
public UserPrincipalExtension(PrincipalContext context, string userName) : base(context) { }
Fixed Constructor:
public UserPrincipalExtension(PrincipalContext context) : base(context) { }
Thanks so much for all of your help.
|
|
|
|
|
The law states that "given enough eyeballs, all bugs are shallow"
You'd have found it in the morning
|
|
|
|
|
public interface IMyInterface
{
string myInterfaceProp{ get; }
}
public class MyClass :IMyInterface
{
public string myInterfaceProp
{
get { return "some value"; }
}
}
public class UserClass
{
MyClass objMyClass =new MyClass ();
objMyClass.
}
How to hide the myInterfaceProp in the userclass when the "MyClass " instance is accessed? .
modified 4-Nov-12 22:00pm.
|
|
|
|
|
Your question is not clear, what exactly are you trying to achieve here? If you make it private or protected then it will be inaccessible.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
You cant hide an interface method.
An interface method has no implementation so you need not worry about hiding it.
|
|
|
|
|
The whole point of an interface is that it defines, well, an interface: a public contract for your class, or for third party classes, and the methods and properties that it will expose. It doesn't define any content, it simply states 'if a class says it implements me, you must be able to do these operations on it'.
Therefore it doesn't make sense to put protected methods or properties on an interface. If you don't want a property to be visible to the outside world, then don't add it to the interface!
|
|
|
|
|
Your question is a bit vague, but I'll have a go.
If you mean that you don't want to list myInterfaceProp as a member of the MyClass class (i.e. in Intellisense), implement the interface explicitly (What you're doing in your snippet is called implicit interface implementation).
Try this:
public class MyClass : IMyInterface
{
string IMyInterface.myInterfaceProp
{
get { return "some value"; }
}
}
That way you want have access to this member if you try to access it directly from MyClass , but you'll need a cast to IMyInterface .
2A
|
|
|
|
|
Explicit interface implemntation solved my problem. Thank u so much for the suggestion.
|
|
|
|
|
Why is this code hanging:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private int Test()
{
System.Diagnostics.Debug.WriteLine("IN");
System.Threading.Thread.Sleep(5000);
System.Diagnostics.Debug.WriteLine("OUT");
return 17;
}
private async Task<int> TestAsync()
{
return await Task.Run(() =>
{
return Test();
});
}
private void button1_Click(object sender, EventArgs e)
{
System.Diagnostics.Debug.WriteLine("BEFORE");
int a = TestAsync().Result;
System.Diagnostics.Debug.WriteLine("AFTER");
}
}
}
If I don't return a value from the async function, this code works as I expect. BEFORE, IN, AFTER, <5 sec pause>, OUT.
Kind of trying to make an async function that returns a value without blocking the UI thread.
|
|
|
|
|
You can't.
If you return a value, then the execution of the calling thread cannot continue until the value is available.
It can't just decide to "remember where it was and come back to it later" because it doesn't know what it may be doing when the value does become available.
If you want this kind of handling, then look at setting up a BackgroundWorker, and handling the RunWorkerCompleted event to get the returned value.
If you get an email telling you that you can catch Swine Flu from tinned pork then just delete it. It's Spam.
|
|
|
|
|
Seems like to make it work I need to make the 2nd function async as well and await on the result instead of using the .Result.
|
|
|
|
|
SledgeHammer01 wrote: Kind of trying to make an async function that returns a value without blocking the UI thread.
Try something like below;
private int Test()
{
System.Diagnostics.Debug.WriteLine("IN");
System.Threading.Thread.Sleep(5000);
System.Diagnostics.Debug.WriteLine("OUT");
return 17;
}
private void TestAsync()
{
Task.Factory.StartNew(delegate
{
OnTestFinished(Test());
});
}
private void OnTestFinished(int result)
{
if (InvokeRequired)
{
Invoke(new Action<int>(OnTestFinished), new object[] { result });
return;
}
this.Text = result.ToString();
}
Bastard Programmer from Hell
if you can't read my code, try converting it here[^]
|
|
|
|
|
I'm trying to get a ASP.net application to talk to a WCF Windows Service. Both are on the same internal network and belong to the same domain, but are on different machines.
When connecting I see on the tracing log on my WCF Windows Service:
System.IdentityModel.Tokens.SecurityTokenValidationException: The service does not allow you to log on anonymously.
From what I understand WCF will use windows authentication by default. How do I pass credentials to the WCF service? This is how i'm connecting from the ASP.net client:
public static string Authenticate(string username, string password)
{
DomainControllerClient dc = null;
try
{
dc = new DomainControllerClient();
dc.Open();
bool success = dc.Authenticate(username, password);
if (success)
return "OK";
else
return "FAIL";
}
catch (Exception ex)
{
return ex.ToString();
}
finally
{
if (dc.State == CommunicationState.Opened)
dc.Close();
else if (dc.State == CommunicationState.Faulted)
dc.Abort();
dc = null;
}
}
** BTW I'm using netTcpBinding
|
|
|
|
|
|
I'm having a little trouble. I'm playing with WCF and trying to install it via a service. I'm following these two articles that I found: http://msdn.microsoft.com/en-us/library/ms733069.aspx[^] and http://a1ashiish-csharp.blogspot.com/2012/02/cnet-how-to-host-wcf-web-service-in.html#.UJGjIMXA_08[^]
Now what is happening is I created my WCF DLL Library and I added the WindowsService file and the ProjectInstaller to it. I then created a Windows Setup and added the primary output to the setup project.
When it installs it creates the service and links it to my DLL file instead of the windows service created a EXE and linking it to that. So what I end up with is a service that is unable to start because it can't use the DLL file.
Here is my ProjectInstaller class:
[RunInstaller(true)]
public partial class ProjectInstaller : System.Configuration.Install.Installer
{
public ProjectInstaller()
{
this.AfterInstall += new System.Configuration.Install.InstallEventHandler(ProjectInstaller_AfterInstall);
InitializeComponent();
}
void ProjectInstaller_AfterInstall(object sender, System.Configuration.Install.InstallEventArgs e)
{
using (ServiceController sc = new ServiceController("HostingPanelDC"))
{
try
{
sc.Start();
}
catch (Exception ex)
{
EventLog.WriteEntry("HostingPanel", "Unable to start service. Error: " + ex.Message, EventLogEntryType.Error);
}
}
}
}
I set the service name and all the service info by clicking serviceInstaller1 and serviceProcessInstaller1 and editing the fields in the properties page.
Here is my windows service class:
partial class WindowsService : ServiceBase
{
public ServiceHost serviceHost = null;
public WindowsService()
{
InitializeComponent();
ServiceName = "HostingPanelDC";
}
public static void Main()
{
ServiceBase.Run(new WindowsService());
}
protected override void OnStart(string[] args)
{
if (serviceHost != null)
serviceHost.Close();
serviceHost = new ServiceHost(typeof(DC.DomainController));
serviceHost.Open();
}
protected override void OnStop()
{
if (serviceHost != null)
{
serviceHost.Close();
serviceHost = null;
}
}
}
What exactly am I missing that is causing this not to generate the exe file? Thank in advanced!
|
|
|
|
|
Duh. Forgot to change the output to windows application and set the startup object. Thanks!
|
|
|
|
|
Suggestion,I was only able to install my service using the command line once I ran my installer, if you google the command line function and use it for your service to show up in the actual list of services.
|
|
|
|
|
Thanks for the suggestion! Once I made the changes listed above I was able to install and start the service automatically with the windows installer once I changed my WCF DLL Library that had the windows service and installer to a Windows Application and set the startup object
|
|
|
|
|