首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Edge.js访问DLL文件

如何使用Edge.js访问DLL文件
EN

Stack Overflow用户
提问于 2022-08-02 12:05:40
回答 1查看 104关注 0票数 0

我想使用OpenHardwareMonitor模块使用node.js访问edge.js DLL文件,并监控计算机的CPU和GPU温度。我在Visual中创建了一个C#控制台应用程序,并将该DLL文件添加到引用中。当我运行下面的Program.cs文件时,我成功地达到了CPU温度。

Program.cs;

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenHardwareMonitor.Hardware;

namespace Get_CPU_Temp5
{
    class Program
    {
        public class UpdateVisitor : IVisitor
        {
            public void VisitComputer(IComputer computer)
            {
                computer.Traverse(this);
            }
            public void VisitHardware(IHardware hardware)
            {
                hardware.Update();
                foreach (IHardware subHardware in hardware.SubHardware) subHardware.Accept(this);
            }
            public void VisitSensor(ISensor sensor) { }
            public void VisitParameter(IParameter parameter) { }
        }
        static void GetSystemInfo()
        {
            UpdateVisitor updateVisitor = new UpdateVisitor();
            Computer computer = new Computer();
            computer.Open();
            computer.CPUEnabled = true;
            computer.Accept(updateVisitor);
            for (int i = 0; i < computer.Hardware.Length; i++)
            {
                if (computer.Hardware[i].HardwareType == HardwareType.CPU)
                {
                    for (int j = 0; j < computer.Hardware[i].Sensors.Length; j++)
                    {
                        if (computer.Hardware[i].Sensors[j].SensorType == SensorType.Temperature)
                            Console.WriteLine(computer.Hardware[i].Sensors[j].Name + ":" + computer.Hardware[i].Sensors[j].Value.ToString() + "\r");
                    }
                }
            }
            computer.Close();
        }
        static void Main(string[] args)
        {
            while (true)
            {
                GetSystemInfo();
            }
        }
    }
}

我希望在我的Nodejs项目中使用edge.js运行相同的代码,并访问CPU温度。我试过了,但我有错误。我检查了computer.Hardware[i].Sensors[j].Value的数据类型。它叫System.Single。我想我需要一个返回值,但我不太明白怎么做。

我不知道我哪里出了问题。如果你能帮忙我会很高兴的

代码语言:javascript
复制
    var edge = require('edge-js');

    var add7 = edge.func({
    source: function() {/*


        using System;
        using System.Data;
        using System.Threading.Tasks;
        using System.Collections.Generic;
        using System.Linq;
        using System.Text;
        using OpenHardwareMonitor.Hardware;

        public class Startup
        {
             public async Task<object> Invoke(object input)
            {              
                   
                    Computer computer = new Computer() ;
                    computer.Open();
                    computer.CPUEnabled = true ;

                    for(int i = 0 ; i<computer.Hardware.Length;i++){
                        
                        if(computer.Hardware[i].HardwareType == HardwareType.CPU){
                            for(int j = 0 ; j< computer.Hardware[i].Sensors.Length;j++){
                                if(computer.Hardware[i].Sensors[j].SensorType == SensorType.Temperature)
                                Console.WriteLine(computer.Hardware[i].Sensors[j].Name + ":" + computer.Hardware[i].Sensors[j].Value.ToString()+"\r");
                               
                            }
                        }
                    }

                    computer.Close();

                
            }
        }
    */},
    references: [ 'System.Data.dll' ,'OpenHardwareMonitorLib.dll' ]
    //references: [ 'OpenHardwareMonitorLib.dll' ],
    
});

这就是错误;

代码语言:javascript
复制
return edge.initializeClrFunc(options);
                ^
Error: Unable to compile C# code.
----> Errors when compiling as a CLR library:
c:\Users\Elessar\AppData\Local\Temp\uphki0bx\uphki0bx.0.cs(14,40) : warning CS1998: Bu zaman uyumsuz yöntemde 'await' işleçleri yok ve zaman uyumlu çalışacak. 'await' işlecini kullanarak engelleyici olmayan API çağrılarını beklemeyi veya 'await Task.Run(...)' kullanarak bir arka plan iş parçacığında CPU bağlantılı iş yapmayı düşünün.
c:\Users\Elessar\AppData\Local\Temp\uphki0bx\uphki0bx.0.cs(14,40) : error CS0161: 'Startup.Invoke(object)': tüm kod yolları değer döndürmez
----> Errors when compiling as a CLR async lambda expression:
c:\Users\Elessar\AppData\Local\Temp\vyq5ricq\vyq5ricq.0.cs(5,42) : error CS1513: } bekleniyor
c:\Users\Elessar\AppData\Local\Temp\vyq5ricq\vyq5ricq.0.cs(43,9) : error CS1519: class, struct veya interface üyesi bildiriminde geçersiz 'return' belirteci
c:\Users\Elessar\AppData\Local\Temp\vyq5ricq\vyq5ricq.0.cs(43,35) : error CS1001: Tanımlayıcı bekleniyor
c:\Users\Elessar\AppData\Local\Temp\vyq5ricq\vyq5ricq.0.cs(45,1) : error CS1022: Tür veya ad alanı tanımı ya da dosya sonu bekleniyor
c:\Users\Elessar\AppData\Local\Temp\vyq5ricq\vyq5ricq.0.cs(16,22) : error CS0542: 'Startup': üye adları kendilerini kapsayan türle aynı olamaz
    at Object.exports.func (C:\Users\Elessar\Desktop\Remote-Control\backend\node_modules\edge-js\lib\edge.js:177:17)
    at Object.<anonymous> (C:\Users\Elessar\Desktop\Remote-Control\backend\app\controllers\pc-control\dll.js:13:17)
    at Module._compile (node:internal/modules/cjs/loader:1103:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1155:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
    at node:internal/main/run_main_module:17:47 {
  Message: 'Unable to compile C# code.\n' +
    '----> Errors when compiling as a CLR library:\n' +
    "c:\\Users\\Elessar\\AppData\\Local\\Temp\\uphki0bx\\uphki0bx.0.cs(14,40) : warning CS1998: Bu zaman uyumsuz yöntemde 'await' işleçleri yok ve zaman uyumlu çalışacak. 'await' işlecini kull

C:\Users\Elessar\Desktop\Remote-Control\backend\app\controllers\pc-control>node dll.js

C:\Users\Elessar\Desktop\Remote-Control\backend\node_modules\edge-js\lib\edge.js:177
    return edge.initializeClrFunc(options);
                ^
Error: Unable to compile C# code.
----> Errors when compiling as a CLR library:
c:\Users\Elessar\AppData\Local\Temp\ktjpgbj0\ktjpgbj0.0.cs(14,40) : warning CS1998: Bu zaman uyumsuz yöntemde 'await' işleçleri yok ve zaman uyumlu çalışacak. 'await' işlecini kullanarak engelleyici olmayan API çağrılarını beklemeyi veya 'await Task.Run(...)' kullanarak bir arka plan iş parçacığında CPU bağlantılı iş yapmayı düşünün.
c:\Users\Elessar\AppData\Local\Temp\ktjpgbj0\ktjpgbj0.0.cs(14,40) : error CS0161: 'Startup.Invoke(object)': tüm kod yolları değer döndürmez
----> Errors when compiling as a CLR async lambda expression:
c:\Users\Elessar\AppData\Local\Temp\haykoruc\haykoruc.0.cs(5,42) : error CS1513: } bekleniyor
c:\Users\Elessar\AppData\Local\Temp\haykoruc\haykoruc.0.cs(43,9) : error CS1519: class, struct veya interface üyesi bildiriminde geçersiz 'return' belirteci
c:\Users\Elessar\AppData\Local\Temp\haykoruc\haykoruc.0.cs(43,35) : error CS1001: Tanımlayıcı bekleniyor
c:\Users\Elessar\AppData\Local\Temp\haykoruc\haykoruc.0.cs(45,1) : error CS1022: Tür veya ad alanı tanımı ya da dosya sonu bekleniyor
c:\Users\Elessar\AppData\Local\Temp\haykoruc\haykoruc.0.cs(16,22) : error CS0542: 'Startup': üye adları kendilerini kapsayan türle aynı olamaz
    at Object.exports.func (C:\Users\Elessar\Desktop\Remote-Control\backend\node_modules\edge-js\lib\edge.js:177:17)
    at Object.<anonymous> (C:\Users\Elessar\Desktop\Remote-Control\backend\app\controllers\pc-control\dll.js:13:17)
    at Module._compile (node:internal/modules/cjs/loader:1103:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1155:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
    at node:internal/main/run_main_module:17:47 {
  Message: 'Unable to compile C# code.\n' +
    '----> Errors when compiling as a CLR library:\n' +
    "c:\\Users\\Elessar\\AppData\\Local\\Temp\\ktjpgbj0\\ktjpgbj0.0.cs(14,40) : warning CS1998: Bu zaman uyumsuz yöntemde 'await' işleçleri yok ve zaman uyumlu çalışacak. 'await' işlecini kullanarak engelleyici olmayan API çağrılarını beklemeyi veya 'await Task.Run(...)' kullanarak bir arka plan iş parçacığında CPU bağlantılı iş yapmayı düşünün.\n" +
    "c:\\Users\\Elessar\\AppData\\Local\\Temp\\ktjpgbj0\\ktjpgbj0.0.cs(14,40) : error CS0161: 'Startup.Invoke(object)': tüm kod yolları değer döndürmez\n" +
    '----> Errors when compiling as a CLR async lambda expression:\n' +
    'c:\\Users\\Elessar\\AppData\\Local\\Temp\\haykoruc\\haykoruc.0.cs(5,42) : error CS1513: } bekleniyor\n' +
    "c:\\Users\\Elessar\\AppData\\Local\\Temp\\haykoruc\\haykoruc.0.cs(43,9) : error CS1519: class, struct veya interface üyesi bildiriminde geçersiz 'return' belirteci\n" +
    'c:\\Users\\Elessar\\AppData\\Local\\Temp\\haykoruc\\haykoruc.0.cs(43,35) : error CS1001: Tanımlayıcı bekleniyor\n' +
    'c:\\Users\\Elessar\\AppData\\Local\\Temp\\haykoruc\\haykoruc.0.cs(45,1) : error CS1022: Tür veya ad alanı tanımı ya da dosya sonu bekleniyor\n' +
    "c:\\Users\\Elessar\\AppData\\Local\\Temp\\haykoruc\\haykoruc.0.cs(16,22) : error CS0542: 'Startup': üye adları kendilerini kapsayan türle aynı olamaz",
  Data: {},
  InnerException: null,
  TargetSite: {},
  StackTrace: '   konum: EdgeCompiler.CompileFunc(IDictionary`2 parameters)',
  HelpLink: null,
  Source: 'edge-cs',
  HResult: -2146233079,
  name: 'System.InvalidOperationException'
}

-编辑--

当我尝试这个;

代码语言:javascript
复制
public class Startup
    {
         public class UpdateVisitor : IVisitor
                    {
                        public void VisitComputer(IComputer computer)
                        {
                            computer.Traverse(this);
                        }
                        public void VisitHardware(IHardware hardware)
                        {
                            hardware.Update();
                            foreach (IHardware subHardware in hardware.SubHardware) subHardware.Accept(this);
                        }
                        public void VisitSensor(ISensor sensor) { }
                        public void VisitParameter(IParameter parameter) { }
                    }

                public async Task<object> Invoke(object _)
                {
                   
                    
                        while (true)
                        {
                            GetSystemInfo();
                            return await Task.FromResult<object>(null);
                        }
                    
                }

            static void GetSystemInfo()
            {
                UpdateVisitor updateVisitor = new UpdateVisitor();
                Computer computer = new Computer();
                computer.Open();
                computer.CPUEnabled = true;
                computer.Accept(updateVisitor);
                for (int i = 0; i < computer.Hardware.Length; i++)
                {
                    if (computer.Hardware[i].HardwareType == HardwareType.CPU)
                    {
                        for (int j = 0; j < computer.Hardware[i].Sensors.Length; j++)
                        {
                            if (computer.Hardware[i].Sensors[j].SensorType == SensorType.Temperature)
                                    Console.WriteLine(computer.Hardware[i].Sensors[j].Name + ":" + computer.Hardware[i].Sensors[j].Value.ToString() + "\r");
                        }
                    }
                }
                computer.Close();
            }
    }

我犯了这个错误

代码语言:javascript
复制
CS5001 Program does not contain a static Main method suitable for an entry point

我也试过了;

代码语言:javascript
复制
 public async Task Invoke(object _)
    {     
       while (true)
          {
             GetSystemInfo();
             return await Task.FromResult<object>(null);
           }
                    
    }

上面写着;

代码语言:javascript
复制
Since 'Startup.Invoke(object)' is an async method that returns 'Task', a return keyword must not be followed by an object expression. Did you intend to return 'Task<T>'?
EN

回答 1

Stack Overflow用户

发布于 2022-08-02 15:26:07

async Task<object>承诺返回值,但是您的代码没有返回值。

尝试Task而不是Task<object>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73207257

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档