首页 \ 问答 \ 如何从C执行命令并终止它(How to execute a command from C and terminate it)

如何从C执行命令并终止它(How to execute a command from C and terminate it)

我有这样的命令:

./mjpg_streamer -i "./input_uvc.so -n -f 15 -r 1280x720" -o "./output_http.so -n -w ./www "

用于通过以太网传输视频。 目前我正在运行终端,而对于退出,我只需按Ctrl + c 。 但我需要使用c代码来做到这一点。 有没有可能或有其他方法可用?

谢谢。


I have command like:

./mjpg_streamer -i "./input_uvc.so -n -f 15 -r 1280x720" -o "./output_http.so -n -w ./www "

for streaming video through Ethernet. Currently I am running through terminal, and for exit I just press Ctrl+c. But I need to do this using c-code. Is it possible or any other method available ?.

Thanks.


原文:https://stackoverflow.com/questions/18530572
更新时间:2023-07-22 13:07

最满意答案

当您说您只能访问仅从UI访问的User属性时,您是部分正确的。 正确答案是 -

当通过UI访问Edit方法时,它实际上是通过ASP.Net控制器初始化管道,该环境负责当前的浏览器会话并分配User变量。 HttpContext目前可用。

但是当你创建像这样的控制器变量时 -

var setvalcon = new SetValuesController();
setvalcon.Edit(item);

您正在绕过所有这些初始化代码。 没有HttpContext ,这只是另一个普通对象,因此它没有填充User属性。

回答问题:

  1. 只有在从UI调用它时,控制器才能访问Windows用户属性吗?

=>是的,绝对正确,因为您正在浏览ASP.Net管道。 但它不仅适用于Windows用户,而是HttpContext中的所有内容。

  1. 是否无法在未通过UI调用的函数中访问用户属性?

=>仅当您手动分配否则否。

更多洞察力:

基本上,你想要实现的是一个非常糟糕的设计。 没有地方,记得无处,你应该从控制器内部调用一个控制器,除非它是基础方法调用的子类。 从另一个控制器内部调用另一个控制器的唯一方法是使用“重定向”方法重定向执行。 没有人会阻止你像这样打电话给控制器,但这显示了糟糕的设计原则..

解决你的情况的最好方法是这样的 -

public class ModelService {
    public void Edit(IPrincipal user, SetValue setValue){
        setValue.Value = valuestring;
            var original = db.SetValue.Find(setValue.SetValueID);
            bool modified = original.Value != setValue.Value;
            if (modified)
            {
                var rev = new RevisionHistory();
                rev.CreatedOn = original.ModifiedOn;
                rev.ModifiedBy = User.Identity.Name; //If modified exception on this line
                db.Entry(original).CurrentValues.SetValues(setValue);
                db.RevisionHistory.Add(rev);
            }
            original.ModifiedOn = DateTime.Now;
            original.ModifiedBy = User.Identity.Name; //if not modified exception on this line
            db.Entry(original).State = EntityState.Modified; 
            db.SaveChanges();
    }
}

然后在两个控制器构造函数中 -

public class ControllerOne : Controller {
    private readonly ModelService _modelService
    //constructor
    public ControllerOne(){
         _modelService= new ModelService ();
    }

    public ActionResult Edit([Bind(Include = "SetValueID,Value,Status,TcSetID,OptionValueID,CreatedOn,CreatedBy,ModifiedOn,ModifiedBy")] SetValue setValue)
    {
        //Many lines removed for simplicity. all the variables used in the code are assigned.
        if (ModelState.IsValid)
        {
            _modelService.Edit(User, Setvalue);
        }
    }

//controller 2
public class HomeController: Controller {
    private readonly ModelService _modelService
    //constructor
    public ControllerOne(){
         _modelService= new ModelService ();
    }

    public ActionResult Index()
    {        
        foreach(var item in db.SetValue.ToList())
        {
            _modelService.Edit(User, item);
        }     
    }
}

您可以从IoC Container获取依赖注入的帮助,这是更好的方法。


You are partially correct when you are saying you can only access the User property only accessing from the UI. The correct answer is -

When accessing the Edit method through the UI, it is actually going through the ASP.Net controller initializer pipeline and that environment takes care of the current browser session and assigns the User variable. HttpContext is available at this time.

But when you are creating the controller variable like this -

var setvalcon = new SetValuesController();
setvalcon.Edit(item);

You are bypassing all those initialization codes. No HttpContext and this is just another normal object and thus it does not have the User property populated.

ANSWER TO QUESTIONS:

  1. Is it that the controller gets access to the Windows user property only when it is called from the UI?

=> Yes, absolutely right, because you are going through the ASP.Net pipeline. But it is not only for Windows user, it is all those things that re in a HttpContext.

  1. Is it not possible to access the User Property in a function that is not called via UI?

=> Only if, you manually assign it otherwise NO.

MORE INSIGHT:

Basically, what you are trying to achieve is a very poor design. Nowhere, remember nowhere, you are supposed to call a controller from inside a controller unless it is a subclass to base method call. The only way you can call another controller from inside another controller is redirecting the execution by using "Redirect" methods. No one will stop you from calling controller like this, but this shows poor design principle..

The best way to solve your situation is like this -

public class ModelService {
    public void Edit(IPrincipal user, SetValue setValue){
        setValue.Value = valuestring;
            var original = db.SetValue.Find(setValue.SetValueID);
            bool modified = original.Value != setValue.Value;
            if (modified)
            {
                var rev = new RevisionHistory();
                rev.CreatedOn = original.ModifiedOn;
                rev.ModifiedBy = User.Identity.Name; //If modified exception on this line
                db.Entry(original).CurrentValues.SetValues(setValue);
                db.RevisionHistory.Add(rev);
            }
            original.ModifiedOn = DateTime.Now;
            original.ModifiedBy = User.Identity.Name; //if not modified exception on this line
            db.Entry(original).State = EntityState.Modified; 
            db.SaveChanges();
    }
}

Then in both the controllers constructors -

public class ControllerOne : Controller {
    private readonly ModelService _modelService
    //constructor
    public ControllerOne(){
         _modelService= new ModelService ();
    }

    public ActionResult Edit([Bind(Include = "SetValueID,Value,Status,TcSetID,OptionValueID,CreatedOn,CreatedBy,ModifiedOn,ModifiedBy")] SetValue setValue)
    {
        //Many lines removed for simplicity. all the variables used in the code are assigned.
        if (ModelState.IsValid)
        {
            _modelService.Edit(User, Setvalue);
        }
    }

//controller 2
public class HomeController: Controller {
    private readonly ModelService _modelService
    //constructor
    public ControllerOne(){
         _modelService= new ModelService ();
    }

    public ActionResult Index()
    {        
        foreach(var item in db.SetValue.ToList())
        {
            _modelService.Edit(User, item);
        }     
    }
}

You can take help from IoC Container for dependency injection, that is even better approach.

相关问答

更多

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。