|
Best solution is to use Thread Call back
like
threadcallback1 = new System.Threading.TimerCallback(fun1);
threadcallback2 = new System.Threading.TimerCallback(fun2);
So both are running at the differnt intereval you can make both of them run parallely.
hope this helps u
Regards
Sri
|
|
|
|
|
Trying to PInvoke CreateProcess, I looked up MSDN, it says library is "Kernel32.lib" - which is what I specified in my DllImport attribute. I keep getting the following exception:
"Unable To Load Dll"
I'm suspecting:
a. Shouldn't I be loading dll? Not lib? Anyway, another reference suggested to load "coredll.dll" (which is nowhere to be found on my XP)
b. return Type for CreateProcess is of type BOOL. In my C# code, I convert that to "int". Is this correct? Perhaps PInvoke failed because function signature doesn't match? I had same problem when I switched to BOOL. Also, does it matter if I use "class" (instead of struct) for "ProcessInfo"?
Here's what I did, can you see anything wrong here?
public Int32 GetPInvokeError()
{
return GetLastError();
}
//************ PInvoke ************ //
[StructLayout(LayoutKind.Sequential)]
public struct ProcessInfo
{
public IntPtr hProcess;
public IntPtr hThread;
public Int32 dwProcessId;
public Int32 dwThreadId;
}
// Launch a process:
[DllImport("Kernel32.lib", SetLastError=true)]
private extern static int CreateProcess(
String imageName,
String cmdLine,
IntPtr lpProcessAttributes,
IntPtr lpThreadAttributes,
Int32 boolInheritHandles,
Int32 dwCreationFlags,
IntPtr lpEnvironment,
IntPtr lpszCurrentDir,
byte [] si,
ProcessInfo pi );
// GetLastError:
[DllImport("Kernel32.lib")]
private extern static Int32 GetLastError();
norm
|
|
|
|
|
I've also tried:
[StructLayout(LayoutKind.Sequential)]
public class ProcessInfo
{
public int hProcess;
public int hThread;
public int dwProcessId;
public int dwThreadId;
}
Which didn't help.
norm
|
|
|
|
|
norm wrote:
a. Shouldn't I be loading dll?
Yes.
BTW, there is already an implementation of CreateProcess in the .NET BCL - check out System.Diagnostics.Process .
|
|
|
|
|
Just got it, I ran into some docs, it should be "kernel32.dll", but that's not what it says on MSDN:
Windows NT/2000/XP: Included in Windows NT 3.1 and later.
Windows 95/98/Me: Included in Windows 95 and later.
Header: Declared in Winbase.h; include Windows.h.
Library: Use Kernel32.lib.
Unicode: Implemented as Unicode and ANSI versions on Windows NT/2000/XP. Also supported by Microsoft Layer for Unicode.
So, next time when I see "XXXX.lib", I should just replace "lib" with "dll" then?
norm
|
|
|
|
|
I got it working now and is tackling on killing the process. I found this in C++/MFC - to translate to PInvoke, I need to identify corresponding dll for CWnd.
void CSomeDlg::killProcess()
{
HANDLE ps = OpenProcess( SYNCHRONIZE|PROCESS_TERMINATE, FALSE, processPid);
EnumWindows(EnumWindowsProc, processPid);
CloseHandle(ps) ;
}
BOOL CALLBACK CSomeDlg::EnumWindowsProc(HWND hwnd, LPARAM lParam)
{
DWORD wndPid;
CString Title;
GetWindowThreadProcessId(hwnd, &wndPid);
CWnd::FromHandle( hwnd )->GetWindowText(Title); //ATTENTION: CWnd is MFC. I guess I need to find out in which dll CWnd resides...
if ( wndPid == (DWORD)lParam && Title.GetLength() != 0)
{
// Please kindly close this process
::PostMessage(hwnd, WM_CLOSE, 0, 0);
return false;
}
else
{
// Keep enumerating
return true;
}
}
Is there any easier alternative?
norm
|
|
|
|
|
Hmm... I'd advise that you use the BCL implementation.
Process process=Process.Start(filename);
ProcessStartInfo startinfo=new ProcessStartInfo();
Process process = Process.Start(startinfo);
process.CloseMainWindow();
|
|
|
|
|
What's BCL? Anyway, I got it. I just used C#/.NET framework, instead of WIN32 API OpenProcess then TerminateProcess. Here's the code:
try
{
System.Diagnostics.Process process = System.Diagnostics.Process.GetProcessById(nProcessID);
process.CloseMainWindow();
process.Close();
}
catch(Exception err)
{
throw new ApplicationException("Exception from CWin32ProcessUtil.KillProcess. ProcessID: " + nProcessID + " Info: " + err.Message);
}
I launched the process using WIN32 API coz I wanted to avoid "TypeInitializationException" when I tried to launch a GUI app from a Windows Service. Thanks though.
norm
|
|
|
|
|
norm wrote:
What's BCL?
Stands for Base Class Library.
-Nick Parker
|
|
|
|
|
I was hoping I could get away with "TypeInitializationException" via Win32 CreateProcess while launching a GUI app from OnStart (Windows Service)
I guess I was wrong.
norm
|
|
|
|
|
Hi ,
i have a winforms control that when i connect it to a datasource generates lots of different objects (all inheriting from component)
what i want is to add those components to the designer surface so that the controls can be accessed on the forms designer/propbrowser
so HOW do i make my control add the generated components to the designer host?
----
scenario:
i add the control to a form
i set a datasource to it
after the source is set , i want lots of dynamicly generated components to appear in the 'component pane' under the form.
//Roger
|
|
|
|
|
At run-time it is easy enough.
But I believe you want to do this at design-time?
If so, it sounds like you want to build a "plug-in." Kind of like the Add New Form wizards.
When you create a plug-in, it exposes the DTE object from which you can work with objects such as Solution, Project, etc.
Hopfully this provides a solution for your needs
|
|
|
|
|
i solved it..
i added a designer to my control and made the designer listen to events from the control
so when the control creates the dynamic components , the designer intercepts them and add them to the designer host..
//Roger
|
|
|
|
|
Is it true that all serviced component must be registered in COM+ catalog? According to the book I'm reading right now, the answer is no. Any class derived from ServicedComponent is a COM+ component - the resulting class library dll is the COM+ application. The dll doesn't has to be installed in GAC or get registered in COM+ Catalog.
To use .NET assembly from COM client, you need to create CCW and use regasm.exe to have it registered in the registry. But, this allows your assembly to be consumed by COM/unmanaged client. You still can't use COM+ attributes or configure the compoent using "COM+ Administration Tools (MMC snap-in) - therefore, a COM+ component is MORE than just a .NET assembly wrapped in CCW.
A COM+ component must:
1. derive from ServicedComponent
2. register in COM+ Catalog, using regsvcs.exe (Which install dll into GAC - meaning you need to first assign a key file)
Your component is then a COM+ serviced component, meaning:
1. you can apply COM+ attributes (like object pooling or JIT..etc)
2. Configure the component using COM+ Administration Tool (MMC snap in).
Is my understanding correct?
norm
|
|
|
|
|
I have an application that consists of a lot of concurrently executing controllers that performs many different time consuming or timed operations. To get an idea what this means practically, you can imagine 10000 threads (each controller), where each thread performs the timed operations by the use of calls/callbacks.
I want to move to some other paradigm. I want a way of simulating several threads (microthreads) and a way of writing C# code that not gets messy by a lot of callback handling.
What I have at the moment is this, for each operation the controller should perform:
void DoOperationX()
{
// Do some stuff or initiate something
}
void OperationXDoneCallBack()
{
// Stuff is done, move to next operation:
DoOperationX+1();
}
What I would like to write code-wise, is the following for each controller. And I would like to simulate concurrent execution of all controllers, only using one real thread (or a few real threads):
void PerformOperations()
{
DoOperation1(); // Perform operation 1, and wait until its done
DoOperation2(); // Perform operation 2, and wait until its done
DoOperation3(); // Perform operation 3, and wait until its done
}
1. How do I simulate microthreads? I can live with the need of calling a special method once in a while to check if the microthread should be paused and execution should be given to the next microthread. Is there a way to copy the current stack and restore it later?
2. Callback hiding. If I can get the microthreading to work, I can hide the callbacks from the API like this:
void DoOperationX()
{
DoTheOperation();
while (!callBackXDone)
{
CheckMicroThread(); // Switch microthread if needed.
}
}
void OperationXCallBack()
{
callBackXDone = true;
}
Am I following the wrong path here? Is there already a simple solution to this?
Regards, Björn
|
|
|
|
|
System.Threading.ThreadPool.QueueUserWorkItem()
-Blake
|
|
|
|
|
Fibers;
<ahref="">http://msdn.microsoft.com/msdnmag/issues/03/09/CoroutinesinNET/default.aspx
Thank You
Bo Hunter
|
|
|
|
|
Really interesting, thanks!
/Björn
|
|
|
|
|
Hi, I'm trying to expose COM+ component via remoting, it works just fine - it seems. But I'm confused by how COM+/Remoting works together.
Remoting - lifetime depends on whether the object is published as SAO(singleton/singlecall), CAO and lifetime services.
In COM+, lifetime of server objects (COM+ application/component) depends on object pooling and whether JIT is enabled. My understanding is that if a COM+ app is published via remoting, than activation option must be "server". I am a little confused at how/what determines an object's life time in this situation. For instance, how can you have a SAO/Singlecall and have object pooling at the same time?
Following are nine possible scenario:
COM+ JIT enabled/Pooling disabled + CAO
COM+ JIT enabled/Pooling enabled + CAO
COM+ JIT disabled/Pooling disabled + CAO
COM+ JIT enabled/Pooling disabled + SAO/Singlecall
COM+ JIT enabled/Pooling enabled + SAO/Singlecall
COM+ JIT disabled/Pooling disabled + SAO/Singlecall
COM+ JIT enabled/Pooling disabled + SAO/Singleton
COM+ JIT enabled/Pooling enabled + SAO/Singleton
COM+ JIT disabled/Pooling disabled + SAO/Singleton
Any idea? Thanks!
norm
|
|
|
|
|
Does anybody know what I'm doing wrong here? I'm trying to encrypt my class before I serialize it. It seems to encrypt and serialize my class just fine (or at least it managles it into something unreadable!), but when I attempt to deserialize I keep getting either SerializationException "End of Stream encountered before parsing was completed", but it seems to work fine without the encryption. I think it has something to do with the way it handles blocks of data, but I'm not sure how to make it work right.
Thanks
<br />
public void SaveClass()<br />
{<br />
IFormatter BinFmt = new BinaryFormatter();<br />
UnicodeEncoding UE = new UnicodeEncoding();<br />
byte[] key = UE.GetBytes(strUser);<br />
<br />
Stream sout = File.Open("Class.bin",FileMode.Create);<br />
RijndaelManaged myRijndael = new RijndaelManaged();<br />
CryptoStream CptOut = new CryptoStream(sout,myRijndael.CreateEncryptor(key,key),CryptoStreamMode.Write);<br />
BinFmt.Serialize(CptOut,this);<br />
sout.Close();<br />
}<br />
<br />
public static MyClass LoadClass()<br />
{<br />
MyClass newObj = new MyClass();<br />
IFormatter BinFmt = new BinaryFormatter();<br />
UnicodeEncoding UE = new UnicodeEncoding();<br />
string currUser = newObj.strUser;<br />
byte[] key = UE.GetBytes(currUser);<br />
<br />
Stream sin = File.Open("Class.bin",FileMode.Open);<br />
RijndaelManaged myRijndael = new RijndaelManaged();<br />
CryptoStream Cptin = new CryptoStream(sin,myRijndael.CreateDecryptor(key,key),CryptoStreamMode.Read);<br />
newObj = BinFmt.Deserialize(Cptin) as MyClass;<br />
sin.Close();<br />
return newObj;<br />
}<br />
|
|
|
|
|
I want to get into threading...
Does anybody know any good C# threading books?
/\ |_ E X E GG
|
|
|
|
|
Tom Archer's Inside C# 2nd Edition contains a whole chapter on Threading. It's very in depth and a great resource for anything C# related.
When I can talk about 64 bit processors and attract girls with my computer not my car, I'll come out of the closet. Until that time...I'm like "What's the ENTER key?"
-Hockey on being a geek
|
|
|
|
|
eggie5 wrote:
Does anybody know any good C# threading books?
Just another book to look into: C# Threading Handbook[^]
|
|
|
|
|
My application communicates with a remote object, and the interface for that remote object allows client's to register events with the server. One of the registered events is designed to open a new form on the client, and that new form (subclass of Form, not surprisingly) is instantiated and Show()n by the event code itself. The window that's created appears, but it doesn't display properly. Do such windows (created by Events triggered by a remote object) need to be manually placed in the Application thread so they properly update and respond to user key and mouse click events?
Thanks in advance for any help.
Jerry
jerry@cs.stanford.edu
|
|
|
|
|
I am supposed to develop a three tier client server based model application using replicated databases. this is for a class project. I know how to develop an application that will access a database and perform updates and all that using ADO.
My plan is to structure the tiers in the following manner
1->C# windows application that uses data from the data source
2->Middle ware in charge of connecting to the database and performing transactions based on client requests (replication should also take place here)
3->Actual database. Could just be a simple access database.
My confusion is how to separate the windows application from the middleware. I am used to just using datasets et al (ADO) in my application when connecting to the the database. Also how do I go about replicating the database via the middle ware. I am assuming the middleware would handle all client requests. Aslo, how do I connect to any available database through the middleware since that would be the whole point of replicating the database.
I would also like to be able to "plug-in" a database to the middleware and have it brought up to date to the state of the other replicated databases.
One idea I was thinking about was having a separate dataset for each client.
Thanks in advance
|
|
|
|