|
Do you want to use a .NET Dll in a .NET Application, but using DllImport ??
|
|
|
|
|
As far as I know DllImport works with unmanaged assemblies, while your code produces a managed one.
|
|
|
|
|
First of all, I think the previous poster is correct - DllImport is definitely meant for unmanaged libraries, like the Windows APIs, and not managed (.net) ones like yours. I can't say for sure that it cannot be used with managed assemblies, but I doubt it - there are better ways to do this you see.
That having been said, your DLL does not have any entry points! Entry points are necessarily static, and your library only contains a class with an instance method.
If you wanted to "convert" the program to a DLL in order to make it available to non-.NET clients there is presumably a way, but I don't know what it is (Google is your friend though).
If you just want to use the assembly in other .net applications, the simplest is of course to just add a reference to it. But if you want to load assemblies dynamically (this can be useful for things like a plug-in architecture) you should have a look at the Assembly class and in particular the Load() method. There's lots of goodies here and you can easily do things like discover all classes that implement certain interfaces or derive from particular classes.
|
|
|
|
|
How can i stop an while loop by clicking a button?
eg:
A while loop is executing, i want to stop the while loop from outside like a button click
Thankyou
YPKI
|
|
|
|
|
you must put that loop in a new thread. then use a flag, like _isButtonClicked.
If yes, then break.
|
|
|
|
|
how to put a while loop in a thread?
|
|
|
|
|
The easiest way to put it in a background thread is to make sure you have a method signature that take one parameter of type object and then use ThreadPool.QueueUserWorkItem(new WaitCallback(method)) - "method" simply being the name of the method you wish to call.
bool stop;
void method(object dummy)
{
while (!stop)
{
...
}
}
However, be aware that the code in method should not use any references to any controls. By default, doing so will result in an exception. You can make this go away by setting Control.CheckForIllegalCrossThreadCalls to false , but it's not a good practice to do so, I think because the controls aren't designed to be thread-safe. Depending on what you're doing, you may or may not decide that you don't care and just use the controls from the background thread anyway. I've done it many times (in apps for my own use only, where I don't really care if this one day leads to some odd behavior of a button because two threads tried to update it's text property at once, resulting in something weird and maybe beautiful) and I haven't had any problems with it, but there is undoubtedly a good reason why Microsoft decided to disallow this by default.
|
|
|
|
|
I don't know do you want to break immediately, but something like this may help
bool stop = false;
while(!stop)
{
..dosomething
}
or
while(true)
{
if(stop)
break;
}
and on your button_click
{
stop = true;
}
hope it helps
|
|
|
|
|
A bit vague. Is your while loop executing in a background thread? If it's executing on your GUI thread and your not calling Application.DoEvents it won't ever break!
Regards,
Rob Philpott.
|
|
|
|
|
while loop is runnung normally, not in thread.
how can i stop using Application.DoEvents
|
|
|
|
|
Create a boolean member:
private bool _running = true;
In the button click event set it to false:
_running = false;
Make your while loop look like this
while (_running)
{
Application.DoEvents();
}
Regards,
Rob Philpott.
|
|
|
|
|
Problem with this is that there won't be any click events, because it won't check this.
You can better use a thread and in this thread you put your code.
Something like this (out of my head):
private bool _running = true;
public static void main(args[])
{
Thread t = new Thread(threadFunction);
t.Start();
}
public void threadFunction()
{
while(_running)
{
}
}
public void Button1_Clicked(object sender, EventArgs e)
{
_running = false;
}
Don't know if the 'thread' part is correct, the rest is ok
|
|
|
|
|
Actually, its perfectly valid. The Application.DoEvents keeps the message pump pumping and is the way mutitasking used to work in the Windows 3.x world.
Admittedly, I prefer your way of doing it but the gentleman suggested he didn't want to use background threads.
Regards,
Rob Philpott.
|
|
|
|
|
I've never used the Application.DoEvents() function. Didn't even know the existance of it.
With that in my mind, I totaly agree with you.
|
|
|
|
|
I tried using this code for reading excel data using c# :
bt the following part of the code is giving errors and is not being executed....
plz help !!!!
using System;
using System.Collections.Generic;
using System.Text;
using Excel;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string Path = @"c:\gg.xls";
Excel.ApplicationClass app = new ApplicationClass();
Excel.Workbook workBook =
app.Workbooks.Open(Path,0,true,5,"","",true,Excel.XlPlatform.xlWindows,"\t",false,false,0,true,1,0);
Excel.Worksheet workSheet = (Excel.Worksheet)workBook.ActiveSheet;
int index = 0;
object rowIndex = 2;
object colIndex1 = 1;
object colIndex2 = 2;
try
{
while (((Excel.Range)workSheet.Cells[rowIndex, colIndex1]).Value2 != null)
{
rowIndex = 2 + index;
string firstName = ((Excel.Range)workSheet.Cells[rowIndex, colIndex1]).Value2.ToString();
string lastName = ((Excel.Range)workSheet.Cells[rowIndex, colIndex2]).Value2.ToString();
Console.WriteLine("Name : {0},{1} ", firstName, lastName);
index++;
}
}
catch (Exception ex)
{
app.Quit();
Console.WriteLine(ex.Message);
}
}
}
}
the error is at "Excel.Workbook workBook = app.Workbooks.Open" :
Error 1
No overload for method 'Open' takes '15' arguments C:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\ConsoleApplication1_trytry\ConsoleApplication1_trytry\Program.cs 16 39 ConsoleApplication1_trytry
|
|
|
|
|
Well, that's saying you've got the wrong number of arguments in the open method. Check the documentation on 'Open'.
Regards,
Rob Philpott.
|
|
|
|
|
Hi All,
Please let me know if we can cast a known type to a generic type...
private static T InitData<T>(T dummyColl, int value)
{
List<SampleData> SampleColl = new List<SampleData> {
new SampleData {
Age = 23,
Name="abc"
}
};
dummyColl = (T)SampleColl;
}
Is there any other way around to get this??
"Don't worry if it doesn't work right. If everything did, you'd be out of a job." (Mosher's Law of Software Engineering)
|
|
|
|
|
It would probably throw an exception if you don't use a Collection type, but try (T)Convert.ChangeType(SampleColl, typeof(T)) . Bear in mind that this will convert the list itself. If you want to convert the objects inside the list, then use SampleColl.Cast<t>()</t>
|
|
|
|
|
It is not very clear from your sample what you are trying to do.
If you are doing this to cast a collection (hence the dummyColl name?), you would probably want to do something like:
private static List<t> CastToDerivedType<t>(List<basetype> list) where T : BaseType
{
List<t> result = new List<t>();
foreach (BaseType item in list)
result.Add(item as T);
return result;
}
</t></t></basetype></t></t>
Try explaining what is your goal first.
|
|
|
|
|
|
Just a note - you should note that this line:
dummyColl = (T)SampleColl;
doesn't do anything to the actual dummyColl being passed to the method.
So this kind of casting at the end of your method doesn't do anything.
|
|
|
|
|
I agree...Actually while working for the solution I've passed the dummyColl argument to the method that's because I thought I can get the T type by using dummyColl.GetType().(But I forgot that, to get Type there is actually 'typeof' exists in .NET )
Also using
veki-peki wrote: dummyColl = (T)SampleColl;
gives me a compiler error!!! Cannot convert List type to T
"Don't worry if it doesn't work right. If everything did, you'd be out of a job." (Mosher's Law of Software Engineering)
|
|
|
|
|
As it stands it won't work as T could be anything and so casting from SampleColl to T may very well not work.
Try this, use 'as':
dummyColl = SampleColl as T;
you'll need (I think) to add a constraint to your generic method
private static T InitData<T>(T dummyColl, int value) where T: class
.. because T might be a value type and you need to constrain it to reference types to use as.
If you're not familiar with constraining generic types and methods have a read up on that - that should help you quite a bit but it's hard to offer advice without knowing what derives from what.
Regards,
Rob Philpott.
|
|
|
|
|
Rob Philpott wrote: T could be anything and so casting from SampleColl to T may very well not work
I agree with you...but at the calling site itself I have confined T for only two data types (as per the requirement). So in my code there is never a chance that T is a value type
Rob Philpott wrote: private static T InitData<t>(T dummyColl, int value) where T: class
I've used this but frankly I don't understand its usage...even with that 'where' clause I'm able to do something like this without any compiler error.
int i=2;
(T)Convert.ChangeType(i, dummyColl.GetType())
So whether I use this constraint or not it throws the error at runtime only.Can you please let me know why is it happening?
"Don't worry if it doesn't work right. If everything did, you'd be out of a job." (Mosher's Law of Software Engineering)
|
|
|
|
|
Spunky Coder wrote: but at the calling site itself I have confined T for only two data types
That's unimportant. The compiler needs to compile your function, and in order to do that it needs to know a bit about T. If it knows nothing about T, it has to assume it could be anything and therefore won't accept your cast. By constraining the function, your telling the compiler something about T, enough for it to be satisfied that the cast will always work. Don't forget your assembly might be used in other projects written by other people- how can you guarantee that they'll call it the correct way?
Spunky Coder wrote: So whether I use this constraint or not it throws the error at runtime only.Can you please let me know why is it happening?
What's the error?
Regards,
Rob Philpott.
|
|
|
|