如何从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
最满意答案
当您说您只能访问仅从UI访问的
User
属性时,您是部分正确的。 正确答案是 -当通过UI访问
Edit
方法时,它实际上是通过ASP.Net控制器初始化管道,该环境负责当前的浏览器会话并分配User
变量。HttpContext
目前可用。但是当你创建像这样的控制器变量时 -
var setvalcon = new SetValuesController(); setvalcon.Edit(item);
您正在绕过所有这些初始化代码。 没有
HttpContext
,这只是另一个普通对象,因此它没有填充User
属性。回答问题:
- 只有在从UI调用它时,控制器才能访问Windows用户属性吗?
=>是的,绝对正确,因为您正在浏览ASP.Net管道。 但它不仅适用于Windows用户,而是HttpContext中的所有内容。
- 是否无法在未通过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 theUser
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 theUser
property populated.ANSWER TO QUESTIONS:
- 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.
- 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.
相关问答
更多-
基于日期的角度ui-grid过滤为null或不为null(Angular ui-grid filtering based on date is null or not null)[2023-04-20]
我在你的plunker的这个叉子里解决了你的问题。 更新:好的,我使用的解决方案是在dateActionedKey的末尾添加一个名为dateActionedKey的隐藏列: { field: 'dateActionedKey', filter: { term: $scope.term }, visible: false } 问题是你在事后指定的属性过滤了列 - 如果没有actionDate ,值为'1',则值为actionDate - 但过滤器选择器按id过滤,查 ... -
Java - 如何将事件发布到UI线程或调用UI线程的函数?(Java - how post event to UI thread or call a function of UI thread?)[2022-05-23]
值得一看Lars Vogel的Eclipse Jobs和Background Processing教程。 有很多关于如何从Eclipse Jobs在UI线程上运行代码的例子 。 Its worth having a look at the Eclipse Jobs and Background Processing tutorial by Lars Vogel. There are good examples on how to run code on the UI thread from Eclipse ... -
使用requestStart , sync , requestEnd来显示和隐藏微调器是不明智的。 这样,您必须编写代码以在每个数据源中显示和隐藏微调器。 如果显示/隐藏的代码发生了变化,会发生什么? 更好的想法是定义一个全局处理程序,在第一次请求后显示微调器并在最后一次响应后隐藏微调器。 在这种情况下, Global Ajax Event Handlers是最好的。 .ajaxStart() : Register a handler to be called when the first Ajax req ...
-
ScriptUI:脚本独立工作,但不从UI按钮调用时(ScriptUI: Script works alone, but not when called from UI button)[2022-09-22]
您将不得不将窗口从palette更改为dialog 。 很明显,Illustrator实现不能从调色板访问document 。 在Adobe论坛https://forums.adobe.com/thread/841889中查看此主题如果您确实需要调色板,则似乎有一种使用BridgeTalk运行代码的解决方法。 这对我来说似乎很愚蠢: - / You will have to change your window from a palette to a dialog. Apparently Illustrat ... -
为什么每次调用此函数时该值都不会设置为空?(Why is the value not set to null each time this function is called?)[2023-05-28]
当您调用节流阀时,会创建一个新的闭包,其中lastEventTimestamp被定义为null 。 内部函数有一个对该变量的引用,所以当该函数返回时,闭包仍然有一个引用,并保持其状态: function test() { } var myTest = test.throttle(100); myTest(); myTest(); 然后,当您重复调用函数myTest (返回的函数)时,它将作用于lastEventTimestamp变量的同一个实例。 请注意,调用该函数将不会执行赋值lastEventTi ... -
如果DOM中没有指定ID的元素,则document.getElementById()函数返回null 。 在循环中创建的元素尚未添加到DOM中,它们被附加到不在DOM中的tr (它只是从您的函数返回而未被附加到任何内容)。 只需在创建元素时直接添加所需的文本即可: makeTableRow(){ let self = this; let row = document.createElement('tr'); for (let i = 0; i < 1; i++) { le ...
-
当您说您只能访问仅从UI访问的User属性时,您是部分正确的。 正确答案是 - 当通过UI访问Edit方法时,它实际上是通过ASP.Net控制器初始化管道,该环境负责当前的浏览器会话并分配User变量。 HttpContext目前可用。 但是当你创建像这样的控制器变量时 - var setvalcon = new SetValuesController(); setvalcon.Edit(item); 您正在绕过所有这些初始化代码。 没有HttpContext ,这只是另一个普通对象,因此它没有填充Use ...
-
尽管Javascript函数的类为null,但它仍然被调用(Javascript function keeps being called although its class is null)[2022-10-02]
x持有对myclass的引用。 myclass正在执行shout 。 当你将x设置为null ,你只是摆脱了对myclass的引用,而不是myclass对象本身。 x held a reference to myclass. myclass is executing shout. When you set x to null, you are just getting rid of your reference to myclass, not the myclass object itself. -
Chrome扩展程序中的Firebase UI:signInSuccess永远不会被调用(Firebase UI in Chrome extension: signInSuccess never called)[2022-10-03]
它似乎是firebaseUI中的一个错误: 而不是使用此链接: 我现在正在使用这个: 现在它的工作原理 - 感谢bojeil-google的支持! It's ... -
确实,变量没有在回调中分配。 我认为有两种方法可以解决这个问题: 使用setTimeout()换行取消订阅: setTimeout(() => _loadGroupsSubscription.unsubscribe()); 添加.delay(0)运算符,基本上是相同的技巧: var _loadGroupsSubscription = getRootGroupNodes() .retry(3) .delay(0) .subscribe( groups => { console ...