2

So I'm a predominantly Linux programmer who's recently got a job working with Windows. I've developed some numerical code, but now need to put a simple GUI on.

At the moment I have a button with an onClick event with calls a function called start which kicks off the numerical stuff. But this freezes the GUI so I want to put the call to start into a worker thread.

I've declared start like this: void __cdecl start(void* args). I added the __cdec1 as I was previously getting the error: error C2664: '_beginthread' : cannot convert parameter 1 from 'void (__clrcall *)(void *)' to 'void (__cdecl *)(void *)'.

Now when the button is clicked the following code runs:

output->Text = "Starting";
_beginthread(start, 0, NULL);
output->Text = "Done";

This gives me the confusing error cannot convert parameter 1 from 'void (__cdecl *)(void *)' to 'void (__cdecl *)(void *)'.

If anyone can see where I'm going wrong I'd be eternally grateful.

James
  • 3,957
  • 4
  • 37
  • 82
  • 1
    We should see how and where `start` is declared but let me guess: it's a managed method (then you're programming in C++/CLI not C++). I'd simply avoid to use `_beginthread`. In managed world (unless you really need to) you'd better use managed functions/classes (in this case `ThreadPool` or `Thread`). – Adriano Repetti Jun 25 '14 at 08:35
  • `start` is declared as `void __cdecl start(void* args)` in a separate file called `Processing.cpp`, it is not in `Form1.h`. I think I have some reading to do regarding C++/CLI vs C++, and what managed vs unmanaged means. – James Jun 25 '14 at 08:40
  • You could drop the `__cdecl` and use `_beginthreadex` API instead, as this accepts pointers to functions in managed code. See [MSDN](http://msdn.microsoft.com/en-us/library/kdzttdcb.aspx). – Evil Dog Pie Jun 25 '14 at 08:41
  • How you include that declaration? Did you put it in a `#pragma unmanaged` section? Anyway...I'd suggest to completely separate them (managed/unmanaged) with well defined "connection points" or move to managed only (see MSDN for `System.Threading.Thread.Start()` method or `System.Threading.ThreadPool` methods). In my experience mixing is always painful (especially when beginning with C++/CLI). – Adriano Repetti Jun 25 '14 at 08:45
  • 1
    You might also want to look at [this answer](http://stackoverflow.com/a/14972532/3581917). – Evil Dog Pie Jun 25 '14 at 08:47

0 Answers0