Creating a Multithreaded Service

Creating Service Binding,从 CDS 模型到可消费 OData 服务的最后一公里 本文探讨了在SAP RAP开发中容易被忽视但至关重要的Service Binding组件。文章指出,Service Binding作为ABAP仓库对象,负责将Service Definition与通信协议(如OData)绑定,是业务能力对外暴露的关键环节。作者通过采购申请审批应用案例,分析了Service Binding在RAP开发链路中的核心地位,强调其协议决策作用。文章详细介绍了在VS Code中创建Service Binding的流程和注意事项,包括命名规范、绑定类型选择和传输管理。最后指出,Serv 阅读详情

Creating a Multithreaded Service

The following sample code demonstrates how a simple service can spawn worker threads, respond to the SCM, notify the threads to exit, keep the SCM notified of the state and progress of the service, and report to the SCM that the service is stopped. To install the service, build it as a console application and use the SC utility included with the Platform SDK. Use the Service Control utility in the Control Panel to start and stop the service.

#include <windows.h>
#include <tchar.h>
#include <stdio.h>

HANDLE  hStopEvent;
HANDLE hThreads[3] = {NULL,NULL,NULL};
LPTSTR  lpszServiceName;
SERVICE_STATUS_HANDLE   ssh;

DWORD WINAPI ThreadProc(LPVOID lpParameter);
void  WINAPI  Service_Main(DWORD dwArgc, LPTSTR *lpszArgv);
void  WINAPI  Service_Ctrl(DWORD dwCtrlCode);
void  ErrorStopService(LPTSTR lpszAPI);
void  SetTheServiceStatus(DWORD dwCurrentState,DWORD dwWin32ExitCode,
                          DWORD dwCheckPoint,  DWORD dwWaitHint);

// Entry point for service. Calls StartServiceCtrlDispatcher
// and then blocks until the ServiceMain function returns.

void _tmain(int argc, TCHAR *argv[])
{
   SERVICE_TABLE_ENTRY ste[] =
      {{TEXT(""),(LPSERVICE_MAIN_FUNCTION)Service_Main}, {NULL,NULL}};

   OutputDebugString(TEXT("Entered service code/n"));
   if (!StartServiceCtrlDispatcher(ste))
   {
      TCHAR error[256];

      wsprintf(error,
         TEXT("Error code for StartServiceCtrlDispatcher: %u./n"),
         GetLastError());
      OutputDebugString(error);
   }
   else
      OutputDebugString(TEXT("StartServiceCtrlDispatcher OK/n"));
}

// Called by the service control manager after the call to 
// StartServiceCtrlDispatcher.

void WINAPI Service_Main(DWORD dwArgc, LPTSTR *lpszArgv)
{
   DWORD ThreadId;
   DWORD t;
   DWORD dwWaitRes;

   // Obtain the name of the service.
   lpszServiceName = lpszArgv[0];

   // Register the service ctrl handler.
   ssh = RegisterServiceCtrlHandler(lpszServiceName,
           (LPHANDLER_FUNCTION)Service_Ctrl);

   // Create the event to signal the service to stop.
   hStopEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
   if (hStopEvent == NULL)
      ErrorStopService(TEXT("CreateEvent"));

   //
   // Insert one-time work that you want to complete before starting.
   //

   for (t=0;t<3;t++)
   {
      hThreads[t] = CreateThread(NULL,0,ThreadProc,
                                (LPVOID)t,0,&ThreadId);
      if (hThreads[t] == INVALID_HANDLE_VALUE)
         ErrorStopService(TEXT("CreateThread"));
   }

   // The service has started.
   SetTheServiceStatus(SERVICE_RUNNING, 0, 0, 0);
   OutputDebugString(TEXT("SetTheServiceStatus, SERVICE_RUNNING/n"));

   //
   // Main loop for the service.
   // 

   while(WaitForSingleObject(hStopEvent, 1000) != WAIT_OBJECT_0)
   {

/***************************************************************/ 
   // Main loop for service.
/***************************************************************/ 
   }

   // Wait for threads to exit.
   for (t=1;TRUE;t++)
   {
      if ((dwWaitRes = WaitForMultipleObjects(3,hThreads,TRUE,1000))
           == WAIT_OBJECT_0)
         break;
      else if((dwWaitRes == WAIT_FAILED)||(dwWaitRes==WAIT_ABANDONED))
         ErrorStopService(TEXT("WaitForMultipleObjects"));
      else
         SetTheServiceStatus(SERVICE_STOP_PENDING, 0, t, 3000);
   }

   // Close the event handle and the thread handle.
   if (!CloseHandle(hStopEvent))
      ErrorStopService(TEXT("CloseHandle"));
   if (!CloseHandle(hThreads[0]))
      ErrorStopService(TEXT("CloseHandle"));
   if (!CloseHandle(hThreads[1]))
      ErrorStopService(TEXT("CloseHandle"));
   if (!CloseHandle(hThreads[2]))
      ErrorStopService(TEXT("CloseHandle"));

   // Stop the service.
   OutputDebugString(TEXT("SetTheServiceStatus, SERVICE_STOPPED/n"));
   SetTheServiceStatus(SERVICE_STOPPED, NO_ERROR, 0, 0);
}

// Handles control signals from the service control manager.

void WINAPI Service_Ctrl(DWORD dwCtrlCode)
{
   DWORD dwState = SERVICE_RUNNING;

   switch(dwCtrlCode)
   {
      case SERVICE_CONTROL_STOP:
         dwState = SERVICE_STOP_PENDING;
         break;

      case SERVICE_CONTROL_SHUTDOWN:
         dwState = SERVICE_STOP_PENDING;
         break;

      case SERVICE_CONTROL_INTERROGATE:
         break;

      default:
         break;
   }

   // Set the status of the service.
   SetTheServiceStatus(dwState, NO_ERROR, 0, 0);
   OutputDebugString(
       TEXT("SetTheServiceStatus, Service_Ctrl function/n"));

   // Tell service_main thread to stop.
   if ((dwCtrlCode == SERVICE_CONTROL_STOP) ||
       (dwCtrlCode == SERVICE_CONTROL_SHUTDOWN))
   {
      if (!SetEvent(hStopEvent))
         ErrorStopService(TEXT("SetEvent"));
      else
         OutputDebugString(TEXT("Signal service_main thread/n"));
   }
}

// Thread procedure for all three worker threads.

DWORD WINAPI ThreadProc(LPVOID lpParameter)
{
   INT nThreadNum = (INT)lpParameter;
   TCHAR szOutput[25];

   while(WaitForSingleObject(hStopEvent, 1000) != WAIT_OBJECT_0)
   {
   // Just to have something to do, it will beep every second.
      Sleep(1000);
      wsprintf(szOutput,TEXT("/nThread %d says Beep/n"),nThreadNum);
      OutputDebugString(szOutput); //Send visual to debugger.
   }

   return 0;
}

//  Wraps SetServiceStatus.

void SetTheServiceStatus(DWORD dwCurrentState, DWORD dwWin32ExitCode,
                         DWORD dwCheckPoint,   DWORD dwWaitHint)
{
   SERVICE_STATUS ss;  // Current status of the service.

   // Disable control requests until the service is started.
   if (dwCurrentState == SERVICE_START_PENDING)
      ss.dwControlsAccepted = 0;
   else
      ss.dwControlsAccepted =
         SERVICE_ACCEPT_STOP|SERVICE_ACCEPT_SHUTDOWN;
         // Other flags include SERVICE_ACCEPT_PAUSE_CONTINUE
         // and SERVICE_ACCEPT_SHUTDOWN.

   // Initialize ss structure.
   ss.dwServiceType             = SERVICE_WIN32_OWN_PROCESS;
   ss.dwServiceSpecificExitCode = 0;
   ss.dwCurrentState            = dwCurrentState;
   ss.dwWin32ExitCode           = dwWin32ExitCode;
   ss.dwCheckPoint              = dwCheckPoint;
   ss.dwWaitHint                = dwWaitHint;

   // Send status of the service to the Service Controller.
   if (!SetServiceStatus(ssh, &ss))
      ErrorStopService(TEXT("SetServiceStatus"));
}

//  Handle API errors or other problems by ending the service and
//  displaying an error message to the debugger.

void ErrorStopService(LPTSTR lpszAPI)
{
   INT t;
   TCHAR   buffer[256]  = TEXT("");
   TCHAR   error[1024]  = TEXT("");
   LPVOID lpvMessageBuffer;
   DWORD  dwWaitRes;

   wsprintf(buffer,TEXT("API = %s, "), lpszAPI);
   lstrcat(error, buffer);

   ZeroMemory(buffer, sizeof(buffer));
   wsprintf(buffer,TEXT("error code = %d, "), GetLastError());
   lstrcat(error, buffer);

   // Obtain the error string.
   FormatMessage(
      FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
      NULL, GetLastError(),
      MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
      (LPTSTR)&lpvMessageBuffer, 0, NULL);

   ZeroMemory((LPVOID)buffer, (DWORD)sizeof(buffer));
   wsprintf(buffer,TEXT("message = %s"), (TCHAR *)lpvMessageBuffer);
   lstrcat(error, buffer);

   // Free the buffer allocated by the system.
   LocalFree(lpvMessageBuffer);

   // Write the error string to the debugger.
   OutputDebugString(error);

   // If you have threads running, tell them to stop. Something went
   // wrong, and you need to stop them so you can inform the SCM.
   SetEvent(hStopEvent);

   // Wait for the threads to stop.
   for (t=1;TRUE;t++)
   {
      if ((dwWaitRes = WaitForMultipleObjects(3,hThreads,TRUE,1000))
           == WAIT_OBJECT_0)
         break;
      else if ((dwWaitRes == WAIT_FAILED)||
               (dwWaitRes == WAIT_ABANDONED))
         break; // Our wait failed
      else
      {
         SetTheServiceStatus(SERVICE_STOP_PENDING, 0, t, 3000);
      }
   }

   // Stop the service.
   SetTheServiceStatus(SERVICE_STOPPED, GetLastError(), 0, 0);
}
【CarMaker实战指南】从零构建环形测试场景与车辆连续仿真 本文详细介绍了如何使用CarMaker软件从零构建环形测试场景与实现车辆连续仿真。通过分步指导搭建闭环道路、连接交叉路口、规划车辆轨迹等核心操作,帮助用户掌握CarMaker的基础应用技巧,并提供了常见问题排查与进阶优化建议,适用于车辆耐久性测试和自动驾驶算法验证等场景。 阅读详情

相关推荐

FR4板材实战:用ADS 2023快速设计微带线低通滤波器(附Line-Cal计算技巧)

本文详细介绍了使用ADS 2023在FR4板材上设计1-3GHz微带线低通滤波器的工程技巧。通过Line-Cal工具快速计算微带线参数,解决FR4板材的介质损耗和阻抗匹配问题,提供从集总参数转换到分布参数的实用方法,并分享版图设计与实测验证的关键要点,帮助工程师高效完成射频滤波器设计。

omega的博客 714

Creating a Simple Win32 Service in C++

Nigel ThompsonMicrosoft Developer Network Technology GroupNovember 1995AbstractThis article describes how to create simple Microsoft® Windows NT® services using Microsoft Visual C++®. The ser

todototry的专栏 1万+

SpringBoot Redis入门(一)——redis、Lettuce、Redisson使用

Lettuce是SpringBoot自带的Redis客户端,性能比Redis要好, 是线程安全的,支持同步编程,异步编程,响应式编程,自动重新连接,主从模式,集群模块,哨兵模式Redisson 是一个在 Redis 的功能基础上实现的 Java 驻内存数据网格客户端。实现了分布式和可扩展的 Java 数据结构,提供很多分布式相关操作服务,例如分布式锁,分布式集合,可通过 Redis 支持延迟队列。

朗朗的博客 3834

Creating a Windows Service

Creating a Windows Service NOTE: Ok, I am being lazy here. The project name in this sample code is mcWebService which is a spell mistake. I meant to put mcWinService. And now I don't want to ch...

dcryefx44317的博客 136

从零到一:RF-DETR如何通过神经架构搜索重塑实时目标检测的边界

RF-DETR通过创新的权重共享神经架构搜索(NAS)机制,重新定义了实时目标检测的技术边界。这项技术在COCO等基准测试中表现卓越,成为首个AP超过60的实时检测器,同时保持跨领域稳定性和高效推理速度。RF-DETR的动态架构调整能力使其能够适应从嵌入式设备到云端服务器的各种硬件环境,为工业质检、自动驾驶等场景提供灵活解决方案。

weixin_29061531的博客 470

手把手搭建你的第一个OCT仿真模型:用Python和光学仿真库重现A-SCAN信号

本文详细介绍了如何使用Python和光学仿真库搭建OCT仿真模型,重现A-SCAN信号。从环境配置、干涉仪建模到信号处理全流程,手把手教你实现光学相干层析成像的核心技术。通过代码示例和性能优化技巧,帮助开发者深入理解OCT原理并应用于医疗诊断等领域。

weixin_42562079的博客 241

dubbo分布式Service不可以创建Error creating bean with name 'XXXXXX'

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'demoService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.fa...

无敌风火棒棒糖 1144

睿尔曼超轻量仿人机械臂--机械臂接口介绍

本文介绍了睿尔曼超轻量仿人机械臂的控制器接口及末端接口。

realman_Rop的博客 3439

Creating a WCF Service

从今天起开始跟着一个教程写一个关于WCF的系列博客,希望能够坚持下来。  作为第一篇,来一个简单点的,创建一个WCF服务。先看一张整体的图片。    通过上图可以看出,就是想通过一个WCF服务,来满足两个客户端的需求。一个是java web服务通过http访问,另一个则是windows web 服务通过Tcp访问。下面开始实战。  1.创建WCF服务      先创建一个类

1641

实战分享:如何用Jetson Nano+HM30图传搭建低空YOLOv5检测系统(附VNC避坑指南)

本文详细介绍了如何利用Jetson Nano和HM30图传搭建低空YOLOv5检测系统,重点解决了无人机应用中的硬件连接和VNC延迟优化问题。通过对比WiFi直连与图传方案的优劣,提供了网络配置、VNC优化及YOLOv5模型加速的实战技巧,帮助开发者实现高效稳定的低空目标检测系统。

weixin_29174385的博客 110

操作系统实验四 进程运行轨迹的跟踪与统计(哈工大李治军)

实验4 进程运行轨迹的跟踪与统计 实验目的 掌握 Linux 下的多进程编程技术; 通过对进程运行轨迹的跟踪来形象化进程的概念; 在进程运行轨迹跟踪的基础上进行相应的数据统计,从而能对进程调度算法进行实际的量化评价,更进一步加深对调度和调度算法的理解,获得能在实际操作系统上对调度算法进行实验数据对比的直接经验。 实验内容 进程从创建(Linux 下调用 fork())到结束的整个过程就是进程的生命期,进程在其生命期中的运行轨迹实际上就表现为进程状态的多次切换,如进程创建以后会成为就绪态;当该进程被调度以

leoabcd12的博客 4654

Springboot controller service 单元测试 Error creating bean

问题: spring boot项目进行单元测试时,照着网上教程测试自己的controller一直失败找了一上午没找到原因。下午终于成功了。 原因: 教程的controller都不带service引用,和带service引用的controller测试方法有一点区别。 写法1:不带service引用的 controller单元测试: ConController 为需要测试的 controller,里面所有方法都不带 service引用。 @RunWith(SpringRunner.clas..

zhuqiuguang的博客 679

Trae+雪球MCP,化身专业的股票分析师(玩转100个MCP系列第二弹)

大家好呀,我是程序员夏叶,深度AI编程使用者,专注于分享AI编程方面的使用技巧、经验以及前沿资讯,有兴趣的可以关注我的公众号,一起学习,共同进步。第二弹,使用Trae+雪球MCP,获取股票实时行情,分析股票走向等,写代码间隙看看股票价格,研究研究行情,哈哈。真的很不错呀,最关键的是,足够隐蔽,同事以为你在写代码,实际你在浏览股票行情,哈哈,感兴趣的可以试一下哦。说实话,沧州明珠这只股我算是砸手里了,不过还好买的不多,哈哈。

cpp加油站的专栏 518

Creating a Custom Web Service

This programming task provides an overview of how to create a custom Web service that operates within the context of Microsoft Windows SharePoint Services 3.0. The task steps through the process of ...

weixin_30819163的博客 182

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

Android Service:Creating a Started Service

  Creating a Started Service     一个被开始的service(started service)是另一个组件调用startService()开启的,结果是这个service的onStartCommand()方法被调用。   当一个service被start,它就拥有一个独立于开始它的组件的生命周期,这个service可以无限地在后台运行,即使开始它的那个组件被...

weixin_34025051的博客 93

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

mybatis:Creating a new SqlSession Closing non transactional SqlSession

Creating a new SqlSession SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@42607e80] was not registered for synchronization because synchronization is not active JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@56ebc6bb] w.

yzh_1346983557的博客 4万+

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

【练习】6.Creating a Batch Service

Business Data Create a Business Class Create an Intermediate Processor Put Together a Batch Job Make the Application Executable Build an executable JAR

m0_59348479的博客 266

Running in a Background Service 之Creating a Background Service

IntentService 不能和UI交互,如果想将结果呈现在UI上,必须将结果发给Activity,让其帮忙显示 其次IntentService 只能顺序的处理Request,如果上一个Request没有处理完,则发送一个Request的时候必须等待. IntentService 中的操作不能被中断. 一般通过实现IntentService的子类来新建一个IntentService

jason的笔记 965

ssm框架的Error creating bean with name ‘itemController‘报错问题

HTTP状态 500 - 内部服务器错误 类型 异常报告 消息 Servlet[springmvc]的Servlet.init()引发异常 描述 服务器遇到一个意外的情况,阻止它完成请求。 例外情况 javax.servlet.ServletException: Servlet[springmvc]的Servlet.init()引发异常 org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.jav.

qq_43562912的博客 385

Lab 2: Creating a Service

PurposeEstimated time to complete this lab: 25 minutesA Composite Web Application Block service is a supporting class that provides functionality to other components (the components can be in the

shclhs的专栏 597

Creating a Map Service Browser

Concepts A Web service is a set of related application functions that can be programmatically invoked over the Internet. ArcGIS Server Web services can be accessed by any development language that ca...

weixin_30445169的博客 167

使用Restlet创建一个简单的web service(Creating a simple web service with Restlet)

Creating a simple web service with Restlet Posted by Philippe Van Nuijs at 16:12 Restlet是一个轻权的RESt框架。遗憾的是没有很好的示例来描述如何使用它。虽然示例代码包里有一些,但是都比较初级,没有涉及如何创建一个完整的REST web service示例(使用create,update,delete功能)

yumengkk的专栏 6896

Android-Creating a Bound Service

When creating a service that provides binding, you must provide an IBinder that provides the programming interface that clients can use to interact with the service. There are three ways you can def

ShareUs的专栏 625

Creating a .NET Web Service

  Creating a .NET Web ServiceBy Chris Peiris Rating: 3.9 out of 5Rate this article document.write("print this article")

也想快乐的生活 1777

Creating and Calling ASP.NET AJAX Web Service

Hi, First we will create a new ASP.NET Ajax web site(Visual Studio 2005) or ASP.NET web site (version 3.5) if we are using Orcas(Visual

clszd0717的专栏 380

学习:Creating a Custom Web Service for SharePoint(转)

Creating a Custom Web Service for SharePoint Category: Microsoft Office SharePoint Server 2007, Windows SharePoint Services 3.0 There are a couple of Microsoft articles available on th...

diaodi4538的博客 255
上一篇: Writing a Control Handler Function
下一篇: Opening an SCManager Database
普通网友
博客等级 码龄22年 1万+粉丝 3156原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值