如何使用 Facebook Sdk C# 控制每个 Post 之间的时间

How to Control the time between each Post Using Facebook Sdk C#

我正在使用 Facebook Sdk C# 开发用于在 Facebook 群组中发布帖​​子的桌面程序。该软件将很快发布我需要一种方法来控制发布速度和发布上限,每次发布与另一次发布之间的间隔不少于 10 秒。如何做这样的方法?

我的class

        public static string UploadPost(string groupid, string intTitle, string inMessage, string inLinkCaption, string inLinkUrl, string inLinkDescription, string inLinkUrlPicture)
    {
        object obj;
        Facebook.JsonObject jsonObj;
        FacebookClient client;
        string access_token = AppSettings.Default.AccessToken.ToString();

        client = new FacebookClient(access_token);

        var args = new Dictionary<string, object>();
        args["message"] = inMessage;
        args["caption"] = inLinkCaption;
        args["description"] = inLinkDescription;
        args["name"] = intTitle;
        args["picture"] = inLinkUrlPicture;
        args["link"] = inLinkUrl;

        if ((obj = client.Post("/" + groupid + "/feed", args)) != null)
        {
            if ((jsonObj = obj as Facebook.JsonObject) != null)
            {
                if (jsonObj.Count > 0)
                    return jsonObj[0].ToString();
            }
        }

        return string.Empty;
    }

    internal static bool UploadPost(string p1, string p2)
    {
        throw new NotImplementedException();
    }
}

}

我的提交按钮

        private void btnPost_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < lstgroupsbox.Items.Count; i++)
        {
            if (Class1.UploadPost(lstgroupsbox.Items[i].ToString(), "amine", txtStatus.Text, "googl", txtLink.Text, "seach", txtImagePath.Text) != string.Empty)
                label23.Text=""+lstgroups.Items[i].Text;
        }
        //foreach (var item in lstgroupsbox.Items)
        //{
        //    if (Class1.UploadPost(item.ToString(), "amine", txtStatus.Text, "googl", 2, "seach", txtImagePath.Text) != string.Empty)
        //        label23.Text=""+lstgroups.Items[i].Text;
        //}

    }

我建议您查看 Timer class。你可以这样做的一种方式:

创建一个ConcurrentQueue(不是普通队列,因为它不是线程安全,查看线程安全)你的主要class。这是您的上传 "job list"。还添加一个 Timer 并为其 Elapsed 事件创建一个事件处理程序方法(有关说明,请参阅此答案中的第一个 link)。事件处理程序方法将执行您的作业。

然后创建一个新的 class,其中包含 post 的信息和上传它所需的详细信息。这是您的上传作业 class.

在您的 for loop 提交按钮的事件处理程序中,您创建该作业的实例 class 并将它们排入您的作业列表(ConcurrentQueue),而不是上传图像。添加完所有内容后,您将启动主 class' 计时器。前面提到的 Elapsed 事件处理程序方法将从队列中取出下一个项目(如果为空则停止?)并将其上传到 Facebook。

编辑: 所以,在你的表单 class 中,先添加一条 using 语句:

using System.Collections.Concurrent;

然后在 class 的顶部附近添加这些:

private System.Timers.Timer _jobTimer = new Timer(10000);

private ConcurrentQueue<UploadJob> _jobQueue = new ConcurrentQueue<UploadJob>();

这些转到您的表单的构造函数(在 InitializeComponent() 方法调用之后):

_jobTimer.SynchronizingObject = this;
// ^  The Elapsed event will be run on the same thread as this.
//    This way we won't get exceptions for trying to access the form's labels 
//    from another thread (the Timer is run on its own thread).
_jobTimer.Elapsed += OnJobTimedEvent;

然后在表单中的某处添加 OnJobTimedEvent 方法 class:

private void OnJobTimedEvent(object sender, ElapsedEventArgs e)
{
    UploadJob job;
    if (_jobQueue.TryDequeue(out job)) // Returns false if it fails to return the next object from the queue
    {
        if (Class1.UploadPost(job.Group,
            job.Name,
            job.Message,
            job.Caption,
            job.Link,
            job.Description,
            job.Picture) != string.Empty)
        {
            // Post was uploaded successfully
        }
    }
    else
    {
        // I believe we can assume that the job queue is empty.
        // I'm not sure about the conditions under which TryDequeue will fail
        // but if "no more elements" isn't the only one, we could add
        // (_jobQueue.Count == 0)
        // to the if statement above
        _jobTimer.Stop();
        // All uploads complete
    }
}

如您所见,它使用了UploadJob。那class完全可以分开成一个单独的文件:

public class UploadJob
{
    public string Group { get; protected set; }

    public string Name { get; protected set; }

    public string Message { get; protected set; }

    public string Caption { get; protected set; }

    public string Link { get; protected set; }

    public string Description { get; protected set; }

    public string Picture { get; protected set; }

    public UploadJob(string group,
                     string name,
                     string message,
                     string caption,
                     string link,
                     string description,
                     string picture)
    {
        Group = group;
        Name = name;
        Message = message;
        Caption = caption;
        Link = link;
        Description = description;
        Picture = picture;
    }
}

最后我们到达您的按钮单击事件处理程序:

private void btnPost_Click(object sender, EventArgs e)
{
    for (int i = 0; i < lstgroupsbox.Items.Count; i++)
    {
        _jobQueue.Enqueue(lstgroupsbox.Items[i].ToString(),
            "amine",
            txtStatus.Text,
            "googl",
            txtLink.Text,
            "seach",
            txtImagePath.Text);
    }
    _jobTimer.Start();
}

就我个人而言,我可能会将所有这些都分离到一个 UploadManager class 或其他东西中,让 class 担心计时器和其他一切,但这应该可以正常工作。