小编m4n*_*n07的帖子

以特定的时间间隔执行功能

任务是Processfunction()每x(例如x = 10)秒执行一个函数(例如)。

使用下面的代码,我能够Processfunction()每x秒调用一次。

问题:如何处理函数需要10秒钟以上才能完成执行的情况?

一种方法是有一个标志来指示Processfunction()执行结束并在调用之前检查它Processfunction()。有一个更好的方法吗 ?


#include <pthread.h>
#include <unistd.h> // for sleep() and usleep()

void *timerthread(void *timer_parms) {  

  struct itimerspec new_value;
  int max_exp, fd;
  struct timespec now;
  uint64_t exp;
  ssize_t s;

  struct timer_params *p =(struct timer_params*)timer_parms;

  printf("starttimer Start\n");
  /* Create a CLOCK_REALTIME absolute timer with initial
     expiration and interval as specified in command line */
  if (clock_gettime(CLOCK_REALTIME, &now) == -1)
    handle_error("clock_gettime");

  new_value.it_value.tv_sec = now.tv_sec;
  new_value.it_value.tv_nsec = now.tv_nsec + p->tv_nsec; …
Run Code Online (Sandbox Code Playgroud)

c pthreads timer

4
推荐指数
1
解决办法
1万
查看次数

从Android应用程序存储Excel文件中的数据

我的应用程序生成数据,我想分享这些数据,例如转移到我的PC并工作或作为excel表发送给其他人.

我们可以以编程方式将Android应用程序中的数据存储到Excel工作表吗?那么我们如何打开Excel文件并写入呢?

java android

3
推荐指数
1
解决办法
9365
查看次数

警报对话框间距问题与textview和编辑文本

我无法在AlertDialog中看到"我的数据",因为它与EditText重叠.

           Context context = MyActivity.this;
           AlertDialog.Builder alert = new AlertDialog.Builder(context);

            alert.setTitle(" NEW TITLE");
            alert.setMessage("MESSAGE 1");

            final TextView tx = new TextView(this);
            tx.setText("MY DATA");
            alert.setView(tx);

            // Set an EditText view to get user input   
              final EditText input = new EditText(this); 
              alert.setView(input);


            alert.setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                    /* User clicked OK so do some stuff */
                }
            });

            alert.setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                    /* User clicked Cancel so do some …
Run Code Online (Sandbox Code Playgroud)

android textview android-edittext android-alertdialog

3
推荐指数
1
解决办法
5168
查看次数

Plink传递参数

plink user@10.220.60.xx -t '/home/user/test/testpgm'
Run Code Online (Sandbox Code Playgroud)

我可以使用上面的plink cmd在windows机器上运行以下程序,该程序驻留在Linux机器上.

#include<stdio.h>
int main(int argc,char *argv[])
{
   int i;
   char buf[30];
   printf("Test Pgm \n");
   printf("No of Arguments=%d\n",argc);
   printf("Enter a string:");
   fflush(stdout);
   gets(buf);
   printf("Input str:%s \n",buf);

   return 0;
}

gcc test.c -o testpgm
Run Code Online (Sandbox Code Playgroud)

问题:如何将命令行参数传递给此函数?我试过了

plink user@10.220.60.xx -t '/home/user/test/testpgm arg1'

bash: /home/user/test/testpgm arg1: No such file or directory
Run Code Online (Sandbox Code Playgroud)

c linux ssh plink

3
推荐指数
1
解决办法
3185
查看次数

getFilesDir()android函数是未定义的,如果我不扩展我的类与活动

如果我有我的班级

import java.io.File;

import java.io.FileOutputStream;

//Extends Activity

public class MyClass extends Activity
{

File fileDir = getFilesDir(); //no error

   . ....    
}
Run Code Online (Sandbox Code Playgroud)

但如果我不扩展MyClass,我会收到一个错误,说"getFilesDir()"未定义.

就像是

   public class MyClass2
    {

    ....

    File fileDir = getFilesDir(); //error

    }
Run Code Online (Sandbox Code Playgroud)

android

2
推荐指数
1
解决办法
6016
查看次数

strptime时间转换 - 年和月的错误值

我正在尝试将strptime()日期/时间字符串解析为其组件值.作为测试,我尝试解析固定的日期时间字符串并使用以下代码打印结果值:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

int main(void)
{
        struct tm tm;
        memset(&tm, 0, sizeof(struct tm));
        strptime("2001/11/12 18:31:01", "%Y/%m/%d %H:%M:%S", &tm);
        printf("year: %d; month: %d; day: %d;\n",
                        tm.tm_year, tm.tm_mon, tm.tm_mday);
        printf("hour: %d; minute: %d; second: %d\n",
                        tm.tm_hour, tm.tm_min, tm.tm_sec);
        exit(EXIT_SUCCESS);
}
Run Code Online (Sandbox Code Playgroud)

我得到的输出是:

year: 101; month: 10; day: 12;
hour: 18; minute: 31; second: 1
Run Code Online (Sandbox Code Playgroud)

其他值看起来不错,但年和月与输入(2001/11/12 18:31:01)不匹配.这是为什么?

c strptime

2
推荐指数
1
解决办法
1044
查看次数

Android:将数据写入文件

使用下面的代码,我能够创建一个名为output.txt的新文件,并且我能够写入数据.问题是,一旦我关闭我的应用程序然后再次打开我的应用程序,此文件将被重新创建.因为我在onCreate()中创建了这个.

但我想只创建一次文件,然后我想在那之后附加数据.

private File outputFile = null;
    FileOutputStream fOut = null;
    OutputStreamWriter osw = null;


        @Override
     public void onCreate(Bundle savedInstanceState) {
       ....
      if(outputFile == null)
               outputFile = new  File("/storage/new/output.txt");

             if(osr==null){
               try {
                   osr = new FileOutputStream(outputFile);
                   out = new DataOutputStream(osr);
               } catch (FileNotFoundException e) {
                   // TODO Auto-generated catch block
                   e.printStackTrace();
               }
             }

             .....
                 try {
                            out.writeBytes(data);
                                out.flush();
                            //out.close();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
Run Code Online (Sandbox Code Playgroud)

android file

1
推荐指数
1
解决办法
7368
查看次数

使用线程的并发TCP服务器

我正在尝试创建一个并发TCP服务器,它接受N已知的多个(N个连接).(例如N = 8)所以我正在尝试创建一个预先编写的TCP服务器.

.....

    for(i=0;i<NUMBER_OF_CONNECTIONS;i++)
    {
       CreateThreads(i);         
    }

    return 0;
}

//Create threads to handle the connection
void CreateThreads( int i )
{
  pthread_create(&thread_tid[i], NULL, thread_function, (void *) i);

  return; 
}

void* thread_function(void *arg)
{
        puts("In thread_function");
        int    client_soc,clilen;

        struct sockaddr_in *clientaddr;

        if( (clientaddr = malloc(addrlen)) == NULL)
          printf("malloc Error\n");

        printf("thread %d starting\n", (int) arg);
        while(1)
        {

                clilen = sizeof(struct sockaddr_in);
                pthread_mutex_lock(&mlock);

                puts("Calling accept \n");
                if ( (client_soc = accept(socket_desc, (struct sockaddr *)&clientaddr,(socklen_t*)&clilen)) < 0)
                {
                    printf("accept error\n");
                }
                pthread_mutex_unlock(&mlock); …
Run Code Online (Sandbox Code Playgroud)

c sockets networking multithreading tcp

1
推荐指数
1
解决办法
2688
查看次数