|
Basically , this is ok for one or two api calls. But I am here handling with more than 300 calls. So instead of putting the if conditions everywhere , just using attribute over each api signature would be lot easier. That is why i am posting this question to get it in a easier way.
|
|
|
|
|
I guess you're looking for a generic method dispatch mechanism that validates the platform before executing calls. How about something like this.
enum Platform
{
XP,
NT,
Vista
}
class PlatformNameAttribute : Attribute
{
public PlatformNameAttribute(Platform platForm)
{
this.Platform = platForm;
}
public Platform Platform { get; set; }
}
class Program
{
[PlatformName(Platform.NT)]
static void SomeAPICall()
{
Console.WriteLine("NT");
}
[PlatformName(Platform.XP)]
static void SomeOtherAPICall(int x)
{
Console.WriteLine("XP");
}
static Platform GetCurrentPlatform() { return Platform.NT; }
static void ExecuteAPICall(Delegate d, params object[] parameters)
{
var attributes = d.Method.GetCustomAttributes(typeof(PlatformNameAttribute), false);
if (attributes != null && attributes.Count() > 0)
{
var platformAttribute = (PlatformNameAttribute)attributes[0];
if (platformAttribute.Platform != GetCurrentPlatform())
{
throw new InvalidOperationException("Platform Type mismatch");
}
d.DynamicInvoke(parameters);
}
}
static void Main(string[] args)
{
ExecuteAPICall(new Action(SomeAPICall));
ExecuteAPICall(new Action<int>(SomeOtherAPICall), 2);
}
You could do something more typesafe than DynamicInvoke, with Expression and lambdas, but that would require .NET 3.5, and I'm not sure you are running it.
|
|
|
|
|
That's a cool solution and I already tried it. But the problem is when API calls uses parameters with ref / out keyword or it has a large number of parameters say 7 or something or have parameters with marshalas attributes you can't use either Action or Func. I am not sure in that case if this method dispatcher will work or not.
For your information I am using .Net 3.5.
Any kind of suggestion is highly required.
Thanks.
|
|
|
|
|
I have the class Class1:
public enum Protocol
{
Digital,
Analog
}
public enum Model
{
Digital1,
Digital2,
Analog1,
Analog2
}
public static Protocol m_selectedProtocol;
public static Model m_ModelSelected;
public static Protocol SelectedProtocol
{
get { return Class1.m_selectedProtocol; }
set { Class1.m_selectedProtocol = value; }
}
public static Model ModelSelected
{
get { return Class1.m_m_ModelSelected ; }
set { Class1.m_ModelSelected= value; }
}
Then here is the code being executed:
if (rdAnalog.Checked == true)
{
Class1.SelectedProtocol = Protocol.Analog;
if (Analog1.Checked == true)
{
Class1.ModelSelected = Model.Analog1;
}
else if (Analog2.Checked == true)
{
Class1.ModelSelected = Model.Analog2;
}
else
{
return;
}
else
{
return;
}
i have this error when this line is executed :Class1.SelectedProtocol = Protocol.Analog;
How can it be solved ? thanks
|
|
|
|
|
The code you have presented (except of a coouple of typos) works -
public class Class1
{
public enum Protocol
{
Digital,
Analog
}
public enum Model
{
Digital1,
Digital2,
Analog1,
Analog2
}
public static Protocol m_selectedProtocol;
public static Model m_ModelSelected;
public static Protocol SelectedProtocol
{
get { return Class1.m_selectedProtocol; }
set { Class1.m_selectedProtocol = value; }
}
public static Model ModelSelected
{
get { return Class1.m_ModelSelected ; }
set { Class1.m_ModelSelected= value; }
}
}
private void button1_Click(object sender, EventArgs e)
{
Class1.SelectedProtocol = Class1.Protocol.Analog;
}
so there is something in the code you have not posted causing the problem - are you able to post some complete code that shows the problem?
___________________________________________
.\\axxx
(That's an 'M')
|
|
|
|
|
Here is the code :
if (rdAnalog.Checked == true)
{
Class1.SelectedProtocol = Protocol.Analog;
private void buttonStartTest_Click(object sender, EventArgs e)
{
try
{
if (Analog1.Checked == true)
{
Class1.ModelSelected = Model.Analog1;
}
else if (Analog2.Checked == true)
{
Class1.ModelSelected = Model.Analog2;
}
else
{
return;
}
else
{
return;
}
}
catch (Exception c)
{
MessageBox.Show(c.Message);
Environment.Exit(0);
}
}
|
|
|
|
|
I'd need to see the code for Class1 - sorry if I wasn't clear. As someone says below, it could be a static constructor causing the issue - but without seeing the code for the class it's hard to guess.
___________________________________________
.\\axxx
(That's an 'M')
|
|
|
|
|
It seems that you have missed a '}' in your code. Changing your code like below is working
if (rdAnalog.Checked == true)
{
Class1.SelectedProtocol = Protocol.Analog;
if (Analog1.Checked == true)
{
Class1.ModelSelected = Model.Analog1;
}
else if (Analog2.Checked == true)
{
Class1.ModelSelected = Model.Analog2;
}
else
{
return;
}
}
else
{
return;
}
|
|
|
|
|
One of the types involved probably has a static constructor, and I'm guessing that static constructor is throwing an exception.
Put breakpoints in the static constructors of all classes involved, and step through them in the debugger.
|
|
|
|
|
hello
I installed my keypair into CSP using sn.exe
sn.exe -i mykeypair.snk MYKEYPAIR_RSA_SNKContainer
Now how do I access CSP? I can't find anything under Control Panel>Administrative tools.
Thanks
dev
|
|
|
|
|
Hi,
while working with an application , i came across method signature that used 'this' keyword. I googled alot but coudn't find any article that provided help, would anybody over here please tell me or either forwrd me an article that explains this method description. thankou
public static bool IsPart(this Product product)
{
try
{
return product.isPart.Value == "true" ? true : false;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return false;
}
}
though, i have pasted the whole method but i am confused with function signature . Thanx for providing help
|
|
|
|
|
Hi,
yes that syntax is a bit strange, google for "C# extension methods", a recent C# feature.
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
|
you're welcome.
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
Good people,
How do I use the preprocessor "#if" to detect whether my app is running in debug or release mode?
(I know, I'm embarrassed to ask. Here, I've built an entire app and don't know how to detect debug mode. Doh!)
Thanks,
Blitz
|
|
|
|
|
Hi,
by default a debug build has DEBUG and TRACE predefined, whereas a release build has not.
However you can change these settings (and predefine other symbols) in the build tab of the project properties.
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
#if DEBUG
code goes here
#endif
easy as that!
___________________________________________
.\\axxx
(That's an 'M')
|
|
|
|
|
Thank you all so much. I appreciate it.
|
|
|
|
|
Hi, In my application i need to open 2 forms. After the first form is loaded, iti loads a second form with the following..
System.Threading.Thread MainFormThread = new System.Threading.Thread(new System.Threading.ThreadStart(OpenMainForm));
MainFormThread.Start();
System.Threading.Thread.Sleep(1500);
Where OpenMainForm() just has Application.Run(new MainForm());
The MainForm has a webbrowser control, and it will not compile due to the error:
ActiveX control '8856f961-340a-11d0-a96b-00c04fd705a2' cannot be instantiated because the current thread is not in a single-threaded apartment.
Does anyone know how i can solve this issue?
Thanks for reading.
Andy
|
|
|
|
|
The Web Browser control is designed to run in a Single threaded fashion.
You can not use multi threading when using Web Broswer Control.
BTW, why do you need a seperate thread for this? You can use a timer instead.
Please remember to rate helpful or unhelpful answers, it lets us and people reading the forums know if our answers are any good.
|
|
|
|
|
Hi,
your situation is not clear to me.
if you have two Application.Run() statements, I think you are on the wrong track.
If the first form is just a splash screen, I know you are doing it wrong.
A splash screen does not need an Application.Run, just create one and Show() it.
Then do Application.Run(new Form2());
And make sure your static main() is decorated with a [STAThread]
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
My apologies for not explaining my situation clearly.
It is a small client application, the first form is a login form which shows the user a dropdown list which they select an account name from.
Then I wish to pass this account name to the main form.
It has been awhile since i have done any work with multi-threading but you cant just use form.Show() can you?
I just tried and the form shows and then closes straight away.
As far as I was aware if you are using multiple forms, you needed to use Application.Run()?
Please correct me if I am wrong.
|
|
|
|
|
Abydosgater wrote: if you are using multiple forms, you needed to use Application.Run()?
wrong.
A Windows app that has modeless forms needs a single Application.Run, so you get one message pump.
Abydosgater wrote: use form.Show()
right.
form.Show() will show the form until the user closes it, the app closes it or the app exits.
it is wise to keep the form's reference alive though.
Remember: A single message pump can serve multiple modeless forms (i.e. those shown by calling Show).
Abydosgater wrote: multi-threading
haven't seen your reasons for even considering multi-threading.(there is no need AFAIK).
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
Ah I see.
Well when i use form.Show() it shows the form and closes it straight away. It doesnt give the user a chance to select an account.
I have tried form.ShowDialog() and that seems to keep the form active.
LoginForm loginForm = new LoginForm();<br />
loginForm.ShowDialog();
I then added OpenMainForm(string accountname) to the Program class:
<br />
public static void OpenMainForm(string accountname)<br />
{ Application.Run(new MainForm(accountname)); }
So when the program launches it shows the login form, and then when the user selects an account it calls the static OpenMainForm in the Program Class which uses Application.Run(), but thats gives errors.
Starting a second message loop on a single thread is not a valid operation. Use Form.ShowDialog instead.
Considering the invalid methods I have been using, would it be more logical for me to launch the main form with Application.Run(), have it hidden and show the login form as a dialog from within the main form?
Andy
|
|
|
|
|
Hi,
I suggest you study this example:
public class Form2 : Form {
static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
new Form2("a").Show();
new Form2("b").Show();
Application.Run(new Form2("c"));
}
public Form2(string title) {
Text=title;
}
}
Find out the difference between those forms.
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|