English 中文(简体)
开始调换工作, 但对变量没有写值
原标题:beginInvoke works but doesn t write value to a variable

我有一个线条函数。 在这个函数中, 我试图使用 HtmlPage. Window. Invoke 方法, 使用 beginInvoke , 因为我不能直接在线条函数中使用它。 但是变量 设置 总是“ ” 。 它显示消息框, 所以BeginInvoke正常工作 。 但是为什么它不写入任何变量?

Thread.Sleep(15000);
if (!this.Dispatcher.CheckAccess())
        this.Dispatcher.BeginInvoke(new Action(() => MessageBox.Show("Try to reload data!")));
string obpath = "C:\program files\windows sidebar\Gadgets\res.txt";
            string path1 = "C:\program files\windows sidebar\Gadgets\settings.txt";
string settings = "";
if (!this.Dispatcher.CheckAccess())
        this.Dispatcher.BeginInvoke(new Action(() => settings = HtmlPage.Window.Invoke("ReadFile", new object[] { path1 }) as string));
问题回答

BeginInvoke安排异步执行的操作。因此,在将值分配给设置时,当前功能可能已退出,设置不再可见。如果你想等到它完成,你需要使用BeginInvoke的返回值。

BeginInvoke异步执行,这意味着它将操作排队到调度器中并立即返回。如果需要结果,可以使用Dispatcher。调用

您应该注意,除非绝对必要,否则使用Invoke被认为是一种糟糕的做法。您在等待同步的线程上浪费了大量时间。考虑重构代码,这样就不会发生这种情况(例如,将所有这些代码放在传递给BeginInvoke的单个Action中。)

编辑

在Silverlight中,不可能等待Dispatcher操作完成,因此您必须重构代码以不依赖它。

if (!this.Dispatcher.CheckAccess())
    this.Dispatcher.BeginInvoke(new Action(() => settings = HtmlPage.Window.Invoke("ReadFile", new object[] { path1 }) as string));

如果check-access返回true,会发生什么?在这种情况下,你不执行任何代码,这就是发生的事情,所以你的IF语句非常奇怪。

还有,方法开始时thread.sleep(15秒)的目的是什么?

If you really want to assign local variable settings to result of that execution you can create a ManualResetEvent, pass it with delegate for dispatcher invocation, and Set it after assignment is done. Then after calling beginInvoke() you wait for you event to fire , and once it fires your settings will be assigned.

尽管我认为这一切都需要大规模的重构。

欢迎来到异步编程的世界,它将理智的逻辑变成看起来疯狂的意大利面条代码。

与其这样做:

string settings = "";
if (!this.Dispatcher.CheckAccess())
  this.Dispatcher.BeginInvoke(new Action(() => 
    settings = HtmlPage.Window.Invoke("ReadFile", new object[] { path1 }) as string));
// use settings here...  

做这个

string settings = "";
if (!this.Dispatcher.CheckAccess())
  this.Dispatcher.BeginInvoke(new Action(() => {
    settings = HtmlPage.Window.Invoke("ReadFile", new object[] { path1 }) as string)
    // use settings here ...
  }
) else {
    settings = HtmlPage.Window.Invoke("ReadFile", new object[] { path1 }) as string)
    // use settings here ...
};




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签