473,786 Members | 2,426 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

System.MethodAc cessException in multi threaded program

Hello all,

Any help on this issue appreciated because I am getting a bit stuck! I am
using VS.NET 2003.

I have wrapped a multithreaded win32 dll using managed C++ and added a C#
interface, but am having trouble directing the events generated by the win32
dll to events declared in the managed C++ layer, which are in turn directed
to the GUI.

The win32 dll is setup with classes that start worker threads which call
virtual methods that map their events to the controlling program. The
controlling program derives from the class and implements the event methods.
Here is a stripped down base class:

// Compile as win32 dll project (native.dll)
class Cnative
{
public:
// ...
void start(); // Sets worker thread going and returns
void stop(); // Stops worker thread and cleans up
protected:
virtual void event(int iProgress); // Generated by worker thread
};

In the managed C++ wrapper class I have mapped the events by defining a
managed class, containing an unmanaged class derived from Cnative as shown
below. The unmanaged class derives from Cnative, implements the event
method, and attempts to invoke a delegate in the wrapper class.

// Compile into managed C++ dll (layer.dll)
namespace layer {
public __gc class MyEventInfo
{
public:
MyEventInfo(Int 32 iProgress) : iProg(iProgress ) { }
Int32 iProg;
};

public __delegate void MyEventHandler( MyEventInfo *info);

public __gc class Wrapper : public System::Windows ::Forms::Contro l
{
Wrapper() { m_pDerived = new Derived(this); }
~Wrapper() { delete m_pDerived; }

void Start() { m_pDerived->start(); }
void Stop() { m_pDerived->stop(); }

__event MyEventHandler *OnEvent;

private:
__nogc class Derived : public Cnative
{
public:
Derived(Wrapper *p) : m_pWrapper(p) { }
virtual void event(int i)
{
Wrapper *p = m_pWrapper; // Helps with auto-complete
MyEventHandler *pDelegate = new MyEventHandler( p,
&Wrapper::raise _OnEvent);
Object *aArgs[] = new Object*[1];
aArgs[0] = new MyEventInfo(i);
p->BeginInvoke(pD elegate, aArgs);
}
private:
gcroot<Wrapper* > m_pWrapper;
};

Derived *m_pDerived;
};
} // end namespace layer

There are various bits of code I have ommitted here that are called to
initialise the CRT library etc. I have a GUI written in C# that makes use of
the layer.Wrapper class. I need events from the worker threads created by
the classes in native.dll to be displayed using the C# GUI.

What I have written compiles, but when the Wrapper::Start method is called
for a wrapper object i get the following error:

An unhandled exception of type 'System.MethodA ccessException' occurred in
Unknown Module.
Additional information: layer.Wrapper.r aise_OnError(Sy stem.String)

I have stepped through my code with the debugger and everything works as
expected except when trying to access the Wrapper class methods from the
Wrapper::Derive d class.

1. Can anybody shed any light as to why this error is occuring?
2. Am I attempting to map the events generated by the worker threads created
by Cnative::run() (in native.dll) correctly? Is there a better method?
3. Does anybody know of any sample code on the net that maps events from
threads in a native win32 dll to objects in a derived class and from there
into .NET? I found pages dealing with callbacks, but all the examples were
in the context of the one thread (the GUI thread).

Thanks in advance,

Dave

Nov 16 '05 #1
0 1352

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1324
by: Sudipto Biswas | last post by:
Hi, I have written a multi-threaded (about 8 threads) networking application with a text-mode interface. It writes to a trace-file in the background. Additionally I have also given some debug prints to STDOUT from different threads. The program runs fine on Python-2.2.2/RH 8.0. But when I move to Python-2.2.2/RH-9.0 the program behaves oddly. It stops suddenly and *does not* give the continueous stream of expected output in STDOUT and...
0
4933
by: Ganbold | last post by:
Hi, I'm new to multi-threaded programming and reading the book "Programming with POSIX Threads" and trying to understand concepts and coding. What I'm trying to do is to rewrite mysql client application (which calculates ISP dial-up customers' billing) into multi-threaded version.
2
3658
by: Michal Przytulski | last post by:
Hi, I'm looking information abouth runing PHP CLI application in multi-threaded - on http://www.php.net/manual/pl/ref.pcntl.php i found information abouth process control - but is PHP support multi-threaded ?? Can anyone help ??
1
1985
by: Mullin Yu | last post by:
As subject. I have lots of VB6 dll and ocx files and they are single-threaded as VB6 doesn't support multi-threaded indeed. Can I wrap anything at .NET so that multi-threaded feature can be provided? Thanks! Mullin
0
1249
by: hjackman | last post by:
I am developing a COM library that exposes a single class Number and uses the ComClass attribute. I have also added another project that I use to debug the COM class. I recently added several events to the COM class which are raised asynchronously. The following code will execute with no problems; however, as soon as I add an event handler to the debug program, I get the error: An unhandled exception of type...
9
6090
by: Stu | last post by:
Hi, I have a web service that does database access and calculations However it keeps falling over becuase its not multi threaded and each call to it is not in its own space!! I dont want to use com+ so is there any way of making it Multi threaded TIA
6
2377
by: Gina_Marano | last post by:
Hey All, I am using multiple child threads per main thread to download files. It sometimes appears as if the same file is being downloaded twice. I am using "lock". Am I using it correctly? Any blantant threading errors here? Any opinions would be greatly appreciated. namespace MainThreadManagement
3
2328
by: C++Liliput | last post by:
It seems that the keyword "volatile" is used to make sure that threads reading (or writing to) the same data should see a consistent picture of the variable i.e. updates made to the common data should immediately get reflected to other threads that are using it. However I have never faced this issue in y multi-threaded code even though the common variables are not declared volatile. So is this just a matter of coincidence that I have had...
14
3414
by: =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
As far as I know, the C Standard has no mention of multi-threaded programming; it has no mention of how to achieve multi-threaded programming, nor does it mention whether the language or its libraries are suitable for multi-threaded programming. For people who are fond of portable C programming, what's the best way to go about multi-threaded programming? I've been reading up on POSIX threads a little, they seem pretty ubiquitous....
0
9647
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9961
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8989
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7512
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6745
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5397
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3669
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.