Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6146545
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:57:53+00:00 2026-05-23T18:57:53+00:00

I have an application that imports data read from text files from a directory

  • 0

I have an application that imports data read from text files from a directory into a database. I have a UI that allows the user to click an import button and begin importing data and when the user clicks on that button again I wanted to stop importing the data in those files. I began using threads to allow this, so that I would not freeze up the UI while data was being imported. But Im having a few issues. I started using thread.Abort() to kill the thread after the user stops wanting to import but when the user clicks import again, some duplicate data is added to the database because it begins reading at the top of the text file which I dont want. I have been told to use ManualResetEvents and Thread.Join() to trigger for the import to finish, but im confused how that is supposed to work. Right now my code looks like this:

    public ManualResetEvent event1 = new ManualResetEvent(false);
    public Thread workerThread;


    public Form1
    {
        InitializeComponent();
    }

    private void importButton_Click(object sender, EventArgs e)
    {
        if(importButton.Text == "Begin Import")
        {
            importButton.Text = "Stop Import";
            //runs a service that begins reading and importing data and writing to
            //a "console" box.
            Service service = new Service(consoleBox);
            //run the new thread to begin importing data
            workerThread = new Thread(service.importData);
            workerThread.Start();

        }
        else
        {
            importButton.Text = "Begin Import";
            event1.Set();
            while(!event1.WaitOne(TimeSpan.FromSeconds(4)))
            {    //imports data for 30 more text files 
                  service.importData(30);
                  workerThread.Join();

            }


        }
    }

Basically what im trying to do is to keep the tread looping and checking to see if there is any files to be read, if there is then import The Data otherwise sleep for 4 seconds. Should I be using a threading Timer for this? I am a bit unsure of what to do.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T18:57:54+00:00Added an answer on May 23, 2026 at 6:57 pm

    Do not, in any way, block the UI thread by calling Thread.Join or ManualResetEvent.WaitOne. This will do exactly what you were trying to prevent; freeze the UI. Instead you need to create the MRE with its state initially set to true. Then in the importData method you need to periodically call WaitOne to see if importing should proceed (when the event is signaled) or pause (when the event is unsignaled).

    Here is a rough sketch of how you would call WaitOne inside the importData method. Obviously, you would need to make adjustments to fit it into your specific implementation.

    private void importData()
    {
      foreach (string filePath in GetFilesInSomeDirectory())
      {
        event1.WaitOne(); // Block when the MRE is unsignaled.
      }
    }
    

    Then from your importButton.Click event handler you can call event1.Reset to pause the import operation or event1.Set to resume it.

    Also, you should try to avoid calling Thread.Abort at all costs. It usually leads to more problems unless extra-special-nearly-impossible care is taken to avoid corrupting the state of the AppDomain.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application where i need to write some data to the serial
I have a legacy database which contains simple data structures (no CODE refs thank
I have a web-application. As part of this, I need users of the app
I'm am creating an ASP.NET 2.0 web application that uses a BasePage.aspx that is
For a school project, I am writing an iOS iPad application in which the
My code produces a zip file from Google App Engine for loading in a
This is my first program using Haskell. I'm writing it to put into practice
I am using VBA to strip out the headers for an import. The issue
Hello My error is produced in generating a zip file. Can you inform what
I am using visual studio 2010 and tried to create a class library which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.