陷入后台工作者逻辑

发布于 2024-11-05 05:42:25 字数 921 浏览 3 评论 0 原文

我有点困惑。

我有一个后台工作者,它在 dowork 方法中关闭并获取一个 pdf 文件(byte[])供我打开。完成后,在 runworker 完成的方法中,我创建 pdf 显示对象并显示它。

但是,我想检查 dowork 方法中这个 byte[] 是否为空,如果是,则转到新的数据库位置(这需要一组新的方法调用来获取不同的 pdf),

我实际上无法看到我该怎么做这(除了在后台工作人员中生成一个新的后台工作人员之外 - 如果这甚至可能的话:))

这是我目前拥有的代码设置,希望这应该说明我遇到的问题,

    byte[] pdf;
    void method_DoWork(object sender, DoWorkEventArgs e)
    {

        pdf = myObject.getPdf();

    }

    void method_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        if (pdf!= null)
        {
            displayPdf(pdf);
        }
        else
        {
            goAndGetAnotherPDF();
        }

    }

显然我可以检查 null相反,在 dowork 方法中。那里有没有说

oid method_DoWork(object sender, DoWorkEventArgs e)
if(pdf != null)
{
    callRunWorkerCompleted()
}
else
{
    doSomeOtherStuffAndStoreInLocalVariables();
}

谢谢”

I am a little confused.

I have a background worker which in its dowork method goes off and gets a pdf file (byte[]) for me to open. when its done, in the runworker completed method i create my pdf display object and display it.

However, i want to check if this byte[] is null in the dowork method, and if so go to a new database location (which requires a new set of method calls to get a different pdf)

I cant actually see how i can do this (other than spawning a new background worker within a background worker - if thats even possible :) )

Here is the code setup i have at the moment, and hopefully this should illustrate the problem i am running in to

    byte[] pdf;
    void method_DoWork(object sender, DoWorkEventArgs e)
    {

        pdf = myObject.getPdf();

    }

    void method_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        if (pdf!= null)
        {
            displayPdf(pdf);
        }
        else
        {
            goAndGetAnotherPDF();
        }

    }

obviously i can check for null in the dowork method instead. Is there anyway from there saying

v

oid method_DoWork(object sender, DoWorkEventArgs e)
if(pdf != null)
{
    callRunWorkerCompleted()
}
else
{
    doSomeOtherStuffAndStoreInLocalVariables();
}

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

逐鹿 2024-11-12 05:42:25

好了,DoWork 方法已经在后台线程中运行了。你为什么不继续在那里做所有的工作呢?当 DoWork 方法完成时,无论如何,回调都会触发。

void method_DoWork(object sender, DoWorkEventArgs e)
{
    pdf = myObject.getPdf();

    if (pdf == null)
    {
        pdf = SomeOtherGetMethod();
    }
}

我认为没有必要仅在第一个返回为空的情况下启动另一个线程。如果第一个获取方法失败,只需调用第二个获取方法。

Well, the DoWork method is already running in the background thread. Why don't you just continue on and do all of the work there? When the DoWork method completes, regardless of how, the callback will fire.

void method_DoWork(object sender, DoWorkEventArgs e)
{
    pdf = myObject.getPdf();

    if (pdf == null)
    {
        pdf = SomeOtherGetMethod();
    }
}

I don't see any need to launch another thread just for the case where the first return is null. Just call the second fetch method if the first fails.

甜尕妞 2024-11-12 05:42:25

您能否在 DoWork 线程中检查 PDF 是否为空,如果是,则切换数据库并再次加载 pdf?

void method_DoWork(object sender, DoWorkEventArgs e)
    {
       pdf = myObject.getPdf();
       if(pdf == null)
       {
         //Switch database or whatever and call again
         pdf = myObject.getPdf();
       }
    }

Can you just check if the PDF is null in the DoWork thread and, if so, switch databases and load the pdf again?

void method_DoWork(object sender, DoWorkEventArgs e)
    {
       pdf = myObject.getPdf();
       if(pdf == null)
       {
         //Switch database or whatever and call again
         pdf = myObject.getPdf();
       }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文