Visual C++ :: How To Fork Or Create Thread

Dec 23, 2012

How can I implement the paradigm demonstrated by the code below to run on WinXP?

I want to fork a process or create a thread that shares global variables with the parent process/thread.

The child process/thread monitors the progress of the parent process/thread.

I cannot find documentation for a fork function per se, a Unix term. It might be called something different for WinXP.

I would be happy to use threads instead. But I'm rusty even with Unix application threads; and I know nothing of WinXP application threads.

So any turnkey implementation that demonstrates the simplest use of process or thread functions for my purpose demonstrated below.

In either case, do "forked" processes and threads share global address space in WinXP, as they do in Unix?

I would prefer to avoid the overhead of IPC mechanisms. The "overhead" includes my own relearning curve.

The GUI screwed up my indentation. I would try to correct it. But the proper indentation appears when I edit the posting. I suppose I need to insert real tabs. Haven't figured out how (yet).

Not even real tabs work; and I cannot get the "paste as text" button to behave as I expect. What is the trick for posting indented text in this GUI?

#include "stdafx.h"
#include <stdlib.h>
#include <Windows.h>
long curCount;
int isRunning;
int _tmain(int argc, char* argv[]) {
curCount = 0;
isRunning = 1;

[code]....

View 4 Replies


ADVERTISEMENT

C++ :: Create A Thread To A Class Function

Jan 25, 2014

I'm trying to create a thread to a class function, but it doesnt work for me...

The function : void Server::RecMsg(SOCKET &socket)

the call : std::thread GetMessages(&Server::RecMsg, ClientSocket);

I get the error : "Error1error C2064: term does not evaluate to a function taking 1 arguments"

View 1 Replies View Related

C/C++ :: Open Directory And For Each File In It Create A Thread

Dec 20, 2014

I'm trying to write a program that opens a directory and for each file in directory,it creates a thread. Inside of the thread,it reads the numbers in file.(one number in one line)And then calculates the average of these numbers. I write something like this;

void *calculate(FILE *piece){
int a,k=0,s=0,ort;
while(!feof(ayri)) {
a=fgetc(ayri);
s=s+a;

[Code] ....

When I run the programme,i get lots of errors like 'segmentation error' and some stuff about pthread_create. And I don't think the 'calculate' function is right.I don't know the right way to read numbers line by line.

View 3 Replies View Related

C# :: One Thread Trying To Pass Data To Another Thread Using Serial Port

Jul 30, 2014

I have a class which I wrote and one of its object is "SerialPort" .NET class. In my MainWindow I created instance of my class called "SerialPortComm", then I send through some functions of mine, commands to the Serial Port, and I receive answers through "DataReceived" event.

But when I trying to use Dispatcher.BeginInvoke to write my data I have received (successfully), nothing shows on the RichTextBox which I'm trying to write to.

What can caused that, and How I can make it works?

SerialPortComm.cs

public partial class SerialPortComm : UserControl {
public SerialPort mySerialPort = new SerialPort();
public void Open_Port(string comNumber, int baudRate) {
mySerialPort.PortName = comNumber;
mySerialPort.BaudRate = baudRate;

[Code] ....

View 7 Replies View Related

Visual C++ :: Add To Listbox Thread

Feb 15, 2015

Startthread get called to launch to update list box.

a)is it the only way to pass string to the PostMessage?. my concern is that allocate using new in each iteration each time slows it down a bit.

b)does OnAddToListBox look memory leak free?.

c)What is the best way handle m_pThread at the end?. Any way to improve it?.

Code:
UINT CMyDlg::Dump(LPVOID lparam) {
HWND *pHndl = static_cast<HWND *>(lparam);
char buffer[200];
for (int i = 0; i < 100; i++) {
sprintf_s(buf,"user %d, data %s",i, "Connected");

[Code] ....

View 8 Replies View Related

Visual C++ :: How To Check Thread With MFC

Feb 12, 2013

I use MFC Find console window and send key to console like this.

Code:
HWND w;
w = FindWindow(NULL, L"c:userspkrudocumentsvisual studio 2010ProjectsCleanVirusDebugCleanVirus.exe");

[Code]....

After program process success in console it not close console windows but it show like this in Output

The thread 'Win32 Thread' (0x1300) has exited with code 0 (0x0).

I think if I can check when VC will show this line, then I can use FreeConsole(); for close console windows.

View 3 Replies View Related

Visual C++ :: Exit Application Using Thread In MFC SDI

Feb 4, 2014

I have a SDI application. I created a method OnClose to handle ON_WM_CLOSE of CMainFrm. This onclose() function calls a method in cmyview.cpp. Here, I created a thread that calls global function and from this function it calls another function in cmyview.cpp. At certain condition my application should close at here, I used postmessgae(WM_CLOSE (or) WM_DESTROY). I am having an error as object reference not set on postmessage(WM_CLOSE) it is going to afxwin2.inl page where exception occurs.

Below is code snippetHere, either j or k will only be true depneds on user input)

MainFrm.cpp:
void CMainFrame::OnClose()
{
CMyView* pview = (CMyView*)((CFrameWnd*)AfxGetMainWnd())->GetActiveView();
pview->method1();
}

[Code]...

View 3 Replies View Related

Visual C++ :: Using One Thread At A Time With CSemaphore

Oct 6, 2014

I need using CSemaphore class in MFC C++ application. I have an edit1 box with multil ine and each line has a string. I'm trying to loop through edit1 box and for each string to start a thread that is using the string for specific function. I'm trying to limit the run of only one thread at the same time with semaphores but all treads start at the same time.

So when i click button1 i loop through edit1 box and start threads:

Code:
void CMFCApplication1Dlg::onButton1Click() {
int i, nLineCount = edit1.GetLineCount();
CString strText, strLine, mesaj;
for (i = 0; i < nLineCount; i++) {

[Code] ....

While looping through edit1 box multi line and starting the threads:

Code:
UINT CMFCApplication1Dlg::StartThread(LPVOID param) {
WaitForSingleObject(semafor, INFINITE); // wait for semafor to signal
THREADSTRUCT* ts = (THREADSTRUCT*)param;
// here i'm doing some operations with the string from edit1 box
ReleaseSemaphore(semafor, 1, NULL); //release the semaphore for next thread to begin
}

Instead of running only one thread at a time all threads start. What am i doing wrong ?

View 14 Replies View Related

Visual C++ :: Unable To Display Dialog In UI Thread

Mar 11, 2015

I am having a strange problem trying to display a dialog from a UI thread. The dialog simply fails to display. I have a function DisplayFlashBox(), which creates the UI thread:

CUIThread* CIMUIHelper:: DisplayFlashBox(const CString &sMessage, const int nInstrumentUID) {
CUIThread *pThread = new CUIThread();
pThread->SetString(sMessage);
pThread->SetInstrumentUID(nInstrumentUID);
pThread->CreateThread();

[Code] .....

The dialog doesn't display. When I tried debugging, I found the OnInitDialog() method of CIMFlashBox class doesn't actually return. Very strange. I tried calling the DoModal() method instead of Create, but doesn't display the dialog either.

View 5 Replies View Related

Visual C++ :: Calling Self-contained Posix Thread?

Mar 2, 2015

pThread turns out to be NULL here. Wondering what the correct way is...

Code:
class CPF_Thread {
public:
unsigned int threadID;
Coordinater coordinater;
virtual UINT proc() {

[Code] .....

View 2 Replies View Related

Visual C++ :: Unable To Invoke JScript Functions From Another Thread

May 6, 2013

I have main thread that creates an WebBrowser2 COM object. and i want to invoke JScript functions on it from another thread. i try to use GIT but still doesn't work for me.. there is a problem with marshal WebBrowser2 for JScript?

View 4 Replies View Related

Visual C++ :: Local Variable Be Passed As Parameter Into A New Thread?

Apr 1, 2013

Can local variable be passed as the parameter for a new created thread procedure? Here is the example code:

Code:
void CDLG::some_function()
{
CString strFileName="abc.doc";
//local variable, can it be valid for being passed into the following new thread???
//Can strFileName still be accessed from within the stack of thread procedure?
::AfxBeginThread(ProcessContentThread,(LPVOID)&strFileName);
}

[Code]...

There is another method using variable on the heap,

Code:

void CDLG::some_function()
{
CString strFileName="abc.doc";
CString* pstrFN=new CString(strFileName);
::AfxBeginThread(ProcessContentThread,(LPVOID)pstrFN);
}

[Code]...

I test these code, both methods work as expected, but I doubt whether the first method is a good way. OR if only the second method is the correct way to pass a parameter to a thread.

View 12 Replies View Related

C/C++ :: Difference Between Fork And Vfork?

Apr 15, 2014

when to use fork and vfork? I am using execl in my child. which one I need to use?

View 1 Replies View Related

Visual C++ :: Terminate A Thread When User Push Stop Button

Nov 16, 2012

I have a thread with a while(1) loop in it. When the user push the stop button I would like that thread to end.

I thought about creating a bool and checking its value periodically in the thread and when I push the stop button I change the value of the bool for that the thread breaks out of the loop and finishes.

View 5 Replies View Related

C/C++ :: Fork Function Involving For Loop?

Sep 26, 2014

what I'm trying to do is create 3 child processes stemming from one parent process. I have figured that out but the problem is my program exits when it get to the third child leaving it orphaned(I think).

This is my code for the first generation of processes:

int i = 0;
pid_t child;
about("Dad");
printf("Now .. Forking !!

[Code] .....

I have created a menu GUI that I feel messes with the loop commands, i.e. "Continue". The menu pops up repeatedly after each child process is created. I commented out the block of code for menu which fixed it. I was wondering if there is a way so that I can have the menu not messing with the processing?

View 1 Replies View Related

Visual C++ :: Obtaining Actual Effective Execution Time Per Thread With Hyperthreading

Jun 13, 2013

I'm looking for a function like GetThreadTimes, but one that gives correct results in a hyperthreading CPU.

For instance, if you start 2 threads on a CPU with 1 physical core and 2 logical cores, GetThreadTimes will say that both these threads use 100% CPU.

However, in reality they only use 50% each. Is there a function that returns correct results, or is there another workaround?

View 8 Replies View Related

Visual C++ :: How To Reduce High CPU Usage Caused By Receiver Thread Of CAN Messages

Feb 13, 2013

I am writing a program which handles the CAN messages Received from BUS.My code is taking almost 60% of CPU usage.

View 14 Replies View Related

Visual C++ :: How To Raise Priority Of MIDI Input Callbacks - Thread Pools

Sep 23, 2014

I'm the author of a realtime MIDI software called ChordEase which makes use of the MIDI aspects of the multimedia API, specifically MIDI input callbacks. In XP and before, these callbacks originated in the kernel and therefore had realtime priority by definition, but from Vista on, they originate in thread pool threads, and have a priority of zero. This is a problem because at priority zero they can be blocked by the GUI thread, causing serious latency, and I have proved that such blocking occurs.

I have experimented with raising the callback thread priority, using either of the following methods: 1) calling SetThreadPriority within the MIDI input callback function, and then setting a flag so that it isn't done repeatedly, or 2) creating a DLL that catches thread creation via DLL_ATTACH_THREAD in DllMain, and calling SetThreadPriority there. The first method is slightly wasteful since the flag has to be tested for every MIDI input event, but it also has the advantage of only affecting the MIDI input threads, whereas the second method affects all threads in the pool regardless of what they're used for. Neither method appears to cause any harmful effects but they make me nervous*. Other possible methods would include 3) using the thread pool API to raise the priority of the pool (assuming I could gain access to the pool handle somehow), or 4) permanently lowering the priority of the GUI thread, which I'm very reluctant to do because of the risk of unintended consequences.

I'm assuming the MIDI input callbacks are using threads in the default thread pool though I haven't actually proved this. Assuming that's so, are these threads private to my application, or is my application sharing them with other applications? Is there a safer way to achieve the result of increasing the priority of MIDI input callbacks? It's incredibly frustrating that MS would change the behavior of MIDI input callbacks so drastically without even telling anyone, but that's how it goes!

[URL] ....

*See for example theses warnings about changing thread pool priorities : [URL] ....

View 13 Replies View Related

C :: Making Fork Call And Have The Instances Run Every Five Minutes

Dec 18, 2014

Understanding Fork() calls. I have to write a C program in UNIX machine, and make two Fork Calls Inside it and have these two instances run every five minutes all round the clock. How to achieve this.

View 1 Replies View Related

C :: Code That Will Store PID Numbers Of Child After A Fork?

Aug 8, 2013

I am currently trying to write a code that will store PID numbers of a child after a fork, I have to be able to enter an amount that will be created, so far I have managed to be able to get them to print (which puts me on the right path as far as I am concerned) but I am having issues. Using the following code:

Code:

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

[Code].....

View 3 Replies View Related

Visual C++ :: Error C3867 Function Call Missing Argument List For Calling Thread Function

Mar 19, 2013

I searched the web for error: C3867... and the discussions where murky or obscure.

My code excerpt is:

#pragma once
#include <windows.h>
#include <stdlib.h>
#include <process.h>
void PutUpfrmIO(void *);
namespace WordParsor {

[Code] .....

I get the generic message:

error C3867: 'WordParsor::Form1::PutUpfrmIO': function call missing argument list; use '&WordParsor::Form1::PutUpfrmIO' to create a pointer to memberc:userskingc++wordparsorwordparsorForm1.h... and the suggestion fix generate another error.

One person suggested the gcroot<> object wrapper... but I do not know how to modify/declair the function or its argument type.

View 2 Replies View Related

Visual C++ :: Create MFC OCX Using DLL

Aug 28, 2014

I am trying to create a OCX from a C++ dll., Here's the scenario I have a C++ dll and this dll needs to be called in VB.net program my boss want's me to create an OCX out of this.

C++ dll and use it in a VB.net class library, apparently I have created an OCX but it requires a form but the VB.net program is a class library.

View 5 Replies View Related

Visual C++ :: Create Web-browser With ATL?

Aug 11, 2013

I'm trying to create a simple webbrowser at my app.

code sample:
....
ATL::CAxWindow m_wndContainer;
ATL::CComPtr<IWebBrowser2> m_spWebBrowser;
HWND hWnd = m_wndContainer.Create(NULL,
CRect(0,0,200,100),
(LPCTSTR)0,
WS_CAPTION|WS_THICKFRAME|WS_OVERLAPPED|WS_MINIMIZEBOX|WS_MAXIMIZEBOX);
CoCreateInstance(CLSID_WebBrowser,NULL,CLSCTX_INPROC,IID_IWebBrowser2,(void**) &m_spWebBrowser);
...

everything is work fine except the fact the it seems that it create an IE version 7.0 and my machine have IE 8 installed.

it not suppose to create the IE according to my installed version?how do i suppose to do it?

View 1 Replies View Related

Visual C++ :: How To Create A Dialog As DLL

Feb 13, 2014

How can I create a dialog as a DLL ? I have a SDI MFC app (CView is based on CFormView) and from a button I want to load a modal dialog, but that dialog must be developed as DLL ... I didn't find something that fit my needs on internet ... P.S. I have only VC6 !

View 14 Replies View Related

Visual C++ :: Create A New Toolbar With Icons?

May 23, 2013

how to create a new toolbar with my icons in vc++?

View 3 Replies View Related

Visual C++ :: How To Create Image Buffer In MFC

Jan 27, 2013

how to create, initialize, and maintain a memory device context that works as a local buffer for images? The idea is to maintain some large images in local DCs and bitmaps, and only bitblt them to screen in OnDraw().

What I did so far was was to define CDC and CBitmap objects as members of my View class, and then tried to initialize them with the sequence that begins at "// Initialize buffer". I placed that sequence in either OnInitialUpdate, or PreCreateWindow, or OnPrepareDC, or the view constructor, to no avail. The code always crashes in OnDraw, and I've noticed that the m_hDC member of myDevice is zero at that point.

Obviously, the initialization is wrong and MFC does (too many) things in the background which I'm not aware of.... My question was where can I read about that?

Code:
class CMyView : public CScrollView {
// ...
CDC myDevice;
CBitmap bmp;
CBitmap *oldbmp;

[Code] .....

View 5 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved