C#threading&事件 [英] C# threading & events

查看:41
本文介绍了C#threading&事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好。


我正在创建一个小应用程序。必须使用第三方API。

此API中的函数没有返回值,但触发事件,如OK

或不正确的事件参数。


我需要使用这个API我必须有一个同步模块,所以我需要

强制同步查看这个API的asynchrone模型。


我以为这会要求等待一些事件,因为我不想做一个循环吃掉所有的cpu功能我想用的是什么? $>
完成此任务的几个线程。


虽然我知道这是非常常见而且给我带来了一些麻烦。

简单的场景。


我希望也许这里的人可以帮我一把。


我需要的就是说,制作一些使用API​​的代码,但我的代码

必须是同步的,而API不是。


所以我可以做类似的事情:

--------------------------

public MyClass

{

private bool InProcess;

private bool ItWentWell;


public bool Function1()

{

InProcess = true;


APIClass apic = new APIClass();


apic.FunctionAPI1_Success + = EventHandler(FunctionOK);

apic.FunctionAPI1_Failed + = EventHandler(FunctionERROR);


apic.FunctionAPI1();


...

等待InProcess从TRUE变为FALSE。

一旦发生这种情况,请返回ItWentWell。

}


public void FunctionOK()

{

Inprocess = false;


ItWentWell = true;

}


public void FunctionERROR()

{

InProcess = false;


ItWentWell = false;

}

}


// --- ---像控制台应用程序的Main一样。 ---

MyClass mc = new MyClass();


if(mc.Function1())

{

Consile.WriteLine(API调用顺利。);

}

其他

{

Consile.WriteLine(API调用变坏了。)

}

------------- -------------


我知道上面的内容不起作用,只是为了给出一张照片

我想要完成的事情。 Somwhere周围有等待

的事件发生应该很可能是使用一个新的线程没有

吃掉所有的CPU功率,但我不能得到我试图工作的东西。


任何帮助都将非常感谢,谢谢:)


/ Aidal

Hi NG.

I''m creating a little app. that has to use a 3rd party API.
The functions in this API has no return value but trigger events like OK
or NOT OK with event args.

Where I need to use this API I must have a synchrone modul so I need to
force a synchrone look to this asynchrone model of the API.

I was thinking this would call for some waiting for events and since I
don''t wanna make a loop eat all the cpu power I was thinking of using a
few threads to accomplish this.

It''s giving me some trouble though I''m aware it''s a pretty common and
simple scenario.

I was hoping maybe somone here could lend me a hand.

What I need is as said, to make some code that uses the API but my code
has to be synchrone while the API is not.

So I can do somthing like:
--------------------------
public MyClass
{
private bool InProcess;
private bool ItWentWell;

public bool Function1()
{
InProcess = true;

APIClass apic = new APIClass();

apic.FunctionAPI1_Success += EventHandler(FunctionOK);
apic.FunctionAPI1_Failed += EventHandler(FunctionERROR);

apic.FunctionAPI1();

...
Wait for InProcess to change from TRUE to FALSE.
Once this happens, return ItWentWell.
}

public void FunctionOK()
{
Inprocess = false;

ItWentWell = true;
}

public void FunctionERROR()
{
InProcess = false;

ItWentWell = false;
}
}

//--- somwhere like the Main of a console app. ---
MyClass mc = new MyClass();

if(mc.Function1())
{
Consile.WriteLine("API call went well.");
}
else
{
Consile.WriteLine("API call went bad.")
}
--------------------------

I''m aware that the above doesn''t work, it''s only to give a picture of
what I''m trying to accomplish. Somwhere around the part with the waiting
for the event to occur should most likely be using a new thread not to
eat all the cpu power, but I can''t get the things I''ve tried to work.

Any help would be much appreciated, thanks :)

/Aidal

推荐答案



" Aidal" < no@address.comwrote in message

news:45 ********************* @ dreader2.cybercity.dk ...

"Aidal" <no@address.comwrote in message
news:45*********************@dreader2.cybercity.dk ...

你好。


我正在创建一个小应用程序。必须使用第三方API。

此API中的函数没有返回值,但触发事件,如OK

或不正确的事件参数。


我需要使用这个API我必须有一个同步模块,所以我需要

强制同步查看这个API的asynchrone模型。


我以为这会要求等待一些事件,因为我不想做一个循环吃掉所有的cpu功能我想用的是什么? />
完成此任务的几个线程。
Hi NG.

I''m creating a little app. that has to use a 3rd party API.
The functions in this API has no return value but trigger events like OK
or NOT OK with event args.

Where I need to use this API I must have a synchrone modul so I need to
force a synchrone look to this asynchrone model of the API.

I was thinking this would call for some waiting for events and since I
don''t wanna make a loop eat all the cpu power I was thinking of using a
few threads to accomplish this.



使用.NET内置的EventWaitHandle类。或者将您的算法转换为状态机。

Use the EventWaitHandle class built in to .NET. Or convert your algorithm
to a state machine.


Aidal skrev:
Aidal skrev:

你好。


我正在创建一个小应用程序。必须使用第三方API。

此API中的函数没有返回值,但触发事件,如OK

或不正确的事件参数。


我需要使用这个API我必须有一个同步模块,所以我需要

强制同步查看这个API的asynchrone模型。


我以为这会要求等待一些事件,因为我不想做一个循环吃掉所有的cpu功能我想用的是什么? $>
完成此任务的几个线程。


虽然我知道这是非常常见而且给我带来了一些麻烦。

简单的场景。


我希望也许这里的人可以帮我一把。


我需要的就是说,制作一些使用API​​的代码,但我的代码

必须是同步的,而API不是。


所以我可以做类似的事情:

--------------------------

public MyClass

{ <无线电通信/>
private bool InProcess;

private bool ItWentWell;


public bool Function1()

{

InProcess = true;


APIClass apic = new APIClass();


apic.FunctionAPI1_Success + = EventHandler(FunctionOK );

apic.FunctionAPI1_Failed + = EventHandler(FunctionERROR);


apic.FunctionAPI1();


...

等待InProcess从TRUE变为FALSE。

一旦发生这种情况,请返回ItWentWell。

}


public void FunctionOK()

{

Inprocess = false;


ItWentWell = true; < br $>
}


public void FunctionERROR()

{

InProcess = false;


ItWentWell = false;

}

}


// --- somwhere like the主要的骗局唯一的应用---

MyClass mc = new MyClass();


if(mc.Function1())

{

Consile.WriteLine(API调用顺利。);

}

其他

{

Consile.WriteLine(API调用变坏了。)

}

------------- -------------


我知道上面的内容不起作用,只是为了给出一张照片

我想要完成的事情。 Somwhere周围有等待

的事件发生应该很可能是使用一个新的线程没有

吃掉所有的CPU功率,但我不能得到我试图工作的东西。


任何帮助都将非常感谢,谢谢:)


/ Aidal
Hi NG.

I''m creating a little app. that has to use a 3rd party API.
The functions in this API has no return value but trigger events like OK
or NOT OK with event args.

Where I need to use this API I must have a synchrone modul so I need to
force a synchrone look to this asynchrone model of the API.

I was thinking this would call for some waiting for events and since I
don''t wanna make a loop eat all the cpu power I was thinking of using a
few threads to accomplish this.

It''s giving me some trouble though I''m aware it''s a pretty common and
simple scenario.

I was hoping maybe somone here could lend me a hand.

What I need is as said, to make some code that uses the API but my code
has to be synchrone while the API is not.

So I can do somthing like:
--------------------------
public MyClass
{
private bool InProcess;
private bool ItWentWell;

public bool Function1()
{
InProcess = true;

APIClass apic = new APIClass();

apic.FunctionAPI1_Success += EventHandler(FunctionOK);
apic.FunctionAPI1_Failed += EventHandler(FunctionERROR);

apic.FunctionAPI1();

...
Wait for InProcess to change from TRUE to FALSE.
Once this happens, return ItWentWell.
}

public void FunctionOK()
{
Inprocess = false;

ItWentWell = true;
}

public void FunctionERROR()
{
InProcess = false;

ItWentWell = false;
}
}

//--- somwhere like the Main of a console app. ---
MyClass mc = new MyClass();

if(mc.Function1())
{
Consile.WriteLine("API call went well.");
}
else
{
Consile.WriteLine("API call went bad.")
}
--------------------------

I''m aware that the above doesn''t work, it''s only to give a picture of
what I''m trying to accomplish. Somwhere around the part with the waiting
for the event to occur should most likely be using a new thread not to
eat all the cpu power, but I can''t get the things I''ve tried to work.

Any help would be much appreciated, thanks :)

/Aidal



任何人都有一个实际的代码示例或指向其中的链接:


Class1.FunctionA调用Class2.FunctionB将(在某些情况下)点)

触发一个事件。

Class1.FunctionA必须在事件发生之前不能返回,因为它需要将b * b的返回值基于什么事件说喜欢真/假。


Aka Class1.FunctionA必须等待由

Class2.FunctionB触发的事件才能知道要返回什么。


如上所述,我假设应该使用一些线程,但我不确定。


/ Aidal

Anyone have an actual code example or a link to one where:

Class1.FunctionA calls Class2.FunctionB which will (at some point)
trigger an event.
Class1.FunctionA must not return before the event occuers because it
needs to base its return value on what the event "said" like true/false.

Aka Class1.FunctionA must wait for the event triggered by
Class2.FunctionB to occuer before it knows what to return.

As mentioned above I assume some threading should be used but I''m not sure.

/Aidal


这样吗?


使用系统;

使用System.Threading;


static class Program

{

static void Main()

{

new ClassA()。MethodA() ;

}

}

class ClassA

{

public void MethodA( )

{

ClassB b = new ClassB();

string theSomething ="" ;;

ManualResetEvent evt = new ManualResetEvent(false);

b.SomethingHappened + = delegate

{

theSomething = b.TheSomething;

evt.Set();

};

ThreadPool.QueueUserWorkItem(委托{

b.MethodB();

});

evt.WaitOne();

Console.WriteLine(theSomething);

}

}

班ClassB

{

公共事件EventHandler SomethingHappened;

私人字符串_theSomething;

public string TheSomething {get {return _theSomething;}}

protected void OnSomethingHappened()

{

EventHandler handler = SomethingHappened;

if(handler!= null)handler(this,EventArgs.Empty);

}

public void MethodB()

{

_theSomething = Console.ReadLine();

OnSomethingHappened();

}

}
Like so?

using System;
using System.Threading;

static class Program
{
static void Main()
{
new ClassA().MethodA();
}
}
class ClassA
{
public void MethodA()
{
ClassB b = new ClassB();
string theSomething = "";
ManualResetEvent evt = new ManualResetEvent(false);
b.SomethingHappened += delegate
{
theSomething = b.TheSomething;
evt.Set();
};
ThreadPool.QueueUserWorkItem(delegate {
b.MethodB();
});
evt.WaitOne();
Console.WriteLine(theSomething);
}
}
class ClassB
{
public event EventHandler SomethingHappened;
private string _theSomething;
public string TheSomething {get {return _theSomething;}}
protected void OnSomethingHappened()
{
EventHandler handler = SomethingHappened;
if (handler != null) handler(this, EventArgs.Empty);
}
public void MethodB()
{
_theSomething = Console.ReadLine();
OnSomethingHappened();
}
}


这篇关于C#threading&amp;事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆